An earlier post about making a "Pong" style game, explained how it was possible (albeit in a very long and tedious way) to create the effect of a ball bouncing of a target using many, many monster elements. Thanks to Dan at 2Simple, there is a much easier way that creates the same effect.
If you were attempting to create a bounce when an object reached the edge of the screen, firstly add a sun element and make it transparent. Any code you give the sun element will be exectuted, but the graphic will not show on screen.
Within the sun element add the following code;
_root.car._x +=_root.h_velocity;
_root.car._y +=_root.v_velocity;
if (_root.car._x > 610 ) { _root.h_velocity = -_root.h_velocity; _root.car._x = 590;}
if (_root.car._x < 30 ) { _root.h_velocity = -_root.h_velocity; _root.car._x = 50;}
if (_root.car._y > 450 ) { _root.v_velocity = -_root.v_velocity; _root.car._y = 430;}
if (_root.car._y < 30 ) {_root.v_velocity = -_root.v_velocity; _root.car._y = 50;}
The first two lines define the movement effect when the object hits the wall (h and v are horizontal and vertical speeds (v for velocity). The last four lines tell the activity to reverse the direction of velocity when the element passes a certain pixel value (in the case when the object is less than 30 or greater than 610 pixels across the screen, less than 30 or more than 450 pixels down the screen.
In addition to the sun code - you will also need to add
var v_velocity:Number = 10;
var h_velocity:Number = 7;
to the start game code (by right clicking on the green triangle). These codes set the initial values for the velocity variables used in the sun animation code.


Recent Comments