Create a flash artillery game - step 1

May 25th update: 2nd part released
October 29th update: modification with a bounce effect developed by Massimo M.

Here we are with the beginning of a new tutorial. We are going to create a flash artillery game. Something like Worms. Or similar.
Well... the first thing we need in an artillery game is...

The crosshair

I created a movieclip linkaged as "crosshair", then on the first frame of the main scene entered this actionscript:

ACTIONSCRIPT:
  1. Mouse.hide();
  2. attachMovie("crosshair", "crosshair", 1);
  3. crosshair.onEnterFrame = function() {
  4.     this._x = _xmouse;
  5.     this._y = _ymouse;
  6. };

Line 1: Hiding the mouse

Line 2: Attaching the movie "crosshair" instanced as "crosshair" at depth 1

Lines 3-5: Every time the crosshair is in the stage, its position is updated to _xmouse and _ymouse coordinates. This means you will move the crosshair with the mouse.

Looks simple... it will get (much) more complicated.

The second thing we need in an artillery game is...

The tank

The tank is the core of an artillery game. You will love your tank. Of course I love mine.

Of course, every tank has its cannon. A big cannon. Size matters.

I created a new movieclip linkaged as "tank" and inside this movieclip I created another one instanced as "cannon". Look at the picture to understand how I placed the movieclips.

Tank

Then the actionscript is:

ACTIONSCRIPT:
  1. Mouse.hide();
  2. attachMovie("crosshair", "crosshair", 1);
  3. attachMovie("tank", "tank", 2, {_x:230, _y:350});
  4. crosshair.onEnterFrame = function() {
  5.     this._x = _xmouse;
  6.     this._y = _ymouse;
  7. };
  8. tank.onEnterFrame = function() {
  9.     mousex = _xmouse-this._x;
  10.     mousey = (_ymouse-this._y)*-1;
  11.     angle = Math.atan(mousey/mousex)/(Math.PI/180);
  12.     if (mousex<0) {
  13.         angle += 180;
  14.     }
  15.     if (mousex>=0 && mousey<0) {
  16.         angle += 360;
  17.     }
  18.     this.cannon._rotation = angle*-1;
  19. };

Line 3: places the cannon on the stage

Lines 9-10 determine the x and y distance between the crosshair and the tank

Lines 11-17 determine the angle with trigonometry. This concept is explained in the tutorial called Create a flash draw game like Line Rider or others - part 3.

Line 18 rotates the cannon according to the angle

Sometimes you may need to limit the cannon angle. It's very simple do it this way:

ACTIONSCRIPT:
  1. Mouse.hide();
  2. attachMovie("crosshair", "crosshair", 1);
  3. attachMovie("tank", "tank", 2, {_x:230, _y:350});
  4. crosshair.onEnterFrame = function() {
  5.     this._x = _xmouse;
  6.     this._y = _ymouse;
  7. };
  8. tank.onEnterFrame = function() {
  9.     mousex = _xmouse-this._x;
  10.     mousey = (_ymouse-this._y)*-1;
  11.     angle = Math.atan(mousey/mousex)/(Math.PI/180);
  12.     if (mousex<0) {
  13.         angle += 180;
  14.     }
  15.     if (mousex>=0 && mousey<0) {
  16.         angle += 360;
  17.     }
  18.     if (angle>160) {
  19.         angle = 160;
  20.     }
  21.     if (angle<20) {
  22.         angle = 20;
  23.     }
  24.     this.cannon._rotation = angle*-1;
  25. };

Lines 18-23 limit the cannon angle between 20 and 160 degrees. Limiting the cannon angle may affect gameplay, use it wisely.

Now it's time to determine the firepower. In this game, we are going to determine it by the distance from the crosshair to the cannon.

ACTIONSCRIPT:
  1. Mouse.hide();
  2. attachMovie("crosshair", "crosshair", 1);
  3. attachMovie("tank", "tank", 2, {_x:230, _y:350});
  4. crosshair.onEnterFrame = function() {
  5.     this._x = _xmouse;
  6.     this._y = _ymouse;
  7. };
  8. tank.onEnterFrame = function() {
  9.     mousex = _xmouse-this._x;
  10.     mousey = (_ymouse-this._y)*-1;
  11.     angle = Math.atan(mousey/mousex)/(Math.PI/180);
  12.     if (mousex<0) {
  13.         angle += 180;
  14.     }
  15.     if (mousex>=0 && mousey<0) {
  16.         angle += 360;
  17.     }
  18.     if (angle>160) {
  19.         angle = 160;
  20.     }
  21.     if (angle<20) {
  22.         angle = 20;
  23.     }
  24.     firepower = Math.sqrt(mousex*mousex+mousey*mousey);
  25.     if (firepower>200) {
  26.         firepower = 200;
  27.     }
  28.     this.cannon._rotation = angle*-1;
  29. };

Lines 24-27 calculate the fire power using the pythagorean theorem (there is no need to publish this example as a .swf because visually it does not change anything).

Now it's time to make our tank do what every tank sould do:

FIRE !!

The first thing to do when you fire a bullet is determine the exit point of the bullet. I mean: the cannon is on a rotating turret, so I have to place the bullet right next to the end of the cannon to give the player the feeling he really fired that bullet.

So I created a new movieclip and linkaged it as "cannonball"

Then I added a new function to the script

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

Line 30: Beginning of the actions to be executed when the player press the fire... pardon, the mouse button.

Lines 32-33: Determining the exit point of the cannonball using with trigonometry. This concept too is explained in the tutorial called Create a flash draw game like Line Rider or others - part 3. The "48" value you can see it given by the cannon lenght (40) plus the ball radius (5) plus 3 "safety pixels" since sine and cosine functions returns non-integer values.

Line 34: Attaching the movieclip with the cannon ball. Notice the "3": it's the depth of the movieclip, and giving a fixed depth assure us to have only one cannon ball on the stage.

FIRE!!! (again)

Now that the cannonball has been released, it's time to make it fly.

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

Line 34: This time I want more than one cannonball in the stage, so I call the attachMovie function giving the cannon ball the next highest depth available. I can do this using the getNextHighestDepth() function. The name of the movieclip is also affected by the next highest depth available.

Line 35: The dirx variable is the horizontal vector of the cannonball, according to its firing angle and the firepower.

Line 36: Same thing for diry, the vertical vector

Line 37: Every time the cannonball enters in a frame (every frame)...

Lines 38-39: ... its _x and _y position changes according to dirx and diry. That "50" is simply a divider to make the ball not to go too fast.

What happens to a cannonball once in the air? It starts falling due to...

Gravity

In an arcade game initially gravity is not calculated in a strict physics formula, so we are going to simulate the gravity in this way:

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.     };
  43. }

Line 2: A variable called gravity is set to 2

Line 39: The gravity value is added to the vertical vector at every frame.

Final result is quite reasonable.

At the moment the tut stops here, download all source codes, give me feedback, change a line or two and publish them as your own game on various game portals :)

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 5 out of 5)
Loading ... Loading ...

» 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.

95 Responses to “Create a flash artillery game - step 1”

  1. NIc on April 28th, 2007 6:21 pm

    Great tutorial. Keep them coming.

  2. intoxicatedLama on April 28th, 2007 8:18 pm

    I love it! I will creat now a game!

  3. hansa on April 28th, 2007 8:19 pm

    just wonder one thing i havent read the full tutorial butt i wonder do you delete the cannon ball or not???

  4. Callum on April 28th, 2007 8:31 pm

    Nice… I cant wait for part 2 it looks like a game on my iPod called parachute. :]

    And Have you got My eMail?

  5. zak on April 28th, 2007 11:03 pm

    this stuff is fantastic

  6. questo on April 30th, 2007 11:36 pm

    nice tutorial, but I want the crosshair to be moved by arrow keys (up arrow counter clockwise down arrow clockwise. I also want a movable charecter

  7. hasna on May 2nd, 2007 10:01 pm
  8. Elmer on May 5th, 2007 2:37 pm

    Loved your tutorial. One little thing I can’t get to work.

    I have made a movieclip instanced as “ballon” And I want the ballon to load next frame where it blow up, when the cannonball hit it.

    So I have this action in the ballon;

    onClipEvent(load) {
    if (this.hitTest( _root.cannonball) ){
    _root.gotoAndPlay(2);
    }

    Maybe there’s another script or maybe my script is wrong. I would appreciate your help. Thanks and bye ;)

  9. sirus on May 6th, 2007 12:07 am

    Really cool

    how do you figure this stuff out in the first place?

  10. Will on May 8th, 2007 6:14 pm

    I liked it. Nice cover of the basics of actionscript and i liked the gravity on the balls.

    I think that the first code you did of the crosshair would be better if you used the “start drag” function instead of making the x and y values the same. It just tends to speed stuff up a little when you have loads of functions going on.

  11. Laura on May 8th, 2007 10:58 pm

    Cool. I can’t wait to get started on my game…nice tank!

  12. levick on May 9th, 2007 11:34 am

    Freekin’ sweet tut man.

  13. Seth on May 11th, 2007 3:17 am

    Great tutorial, waiting second part :)

  14. Joe on May 14th, 2007 3:05 am

    Awesome tut. May I suggest one thing? It would be cool if the cannonballs reacted with each other. For example, if you shot a cannonball at 200 firepower, and on it’s way down you shot another cannonball at 150 firepower, the two would collide and react physically? Just a thought.

  15. hanny on May 14th, 2007 10:08 pm

    i think what would be really help full is if you made stage 2 for this tut and have it so that the “tank” can move around, because i folowed the tut and when i had done all the stages i moderated some parts and made the tank move, however the bullets only went upwards and it didnt function properly..

  16. N00b on May 15th, 2007 9:31 pm

    oH IT didn’t all copy….

  17. conor on May 19th, 2007 9:13 pm

    alrity then ive made this and ive edited it using bits of code
    i have made it so the enemys move towards the tank but and this is a big but, i need the right hittest heres the code foe the enemys. (if the tank moved they would follow the tnk where ever)help anyone?

    code for following tank

    onClipEvent (enterFrame) { if (_root.tank._x > _x) { _x = 5; }}onClipEvent (enterFrame) { if (_root.tank._x_y) { _y = 5; }}onClipEvent (enterFrame) { if (_root.tank._y

  18. conor on May 19th, 2007 9:18 pm

    also i made the balls bounce off the walls just for fun like

    iwas thinking about making that an upgrade for the future i had to take out the grasvity to do that cause i couldent lower it enough

  19. darnpunk on May 20th, 2007 5:45 pm

    Hello guys, I need some help regarding some trigo maths here. Referring to line 32:

    start_ball_x = tank._x 48*Math.cos(angle*Math.PI/180);

    Is this declaring that tank._x 48 is the hypotenuse if I were to refer to trigo formula -

    cos (angle in degrees) = adjacent / hypotenuse OR better written as,

    adjacent = hypotenuse * cos(angle in degrees) which is same format as,

    start_ball_x = tank._x 48*Math.cos(angle*Math.PI/180);

    So can I say tank._x 48 is like the hypotenuse? If it is, why are we not using Pythagorean theorem?

    Another one -

    On line 31 we have:

    angle = tank.cannon._rotation-1;

    this angle is in degrees and why are we converting it back to radians in line 32? is it cos flash measures angle in radians and we are just reversing the process?

    start_ball_x = tank._x 48*Math.cos(angle*Math.PI/180);

    I hope someone can enlighten me on this! Sorry for my poor maths!

  20. dishcdiu on May 22nd, 2007 3:04 am

    this is for elmer i had that action script before heres the problem

    onClipEvent(enterframe) {
    if (this.hitTest( _root.cannonball) ){
    _root.gotoAndPlay(2);
    }

    add the enter frame function

  21. person on May 22nd, 2007 4:03 am

    for some reason no matter what I do, the cannonball doesn’t fire, does anyone know what I should do?

  22. eblup on May 22nd, 2007 6:50 am

    hey i need help it wont let me open the sourse codes

  23. eblup on May 22nd, 2007 7:01 am

    plz help it keeps saying un expected file format

  24. Gagan on May 22nd, 2007 9:49 am

    Hi! I am having the same problem “Unexpected file format”. I am trying to open the .FLA file with Flash Mx. Please help

  25. Im here on May 22nd, 2007 7:20 pm

    how would you rotate the turret using left and right keys?

  26. eblup on May 23rd, 2007 5:37 am

    plz help me, help me plz.

  27. eblup on May 23rd, 2007 6:44 am

    i got it and now i need help agin lolol

    CAN YOU RE-MAKE THIS IN A MOVIE CLIP INSTEAD OF A FRAME

  28. George W. Bush on May 23rd, 2007 7:27 pm

    i disagree i think u can all u gotta do is switch the first actionscript with the next and there u go alright well i hope that solves your problems

  29. eblup on May 24th, 2007 9:13 am

    to get te cross hair to move with up and down you need to make it so when you press up it will use cannon angle in some trig let me explain…

    tank.cannon._rotation = rot;

    if key is down key up
    rot -= 6;

    crosshair_x = tank._x 75*Math.cos(rot*Math.PI/180)
    crosshair_y = tank._y 75*Math.sin(rot*Math.PI/180)

    their may be some errors but thats the best i can do while not working and just being lazy

  30. George W. Bush on May 24th, 2007 7:04 pm

    Hey guys, i just wanted to ask why are you guys deleting “King Kong’s” notes?I believe that we should all post our own oppinions and not to worry if it is going to get deleted.Well i hope you all listen to what i am saying and for you to stop deleting posts. Thank you

  31. Emanuele Feronato on May 24th, 2007 7:29 pm

    Because it’s you…

  32. David on May 25th, 2007 3:27 pm

    hey i love your tuts they are so cool best tut site on whole world but i was wondering how could you create a enemy so when the cannon ball hits it it dies? (enemy made by script like blah blah blah i)

    well thx for your tut reely COOL!

    cya

  33. Create a flash artillery game - step 2 at Emanuele Feronato on May 25th, 2007 5:17 pm

    [...] Welcome to the 2nd part of this tutorial. I recommend you to read the 1st part if you haven’t done it yet and we are ready to start. [...]

  34. Alec on May 25th, 2007 9:09 pm

    What is a movie clip linkage.

  35. Leon on May 26th, 2007 9:58 pm

    Great tutorial dude I’m making a similar game, but you move the turret, and it goes around 360 degrees. But anyway, i was trying to get the spawn point working (where it duplicates) and I tried everything, and I wasforgot to conv sure I haert it to radians, and you reminded me. But I had no clue about how to make it move, but I figured it out from your code,, but because it moves 360 degrees, I had to edit it slightly, so I just changed it to:

    angle = this.Turret.Turret_rotatable._rotation-90

    Instead of minus 1, so thanks, keep it up! I can’t wait for the next tutorial. BY the way, could you help me with something? I have attached an “enemy” Movie clip which moves towards the player slowly, but as both the enemy and the bullets were created dynamically, I can’t really figure out how to do it. I think that a for loop is probably the bast way, but I don’t know how to tackle it myself. If I figure it out before you respond, I will post up how I did it. ANyway, thanks again for the help from your tutorial! It’s really good work! I’ve been following the line rider one, please will you do how to do a bike or car. I don’t need the line drawing part, it’s just for a different game I’m working on.

  36. And Mar on May 27th, 2007 4:56 am

    @Leon

    In my game I had the same problem. What I did is I added an onEnterFrame function for each bullet I shot. In each event, it looped through an array of all enemies on stage.

    attachMovie(”bullet”, “bullet” i, _root.getNextHighestDepth());
    i ;
    _root["bullet" i].onEnterFrame = function() {
    for(j=0 ; j

  37. And Mar on May 27th, 2007 5:00 am

    thats weird. it chopped my post in half. anyway…

    @leon

    _root[”bullet” i].onEnterFrame = function() {
    for(j=0 ; j

  38. And Mar on May 27th, 2007 5:03 am

    dang
    Manny you got a bug with you post system. I’m guessing with less than or greater than signs

    @Leon

    _root[”bullet” i].onEnterFrame = function() {
    for(j=0 ; j < enemies.length ; j ){
    //check for collision between this bullet and the current enemy
    //e.g. if(this.hitTest(enemies[j])
    //if true then remove both
    }
    }

    each time you dynamically create an enemy, you must push it onto the array, “enemies”.

  39. And Mar on May 27th, 2007 5:05 am

    srry again

    for(j=0 ; j < enemies.length ; j ){

    is supposed to be

    for(j=0 ; j < enemies.length ; j ){

    my bad :D

  40. And Mar on May 27th, 2007 5:10 am

    omg!!

    I hate this!!

    for(j=0 ; j < enemies.length ; j ){

    >>>>

    for(j=0 ; j < enemies.length ; j *two plus signs here* ){

  41. Leon on May 27th, 2007 8:45 pm

    dont worry, i get it, thanks!

  42. togie on May 30th, 2007 4:14 am

    hey … dunno if its just me but im having some troubles with the cannonball … i have made the cannonball and named it “cannonball” following the script … and it will not #1 connect to the cannon, #2 shoot the cannonball…. not shur … thx in advanced (mx flash)(obiusly :P)

  43. person on June 1st, 2007 11:09 pm

    I have the same problem as togie, someone help

  44. person on June 1st, 2007 11:09 pm

    I’m having the same problem togie

  45. wawa on June 4th, 2007 2:13 pm

    you have to fix the linkage for the cannonball, right click it in the library and select “linkage”, there mark the “export for actionscript” or something box. After that type “cannonball” in the “identifier” box.

  46. person on June 4th, 2007 6:52 pm

    now it works, thanks wawa

  47. Kaimie on June 5th, 2007 9:23 pm

    can anyone tell me how do I make the enemies move toward the tank? also how do I make the cannon balls bounce off the walls???

  48. A guy that needs help on June 8th, 2007 3:13 pm

    ok I have a problem, I type the code for the crosshair and nothing happens in preview. all that happens is my real mouse disappears.

    Plz help

  49. cody Anderson on June 13th, 2007 8:00 pm

    i followed the instructions but it only shoots one cannonball at a time and if i click 2 times my first cannon ball dissipears

    help!!

  50. hayden on June 14th, 2007 8:06 pm

    I have a question
    Games just like this have like upgrades which change things and add new units etc
    how would that be in script?

    and btw this sites tutorials are awesome,a big help ty!

  51. flaming on June 15th, 2007 12:44 am

    whenever i type the script, it doesn’t work. but when i copy and paste,even when its the same, it somehow works.

    Am i doing something worng?

  52. Cj on June 15th, 2007 7:31 pm

    Um Hey i’m just wondering how you get to those places where you type all that stuff down. I’ve never done this.

  53. anonymous on June 18th, 2007 2:58 am

    How come my action script has errors, even though I copied yours

  54. Leon on June 19th, 2007 1:07 am

    “I created a new movieclip linkaged as “tank” and inside this movieclip I created another one instanced as “cannon”. Look at the picture to understand how I placed the movieclips.”

    I am really new so forgive me being a total noob.

    I don’t fully understand what I am to do here. I created the two movieclips and placed them in the tank shape position. I even set two linkages. From what i understand we set a linkage so that they will be available without having to be on the stage?? The tank body moves to the stage with the attachMovie(”tank”,”tank”,2,{_x:230, _y:350}); but the cannon is missing. I guess what I am really after is what/how do i do “and inside this movieclip” from the above text?

  55. tyler on June 23rd, 2007 4:56 pm

    hello, I’ve been looking at this and yes It’s good, almost too good. Anyways, I was wondering on line 34 how It supose to duplicate the bullets or somthing. So it shot more then 1. Problem It keeps shooting 1 bullet and If I click again the bullet comes back and restarts. Also I’m using Macromedia Flash Mx 5. Should I get 8? Also If you want an Idea Creator of this tutorial you should get out there and design games that way you can work your way up there and make big money.

    If you ever need a partner I’m availibly for 2-D kinda 3-D designing I can make sprites, Backgrounds, Objects, and etc(depends). Email me at tjmoore_1993@hotmail.com

  56. On the horizon #1 at Emanuele Feronato on June 24th, 2007 2:04 am

    [...] Next part of the artillery game tutorial [...]

  57. s0daplayer on June 28th, 2007 8:12 am

    Tyler, You may want to create the bullets on different depths. Like:

    depthholder = 1
    attachMovie(”bullet”, “bullet” depthholder, depthholder”)
    depthholder

    you may want to do something like this instead.

  58. Saad on July 1st, 2007 8:22 pm

    for:

    start_ball_x = tank._x 48*Math.cos(angle*Math.PI/180);
    start_ball_y = tank._y 48*Math.sin(angle*Math.PI/180);

    if we get the xpos, don’t we go

    adj = hypotheneuse*cos(angle)?

    why is the code using the x pos of the main object rather than hypotheneuse?

  59. alex on July 2nd, 2007 9:16 am

    hey i can’t get the second bit of code to work are u some how ment to make the tank and the cannon in the same movieclip but with different instances or wat?

    plz help some 1!?!

  60. bontif on July 3rd, 2007 6:57 am

    I came upon this site while googling “geometry of war”.I am a Maths teacher and I was looking for material I could use for a project based Maths grade 9/10 lesson to be done in groups of 3 or 4 over a couple of weeks. The Artillery game with all its trig fits the bill.

    Except that I know nothing about Flash programming although I cannot see this being a problem with my students- they pick up things very quickly and run with it. Since you guys are very good at what you do (that’s obvious from the games you have posted)and are probably closer in mindset to my students,I’d like your advice.

    If you were the teacher, what steps (1,2,3…)would you go through with the class before they got down to the project. You can safely assume that there are students who know something about Flash and can share their knowledge- one of the main ojectives of this exercise is collaoration anyway.The students have the necessary Trig background already.

    Thanks for any suggestions you might have.

  61. Bj on July 4th, 2007 3:01 am

    The second part of the code does not work for me.I cant see why not,and i have the same setup.If anyone knows why it doesent work,pleasetell me.Also im using Flash 8 Pro,So if thats the problem please tell me

  62. Saad on July 4th, 2007 9:30 pm

    Mr. bontif since your a math teacher maby you could help me with this problem I have:
    “start_ball_x = tank._x 48*Math.cos(angle*Math.PI/180);
    start_ball_y = tank._y 48*Math.sin(angle*Math.PI/180);

    if we get the xpos, don’t we go

    adj = hypotheneuse*cos(angle)?

    why is the code using the x pos of the main object rather than hypotheneuse?

  63. destructin on July 22nd, 2007 9:26 pm

    please help!!!

    **Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 63: Statement must appear within on/onClipEvent handler
    Mouse.hide();

    **Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 64: Statement must appear within on/onClipEvent handler
    attachMovie(”crosshair”, “crosshair”, 1);

    **Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 65: Statement must appear within on/onClipEvent handler
    attachMovie(”tank”, “tank”, 2, {_x:230, _y:350});

    **Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 66: Statement must appear within on/onClipEvent handler
    crosshair.onEnterFrame = function() {

    **Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 70: Statement must appear within on/onClipEvent handler
    tank.onEnterFrame = function() {

    Total ActionScript Errors: 5 Reported Errors: 5

  64. Jake on July 24th, 2007 12:54 am

    i know how to do that but if you have a tutorial for making an enemy in that sort of game can you e-mail me the url?

  65. Jake on August 1st, 2007 8:43 pm

    hi im kinda new to reading these tutorials and i was wondering if you had a tutorial to make a game like Smashing http://www.miniclip.com/games/smashing/en/ i read how you made the angle calculator on the line rider tutorial but i really dont know where to start. if you have a tutorial like that can you email it to me or can you make one please?

  66. Andy on August 14th, 2007 3:36 pm

    Great tute, i really learned loads from it.

  67. dudue on September 13th, 2007 7:26 am

    everythings awesoem except the cannon ball which won’t appear or fire! lol y?

    Plz help
    wb

  68. endor on September 20th, 2007 2:10 pm

    Your tutorials look great, but I have one problem, I try to use ActionScript 3.0
    Anyone here that uses Actionscript 3.0 that can help me to get started? They have removed the AttacheClip function, so I don’t know where to start.

  69. endor on September 20th, 2007 3:09 pm

    I have now figured it out.
    To get the Crosshair to work in CS/AS 3.0 you can write this code:

    import flash.events.MouseEvent;

    Mouse.hide();
    var _crosshair:Crosshair= new Crosshair();
    stage.addEventListener(MouseEvent.MOUSE_MOVE, crosshairMove);
    function crosshairMove(ev:MouseEvent) {
    _crosshair.x = ev.stageX;
    _crosshair.y = ev.stageY;
    }

    addChild(_crosshair);

    You also need to rigth-click the MoveiClip in the Library and select Properties. Here you make sure the Export for ActionScript checkbox is checked, and give it a name.

    Hope this helpe you get started in AS/CS 3.0

  70. whatitdo on September 26th, 2007 4:27 am

    awsome tutorial, thanks

  71. Paul on October 17th, 2007 5:39 am

    This may sound real amateur-ish…But I’m using this code as an example, and I want to start a bullet from the beginning of the gun, which is a symbol inside of symbol…only thing is, where i set the start x and y locations to that of the gun, the bullets only come from the top of the screen where the gun seems to have started off, and it never appears on the gun location itself.
    I’m in a jam, can someone help me with this?

  72. Artillery with bounce: a modification of Artillery tutorial : Emanuele Feronato - italian geek and PROgrammer on October 29th, 2007 5:10 pm

    [...] received an email from Massimo M. - an italian reader - that modified the code explained in Create a flash artillery game - step 1 [...]

  73. johanes on October 31st, 2007 7:05 pm

    great tutorial… now i will began to create one…

  74. confused teenager on November 16th, 2007 6:53 pm

    i have my crosshair on 2 scene.. one for the actual game and one for the menu.. i cant seem to get my crosshair to follow the mouse in my second scene.. what do i do?

  75. god on November 19th, 2007 2:43 pm

    i managed to make a crosshair but i didn’t get the cannon n tank bits….i need help with step 2.

  76. Izzy on December 3rd, 2007 12:54 am

    seriously if you don’t know that yet you should skip ahead to things like this…the code to do that is extremely simple. You make yourself look like you just got flash and you already want to make games

  77. noob on December 4th, 2007 4:54 am

    lol This is action script 2.0!!!?? I thought it was AS3.0 Nice tutorial!!! Doesn’t work well in AS3.0 compiler for those who are having same problem as me… i dnt think… 8P

  78. The RoosterRanger on December 5th, 2007 11:21 pm

    Paul: to do what you wish to accomplish, you need to know a little about trigonometry. It has something to do with sine and cosine and you use them to achieve a coordinate. I haven’t done it in a long time, so I can’t tell u exactly. Try googling it.