Artillery with bounce: a modification of Artillery tutorial

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:

ACTIONSCRIPT:
  1. Mouse.hide();
  2. gravity = 2;
  3. attachMovie("crosshair", "crosshair", 1);
  4. attachMovie("tank", "tank", 2, {_x:230, _y:350});
  5. crosshair.onEnterFrame = function() {
  6.     this._x = _xmouse;
  7.     this._y = _ymouse;
  8. };
  9. tank.onEnterFrame = function() {
  10.     mousex = _xmouse-this._x;
  11.     mousey = (_ymouse-this._y)*-1;
  12.     angle = Math.atan(mousey/mousex)/(Math.PI/180);
  13.     if (mousex<0) {
  14.         angle += 180;
  15.     }
  16.     if (mousex>=0 && mousey<0) {
  17.         angle += 360;
  18.     }
  19.     if (angle>160) {
  20.         angle = 160;
  21.     }
  22.     if (angle<20) {
  23.         angle = 20;
  24.     }
  25.     firepower = Math.sqrt(mousex*mousex+mousey*mousey);
  26.     if (firepower>200) {
  27.         firepower = 200;
  28.     }
  29.     this.cannon._rotation = angle*-1;
  30. };
  31. function onMouseDown() {
  32.     angle = tank.cannon._rotation-1;
  33.     start_ball_x = tank._x+48*Math.cos(angle*Math.PI/180);
  34.     start_ball_y = tank._y+48*Math.sin(angle*Math.PI/180);
  35.     cannonball_fired = attachMovie("cannonball", "cannonball_"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_ball_x, _y:start_ball_y});
  36.     cannonball_fired.dirx = Math.cos(angle*Math.PI/180)*firepower;
  37.     cannonball_fired.diry = Math.sin(angle*Math.PI/180)*firepower;
  38.     cannonball_fired.onEnterFrame = function() {
  39.         this.diry += gravity;
  40.         this._x += this.dirx/50;
  41.         this._y += this.diry/50;
  42.         if (this._y+this._height/2>350) {
  43. this._y = 350-this._height/2;
  44. this.diry = -this.diry*.3;
  45. }
  46.     };
  47. }

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!

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

10 Responses to “Artillery with bounce: a modification of Artillery tutorial”

  1. krabex on October 29th, 2007 6:33 pm

    You can add some friction too that also to give some more realism. Velocity *= Friction (0.95 is pretty good)

  2. Seth on October 29th, 2007 9:24 pm

    Looks very cool, it add a nice look to the game.
    But, some balls stay too much time in stage, you may want to slowly fade them out :)
    Sorry for my bad english :s

  3. Massimo M. on October 29th, 2007 11:35 pm

    Good idea for the next time ;)

    it can be a good idea to fade out the ball after 2 or 3 bounces or at least after X seconds

    Massimo :)

  4. zedia.net on October 30th, 2007 6:15 am

    I you want to continue upgrading this game, you should consider ActionScript 3. I already converted it, you could go on from there.

    Here is the source
    http://www.zedia.net/2007/porting-a-game-in-actionscript-2-to-actionscript-3/

  5. Monkios on October 30th, 2007 2:52 pm

    Pretty nice improvment.

    I guess the bounce factor should be customable.

  6. Frederik J on October 30th, 2007 2:58 pm

    Nice. How i just love contributions. I’m on my way, to make another game, with help from this site. Good, Massimo!
    /Frederik

  7. Massimo M. on October 30th, 2007 5:32 pm

    tnx you Fred! ;)

    (for monkios) to change the bounce factor simply change the “0.3″ value in line 44.

    (for Zedia) tnx but for now i prefer to concentrate on version 2 cause i prefer better support for all to see my works ;) i will pass on flash 9 or more later!

  8. Marre2795 on September 21st, 2008 4:19 pm

    its a good tutorial but how can we add wind

Leave a Reply




Trackbacks

  1. Porting a game in ActionScript 2 to ActionScript 3 | zedia flash blog on October 30th, 2007 5:14 am

    [...] I blogged about a post on Emanuele Feronato’s blog. Today he put on is blog some code on an artillery game. It is nice code, but it is in ActionScript 2. I thought it would be fun to try to make the same [...]

  2. A Gem of Flash Game Tutorials | Newbie Game Programmers on December 17th, 2007 4:23 pm

    [...] Create a flash artillery game – Part 1 :: Part 2 :: Modification [...]

Posts