Tips     Beginners     Intermediate     Advanced     Resources     E-mail me

Arkanoid/Breakout

Part 2:
If you follow the trajectory of the ball you will notice that it is following the same path all the time. How can we improve this?
We need to add in a random element into the bounce so that the angle changes when it rebounds.
This can be done by using the random function. Add in the following in the onClipEvent (load) function below the code for the y_direction = 1.
			randomness= 3;
		
This can be changed to make the rebounds more random. However it looks choppy if the value is too big. Test different values.

Next change the code in the onClipEvent (enterFrame) function so that it now reads.

				if ( this._x < 495 and this._x > 55 ){
					this._x+= x_direction*x_speed;
				} else {
					x_direction*=-1;
					this._x+= x_direction *(random(randomness)+1)*x_speed;
				}
				if ( this._y > 50 and this._y < 345 ){
					this._y+= y_direction*y_speed;
				} else {
					y_direction*=-1;
					this._y+= y_direction *(random(randomness)+1)*y_speed;
				}
		
You should now have a ball "randomly" bouncing within the rectangle.

Back to top     Previous     Next