Erractic Behavior
Hello all.
I have a simple program where two balls bounce off the four sides of the stage and off each other.
The program runs fine for about a minute, then the ball start to act erractically ; e.g. the balls start bouncing in another direction before hitting the sides. Eventually, the motion of the ball behave as if they are in a small contained space.
import flash.events.Event;
import flash.media.Sound;
var xVel:Number = 5;
var yVel:Number = 5;
ball_mc.addEventListener(Event.ENTER_FRAME, redBall);
yball.addEventListener(Event.ENTER_FRAME, yellowBall);
function redBall(evtObj:Event):void
{
ball_mc.x += xVel;
ball_mc.y += yVel;
if (ball_mc.y > stage.stageHeight)
{
yVel *= -1;
}
if (ball_mc.y < 0)
{
xVel *= -1;
}
if (ball_mc.x < stage.stageWidth)
{
xVel *= -1;
}
if (ball_mc.x < 0)
{
xVel *= -1;
}
if (ball_mc.hitTestObject(yball))
{
trace("hit");
xVel *= -1;
}
}
function yellowBall(evtObj:Event):void
{
yball.x += xVel;
yball.y += yVel;
if (yball.y > stage.stageHeight)
{
yVel *= -1;
}
if (yball.y < 0)
{
yVel *= -1;
}
if (yball.x < stage.stageWidth)
{
xVel *= -1;
}
if (yball.x < 0)
{
xVel *= -1;
}
if (yball.hitTestObject(ball_mc))
{
trace("boo");
xVel *= -1;
}
}
