Create a flash artillery game – step 2
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.
Fixing bullets amount
In the last example, when you shot a bullet, it "lives" forever. I mean, once the bullet movieclip is on the stage, there isn't any routine to check if the bullet should exist or not.
Try to imagine thousands of bullets that continue falling down at increasing speed. Obviously, if you fire a large amount of bullets, the movieclip starts lagging.
We need to remove bullets when they are off the stage.
-
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/30;
-
this._y += this.diry/30;
-
if ((this._x<0) or (this._x>500) or (this._y>350)) {
-
this.removeMovieClip();
-
}
-
};
-
}
Lines 42-44: Check if the bullet if out of visible left, right and bottom area (I did not include the top because a bullet is supposed to fall down...) and if true, removes the bullet movieclip.
In this case, the only movieclips active are those visible on stage plus the ones falling from the upper edge of the stage.
Maximum bullets at the same time
Now we need to put a limit to the maximum bullets fired at the same time. It would be too easy if we let the player fire a large amount of bullets. So we are going to put a limit.
-
Mouse.hide();
-
gravity = 2;
-
fired = 0;
-
max_firepower = 3;
-
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() {
-
if (fired<max_firepower) {
-
fired++;
-
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/30;
-
this._y += this.diry/30;
-
if (this._y>350) {
-
this.removeMovieClip();
-
fired--;
-
}
-
};
-
}
-
}
Line 3: Variable designed to count the bullets currently on stage
Line 4: Maximum firepower allowed. Changing this value you will change the maximum bullets allowed to be shot at the same time. Changing this value will affect gameplay.
Line 34: When the player wants to fire, I check if the bullets on stage are minor than the maximum bullets allowrd
Line 35: if true, increases the fired variable
Line 48: decreases the fired variable when a bullet is removed. If you look at line 46, you will see the control is performed only on bullets leaving the stage to the bottom. This will always happen even for bullets thare are leaving to left or right, since when they fall, sooner or later they will leave to the bottom. This will make the game a little bit harder, because if you fire a bullet out to the right, you will have to wait for it to leave the stage to the bottom to have the bullet reloaded.
Now it's time to introduce the other great star in an artillery game
The ground
I created a new movieclip linkaged as ground that represents... well... the ground.
-
Mouse.hide();
-
gravity = 2;
-
fired = 0;
-
max_firepower = 3;
-
attachMovie("crosshair", "crosshair", 1);
-
attachMovie("tank", "tank", 2, {_x:295, _y:255});
-
attachMovie("ground", "ground", 3, {_x:0, _y:200});
-
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() {
-
if (fired<max_firepower) {
-
fired++;
-
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/30;
-
this._y += this.diry/30;
-
if ((this._y>350) or (ground.hitTest(this._x, this._y, true))) {
-
this.removeMovieClip();
-
fired--;
-
}
-
};
-
}
-
}
Lines 6-7: Tank and ground are placed on the stage as if the tank were on the ground (of course...)
Line 47: the test to remove the bullet is extended to its collision with the ground. At the moment, I do not care about explosions or accurate hit test
The enemy
In an artillery game, you are supposed to shot to an enemy... so I linkaged another movieclip as enemy... it should be that red square (you know, red squares are evil).
-
Mouse.hide();
-
gravity = 2;
-
fired = 0;
-
max_firepower = 3;
-
place_enemy();
-
attachMovie("crosshair", "crosshair", 1);
-
attachMovie("tank", "tank", 2, {_x:295, _y:255});
-
attachMovie("ground", "ground", 3, {_x:0, _y:200});
-
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() {
-
if (fired<max_firepower) {
-
fired++;
-
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/30;
-
this._y += this.diry/30;
-
if ((this._y>350) or (ground.hitTest(this._x, this._y, true))) {
-
this.removeMovieClip();
-
fired--;
-
}
-
};
-
}
-
}
-
function place_enemy() {
-
enemy_placed = attachMovie("enemy", "enemy", _root.getNextHighestDepth(), {_x:0, _y:350});
-
enemy_placed.yspeed = 0;
-
enemy_placed.onEnterFrame = function() {
-
this.yspeed += gravity/10;
-
this._x++;
-
while (_root.ground.hitTest(this._x+this._width/2, this._y+this._height, true)) {
-
this._y--;
-
this.yspeed = 0;
-
}
-
if (!_root.ground.hitTest(this._x+this._width/2, this._y+this._height+1, true)) {
-
this._y += this.yspeed;
-
} else {
-
this.yspeed = 0;
-
}
-
if (this._x>500) {
-
this.removeMovieClip();
-
place_enemy();
-
}
-
};
-
}
Line 5: call to a place_enemy function that will... uh... something like placing an enemy...
Line 55: here starts the function place_enemy
Line 56: Placing an enemy in the bottom left of the stage
Line 57: Init enemy yspeed at zero. It's obvious that I won't move the enemy with a motion guide but with pure actionscript, just in case tomorrow I would like to change the terrain. I am using some of the basics explained in the tutorial called Create a flash draw game like Line Rider or others - part 5 so give it a look if you haven't done it yet.
Line 58: Function to be executed every time the enemy enters in a frame.
Lines 59-69: As said, these are lines already explained in the the tutorial called Create a flash draw game like Line Rider or others - part 5
Lines 70-73: When the enemy reaches the right border, I remove it and call the function again, creating another enemy.
And we have our walking enemy... want to hurt it?
The killable enemy
In this tutorial I won't cover energy, health and so on, but I will during next tuts. At the moment, when a bullet hits an enemy, he/it dies and repops in the starting point.
-
Mouse.hide();
-
gravity = 2;
-
fired = 0;
-
max_firepower = 3;
-
place_enemy();
-
attachMovie("crosshair", "crosshair", 1);
-
attachMovie("tank", "tank", 2, {_x:295, _y:255});
-
attachMovie("ground", "ground", 3, {_x:0, _y:200});
-
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() {
-
if (fired<max_firepower) {
-
fired++;
-
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/30;
-
this._y += this.diry/30;
-
if ((this._y>350) or (ground.hitTest(this._x, this._y, true))) {
-
this.removeMovieClip();
-
fired--;
-
}
-
if (enemy.hitTest(this._x, this._y, true)) {
-
this.removeMovieClip();
-
enemy.removeMovieClip();
-
fired--;
-
place_enemy();
-
}
-
};
-
}
-
}
-
function place_enemy() {
-
enemy_placed = attachMovie("enemy", "enemy", _root.getNextHighestDepth(), {_x:0, _y:350});
-
enemy_placed.yspeed = 0;
-
enemy_placed.onEnterFrame = function() {
-
this.yspeed += gravity/10;
-
this._x++;
-
while (_root.ground.hitTest(this._x+this._width/2, this._y+this._height, true)) {
-
this._y--;
-
this.yspeed = 0;
-
}
-
if (!_root.ground.hitTest(this._x+this._width/2, this._y+this._height+1, true)) {
-
this._y += this.yspeed;
-
} else {
-
this.yspeed = 0;
-
}
-
if (this._x>500) {
-
this.removeMovieClip();
-
place_enemy();
-
}
-
};
-
}
Lines 52-57: if a bullet hits THE enemy (I will explain that bold "the" later), I remove both the bullet (decreasing the fired variable) and the enemy and place another enemy.
It's very simple, but I wrote that "the" because I know that A bullet on the stage can hit only THE only enemy on the stage.
When we'll have lots of bullet that can hit lots of targets, it gets a bit more complicated.
That's what I'll cover during next step, coming soon.
Meanwhile, download the sources and give me feedback.
They can be easily customized to meet the unique requirements of your project.
32 Responses to “Create a flash artillery game – step 2”
Leave a Reply
Trackbacks
-
Create a flash artillery game - step 1 at Emanuele Feronato on
May 25th, 2007 5:19 pm
[...] May 25th update: 2nd part released [...]
- Arch Games Webmasters - Arcade Webmasters » Game Developer tutorial List on May 13th, 2008 7:32 pm
-
Como criar jogos online (em flash)? on
April 11th, 2009 10:20 pm
[...] 11. Jogo de artilharia [...]
-
Create a flash artillery game – step 2 | Tutorial Collection on
July 1st, 2009 3:24 am
[...] View Tutorial No Comment var addthis_pub="izwan00"; BOOKMARK This entry was posted on Wednesday, July 1st, 2009 at 6:54 am and is filed under Adobe Flash Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]
Posts
- Rick Triqui: my first PlayCrafter game
- Prototype of a Flash game like Meeblings
- Games for the game developers!
- The art of debugging
- How to embed a text file in Flash
- Create a Flash game in minutes with PlayCrafter
- Upgrade your Flash CS4 to 10.0.2
- Play Mazeroll, my latest Box2D game
- Triqui MochiAds Arcade plugin for WordPress Released!!
- The MochiAds funnel
- Flash game creation tutorial - part 1
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Create a flash draw game like Line Rider or others - part 1
- Create a Flash Racing Game Tutorial
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash artillery game - step 1
- Create a flash draw game like Line Rider or others - part 5
- Flash game creation tutorial – part 5.2




(4.9 out of 5) - Flash game creation tutorial – part 3




(4.86 out of 5) - Creation of a platform game with Flash – step 2




(4.84 out of 5) - Create a survival horror game in Flash tutorial – part 1




(4.82 out of 5) - Create a flash artillery game – step 1




(4.82 out of 5) - Create a Flash Racing Game Tutorial




(4.8 out of 5) - Create a flash artillery game – step 2




(4.75 out of 5) - New tile based platform engine – part 6 – ladders




(4.74 out of 5) - Flash game creation tutorial – part 2




(4.73 out of 5) - The experiment – one year later




(4.7 out of 5)


Wow, thats pretty good. Im making a Game where one of my teachers is throwing geography books at you and you have to shoot the geog books and at the end kill him xD
Im using 2 of your tutorials and when Im finished ill show you the final thing xD
Hey I made a game using some of your tutes. Its not very good but its my first.
http://andmargames.bravehost.com/jetpack.html
veary good well don iam kinda con fused on how to wright action script in frames instead of movie clips.
To write actionscript in frames, just click on the keyframe and open the actions panel.
hey
love yout tuts i was wondering how could you instead of using mouse click to fire you press space bar? plz someone tell me how thx
:)
I can only shoot one bullet at a time and when I shoot the enemy suddenly disappears, does anyone know how to fix this? thanks
how do you add more enemys????
Hey great tutes it’s really hard to find good ones with good English and not too much detail. Look forward to more.
help me to learn to make flash game
I’m making a game at the moment,
so when your player touches the mighty and powerfull enemy
you would go back to frame 1.
Please help ME!!!!!!!!!!
[In the player movieclip]
if(_root.mightyandpowerfulenemy.hitTest(this)){
_root.gotoAndPlay(1);
}
Nice tutorial you go there! Everything works, but it’s pretty hard to make the turret itself work correctly. Could you maybe make a defense game tutorial? Lots of people are asking for one, and I bet you can make one. ^^,
i couldn’t get the rotation limiter to work with a script with similar to this.
Please help!
how could i do it so the cannon and the tank moved with the arrow keys?
please help me,
i’m making a game like this, and when the cannonball hits the enemy the enemy explodes.
the actionscript:
if (enemy.hitTest(this._x, this._y, true)) {
_root.scoretext.text = deaths+ “Deaths!”;
exp_sound.start();
this.removeMovieClip();
_root.enemy.play();
fired–;
place_enemy();
}
but the game still acts like the enemy is on stage, how do i remove him after the frames are played??
Great tut very helpfull i added a score variable wich worked fine
then i drew a death sequence in enemy and i wrote this code in the hit test
ActionScript
if (enemy.hitTest(this._x, this._y, true)) {
_root.enemy.gotoAndPlay(2)
score ++; this.removeMovieClip();
enemy.removeMovieClip();
fired–;
place_enemy();
Heya, I’m making a shooter game that allows the player to shoot 10 times. If he shot 10 times I want the game to end. I tried it with your tut but it did not work. In your tut you can have 3 shots at the stage max. But when they are gone you can shoot another 3 times.
So my question is:
How can flash make my player shoot 10 times max., with only one bullet on screen at the time?
Thank you so much,
Ruby
Hey, I know you probably get a lot of emails, and messages, but. I’ve looked all over and can’t figure out a way to do what I’m trying to do. I’m making a side scrolling shooter, where the character is in the center of the screen and as you move around you can traverse a map. I’m going to figure out all that later, but for now I need to find a way to have it so that my character’s arm and selected gun stay attached to the character but point toward the cursor location. I’ve tried dozens of methods and nothing works right. Thanks in advance.
=/ How do you combine the bouncing with getting rid of bullets that aren’t on the screen? No matter what I do to try and combine the two, I get an error.
@ Ruby
make a damned counter.
you did a.
(_root.blank.hitTest)
do this.
_root.blank.hitTest(_x,_y,true) {
hi, please help.
i added a dynamic score box edited the ground everything added clouds and all.
and it worked. but when i added the kongregate highscore api ,everything went wrong, now the score doesnt work it jus says _level0 or somtimes NaN please help.
This works well! But I see that the hit test in the cannon ball relies on there being one definite enemy instance with a known name.
How do you have it so there can be numerous enemies AND numerous cannonballs – where any enemy can collide with any cannonball?
Would you need to create an array on which you place each instance – then loop through it?
when will there be a second step?
Found the code on another website… http://www.foundation-flash.com/tutorials/turretpart1/
thank you
por lo que entendi me gusto muchho el tutorial. lastima que no lo puedo completa por que no entiendo mucho inglés.
when will the next one come out?