Quantunet.com

Flash QuickSkills

Collision with sides (Actionscript)  

Actionscript inside a movieclip

 

 

onClipEvent(load) {
speedX =50;
speedY = 30;
}

onClipEvent(enterFrame) {
this._x += speedX;
this._y += speedY;

if (this._x >= 490) {
speedX = -speedX;
} else if (this._x <= 0) {
speedX = -speedX;
}

if (this._y >= 340) {
speedY = -speedY;
} else if (this._y <= 0) {
speedY = -speedY;
}
}

//effective size of the canvas is reduced from 550x400 to
// 490x340 to account for the ball's dimensions

Tells flash to define a value for a term called x_vel as 50 when the movie clip has loaded.

 

onClipEvent(load) {
x_vel =50;
y_vel = 30;
}

When enter each frame add the value of x_vel to the mc position.

An x_vel of 50 represents the addition of 50 pixels every time a frame is entered which becomes 600 pixels a second at a 12 frames per second frame rate.


onClipEvent(enterFrame) {
this._x += x_vel ;
this._y += y_vel;
 

A conditional if statement compares the x coordinate of the mc to 550 ( the edge of the stage) and if it is bigger it changes changes the sign of the mc velocity. however if the position is less than zero its velocity is made negative again. This has the effect or reflecting the velocity of the object each time it encounters a wall ( edge of the stage).

if (this._x >= 490) {
x_vel  = -x_vel ;
} else if (this._x <= 0) {
x_vel  = -x_vel ;
}

If (this._y >= 340) {
y_vel = -y_vel;
} else if (this._y <= 0) {
y_vel = -y_vel;
}
}

© 2005 - 2007 Quantunet.com All Rights Reserved
Home | Terms of Use | Contact Us