Artillery with bounce: a modification of Artillery tutorial

Talking about Artillery game, Flash and Users contributions.

I received an email from Massimo M. – an italian reader – that modified the code explained in Create a flash artillery game – step 1 tutorial.

Massimo wrote:

“Hi, my name is Massy and I browsed your tutorials about Flash games. I think they are very good.

I modified a bit your artillery 8 file adding two rows to the code and obtaining some kind of artillery 9. I am attaching it in case you are interested in publishing it.

I’ve just added elastic bounces to cannon balls when they hit the ground, using a low bounce factor to make it “realistic”.

This is the code to replace in the first frame:

Mouse.hide();
gravity = 2;
attachMovie("crosshair", "crosshair", 1);
attachMovie("tank", "tank", 2, {_x:230, _y:350});
crosshair.onEnterFrame = function() {
	this._x = _xmouse;
	this._y = _ymouse;
};
tank.onEnterFrame = function() {
	mousex = _xmouse-this._x;
	mousey = (_ymouse-this._y)*-1;
	angle = Math.atan(mousey/mousex)/(Math.PI/180);
	if (mousex<0) {
		angle += 180;
	}
	if (mousex>=0 && mousey<0) {
		angle += 360;
	}
	if (angle>160) {
		angle = 160;
	}
	if (angle<20) {
		angle = 20;
	}
	firepower = Math.sqrt(mousex*mousex+mousey*mousey);
	if (firepower>200) {
		firepower = 200;
	}
	this.cannon._rotation = angle*-1;
};
function onMouseDown() {
	angle = tank.cannon._rotation-1;
	start_ball_x = tank._x+48*Math.cos(angle*Math.PI/180);
	start_ball_y = tank._y+48*Math.sin(angle*Math.PI/180);
	cannonball_fired = attachMovie("cannonball", "cannonball_"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_ball_x, _y:start_ball_y});
	cannonball_fired.dirx = Math.cos(angle*Math.PI/180)*firepower;
	cannonball_fired.diry = Math.sin(angle*Math.PI/180)*firepower;
	cannonball_fired.onEnterFrame = function() {
		this.diry += gravity;
		this._x += this.dirx/50;
		this._y += this.diry/50;
		if (this._y+this._height/2>350) {
this._y = 350-this._height/2;
this.diry = -this.diry*.3;
}
	};
}

and this is what you’ll get:

As you can see, now cannon balls bounce when they hit the ground.

Here it is the zip file with the source code.

Thank you Massimo!