Hi,
I have created a flash AS3 file with two movieclips. One is a ball (ball_mc) and a wall (wall_mc). The ball bounces around off the outer walls of the stage, in horizontal lines.
The ball also changes direction if it bounces off the wall. However its not bouncing off the wall in the right direction?
Could anyone help me with this?
My code looks like this:
var dx:Number=3; //the amount the ball will move horizontally each frame
var dy:Number=5; //the amount the ball will move vertically each frame
this.addEventListener(Event.ENTER_FRAME,moveBall); // call the function moveBall every frame
function moveBall(event:Event): void
{
ball_mc.x=ball_mc.x+dx; //the ball moves horizontally
if(ball_mc.x>stage.stageWidth - ball_mc.width || ball_mc.x<0)
//at either the right edge or left edge
dx=dx* -1; //change horizontal direction
ball_mc.y=ball_mc.y+dy; //the ball moves vertically
if(ball_mc.hitTestObject(wall_mc))
{
if(ball_mc.y > wall_mc.height - ball_mc.height ||
ball_mc.y < wall_mc.y -ball_mc.height)
{
dy=dy* -1;
}
if(ball_mc.x > wall_mc.width - ball_mc.width ||
ball_mc.x < wall_mc.x - ball_mc.width)
{
dx=dx* -1;
}
}
if(ball_mc.y > stage.stageHeight - ball_mc.height || ball_mc.y < 0)
//at either the top or bottom edge
dy=dy* -1; //change vertical direction
}