Hi,
This can be done by assigning a variable such as speed to an objects _x or _y position in an enterframe event. Then, when you want to reverse direction, simply apply negative to the speed variable. To see this in practice, create a new movie with a circle movieclip named "ball". Paste this onto the first frame of the movie...
Code:
speed = 10;
this.onEnterFrame = function() {
if (ball._x>=Stage.width) {
speed = -speed;
} else if (ball._x<=0) {
speed = -speed;
}
ball._x += speed;
}
The above only accounts for motion on the _x axis. The same thing would need to apply for the _y axis. To further the above example to include motion on both axis and create a boundary to show the ball bouncing off different boundaries,......... remove the above posted code from frame 1 and replace it with the following function....
Code:
function ballBounce(myTarget) {
speedY = 10;
speedX = 8;
walls = 300;
top = 100;
bottom = 300;
r = myTarget._width/2;
_root.createEmptyMovieClip("outline", 1);
with (_root.outline) {
lineStyle(1, 0xFF00FF, 100);
moveTo(top, bottom);
lineTo(top, bottom);
lineTo(walls, bottom);
lineTo(walls, top);
lineTo(top, top);
lineTo(top, bottom);
}
this.onEnterFrame = function() {
posY = myTarget._y+speedY;
posX = myTarget._x+speedX;
if (posY>(bottom-r)) {
myTarget._y = bottom-r;
speedY = -speedY;
} else if (posY<top+r) {
myTarget._y = top+r;
speedY = -speedY;
} else {
myTarget._y = myTarget._y+speedY;
}
if (posX>(walls-r)) {
myTarget._x = walls-r;
speedX = -speedX;
} else if (posX<top+r) {
myTarget._x = top+r;
speedX = -speedX;
} else {
myTarget._x += speedX;
}
}
}
ballBounce(ball);
This should help you understand the motion involved. You will need to research the hitTest method for detecting when movieclips collide to create a paddle that will change the speed variables upon contact.
Regards
NTD
Bookmarks