The avoiding game tutorial series:
Part 1 Part 2 Part 3 Part 4 Part 5 Part 5b
Welcome to part the third of this series of tutorials on creating an avoiding game, I hope all is well so far with your games (remember, I reply to comments and emails if I can!) and we’re ready to advance the game slightly more. In this segment of the tutorial, I’ll show you how to control the number of enemies dynamically with a variable on the frame. Here’s what you should end up with at the end of this tutorial:
Dynamicisising (totally should be a word)
So, first of all I’ll show you how to make your enemies dynamic. That is, you can control the hordes with a numerical variable, this means you can make your game progressively harder, which in turn makes your game more challenging. And a challenge is more fun, right? It also adds variety, which is good to stop your game becoming boring.
Now, the thing to grasp here is that the enemies will be placed on the stage with Actionscript, and therefore you do not need them onstage to begin. So, the first step is to select your enemies and delete them. Then, hit CTRL+L to open the library (If it isn’t already open). Here you need to hunt down your enemy movie clip, right click it and hit Linkage. Then check ‘Export for Actionscript’ and type ‘enemy’ into your identifier field.
Then, open the actions for your frame and delete them (if you have been following the tutorial to the letter, if you have added extra things in be careful what you delete) and paste these actions in:
-
stop();
-
clock = 0;
-
lives = 3;
-
score = 0;
-
enemies = 4;
-
enemiesonstage=0;
-
timer = function () {
-
clock += 1;
-
};
-
onEnterFrame=function(){
-
if (enemiesonstage<enemies) {
-
var enemy:MovieClip = attachMovie("enemy", "enemy"+enemiesonstage, _root.getNextHighestDepth());
-
enemiesonstage += 1;
-
enemy._x = random(300);
-
enemy._y = random(-50);
-
enemy.speed = random(7)+1;
-
enemy.onEnterFrame = function() {
-
this._y += this.speed;
-
if (this.hit.hitTest(_root.player)) {
-
if (_root.lives>=1) {
-
this._x = random(300);
-
this._y = random(-50);
-
this.speed = random(7)+1;
-
_root.lives -= 1;
-
}
-
}
-
if (_root.lives<=0) {
-
_root.gotoAndStop(3);
-
this.removeMovieClip();
-
}
-
if (this._y>=310) {
-
this._x = random(300);
-
this._y = random(-50);
-
this.speed = random(7)+1;
-
}
-
};
-
}
-
}
In these actions, we’ve moved from coding on Movieclips to coding on the frame. This is much better practice, easier to see all of your code in one place, and will help you prepare for the frame/external files only strictness of AS3. In a later tutorial I’ll show you how to convert the majority of the actionscript in this tutorial to frame coding (apologies for not doing so in the first place, but it’s much easier to learn the other way). Now to explain it!
The variable we’ve added at the start, ‘enemies’, controls how many enemies are onstage. You can change this ingame (for example, using the timer to add one every 5 seconds) The ‘enemiesonstage’ variable tells the game how many are currently onstage so it can know when to add more.
Then we open the onEnterFrame function, this acts as the equivalent to onClipEvent(enterFrame). The if test inside it then checks if enemiesonstage is less than enemies. If it is, it adds an enemy to the stage.
Now we get to the good part, the attaching of the enemies to the stage. The first part in quotemarks is the linkage name we gave it earlier, and the next part is the new instance name, in this case it’s enemy plus the current value of enemiesonstage. This ensures each enemy has a unique instance name. The last part of this line defines the depth of the movie clip, set to next highest for ease of use.
Now we set the enemy’s actions to be run on it’s load. By setting the attached Movieclip as the variable enemy, we can add these easily.
Now we have to add the enterFrame actions for the enemy, these are the same as the actions that were on it before, except I increased the speed a little (adjust to preference) and all variables that are on the movieclip must have this. before them now, as unprefixed variables reference the root timeline.
Test your movie and see how it works!
Changing the variable ingame
Now I’ll show you how to set it up so every 5 seconds your game adds an extra enemy to the stage. To do this we’re simply going to setup another timing function that runs every 5 seconds.
Now, go to your player’s character and open it’s actions panel. This should be where you added the function to time the game. You merely have to create a second interval that will run a function every 5 seconds.
-
onClipEvent (load) {
-
_root.timeclock = setInterval(_root.timer, 1000);
-
_root.enemyclock = setInterval(_root.enemytimer, 5000);
-
}
Now we must go back to the frame actions to add the enemytimer function. This is extremely similar to the timer function.
-
enemytimer = function () {
-
enemies += 1;
-
};
Finally, we must clear the interval on gameover. So go to your gameover frame and open up it’s actions panel, where you’ll find where we cleared the timer interval last tutorial. We just have to add one in for enemyclock.
-
clearInterval(enemyclock);
And that should make your enemies increase every 5 seconds, test your movie and check it out!
That concludes this portion of the avoiding game tutorial, click here to grab the source. As always, if you have any problems, requests or suggestions post them in the comments below or email me at alternateaccount[AT]frozenhaddock.co.uk
Thanks for reading!



![[del.icio.us]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/digg.png)
![[Google]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/linkedin.png)
![[StumbleUpon]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Windows Live]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/email.png)


August 21st, 2008 at 1:23 pm
No offence but this tutorial kinda sucks. with this i was able to add 1 new thing to my game.
August 21st, 2008 at 7:42 pm
Yeah?
It’s only supposed to cover one thing, because said thing is far more complicated than the rest of the tutorial so far. Also, it’s quite a big thing for the game, increasing difficulty over time is a perfect way of balancing the difficulty of a game.
Also, this tutorial shows you -how- to do something. It doesn’t mean you can’t take that thing and use it to add much, much more. The timer function could be used to randomly place powerups, extra lives, bonus score etc. It could even be applied to the coin to increase the reward along with the difficulty.
You can’t expect a tutorial to drop it on your lap, already completed. You have to take the information gained, and run with it. Besides, you managed to add one new thing into your game? That’s one thing more than you had beforehand, right?
I suggest you take the time to read this article from Emanuele Feronato:
http://www.emanueleferonato.com/2008/06/04/how-to-use-a-flash-game-tutorial-to-make-your-own-game/
Thanks.
August 25th, 2008 at 12:11 pm
Well Tazzydevil. I think it’s great. Once I get back to my computer with my flash files, I’ll be able to try it out. Thanks!
August 25th, 2008 at 3:41 pm
Yeah i know but i was hoping for a section on power ups and i only have mx so i cant run this actionscript.
August 25th, 2008 at 9:57 pm
This should work in MX too, it’s just the download that won’t work.
August 26th, 2008 at 1:52 am
is there a way to create the amount of lives increases if the user passes a certain timeframe???? eg is the passes 50 seconds an extra life will be given???
August 31st, 2008 at 2:11 pm
TazzyDevil this tutorial rocks. I just have 1 question. I’m trying to make a game where you only COLLECT coins and not dodge smilies. Would just using the code from what we have made so far, only removing anything that has to do with enemies that you have to dodge, work? If not. Do tell. Not sure if this game has been done before but it could be amazing. I’m not gonna say my idea out here lol. People WILL steal it. I can use Email if needed.
Thanks,
Jmax
August 31st, 2008 at 2:15 pm
It should work, removing stuff that relates to the enemies should work :)
September 3rd, 2008 at 9:56 am
Ok thats good. Just realised it’s gonna be a LOT more complicated than that. I’ll have to wait till the final tutorial.
September 4th, 2008 at 9:47 pm
how do you get the source?
September 6th, 2008 at 2:09 pm
…click the link at the bottom of the post?
Or paste this into your browser:
http://www.frozenhaddock.co.uk/tutfiles/avoidingamept3.fla
September 9th, 2008 at 1:50 am
ya but how do i download it?
October 9th, 2008 at 9:45 am
@ taz , ive got a idea for a game like this, but i want the enemies to appear from all sides of the map
ideas? which part of the coding should be changed?
October 9th, 2008 at 7:21 pm
If you check Parts 4 and 5, I showed you how to do that :)
October 10th, 2008 at 7:18 pm
[...] Game Concepts, Games, Tutorials with 197 Comments The avoiding game tutorial series: Part 1 Part 2 Part 3 Part 4 Part 5 Part [...]
October 10th, 2008 at 7:19 pm
[...] Game Concepts, Games, Tutorials with 9 Comments The avoiding game tutorial series: Part 1 Part 2 Part 3 Part 4 Part 5 Part [...]
October 10th, 2008 at 11:00 pm
hey tazzy, nice tutorial, pretty awsome.
but i got a problem…
the the timer goes up on the gameover screen…
October 10th, 2008 at 11:35 pm
Add this line to the gameover frame:
clearInterval(timeclock);
:)
October 13th, 2008 at 12:01 pm
[...] is writing these quickly, Part 4, the latest in his AS2 to AS3 translation of the avoiding game tutorial series here at FrozenHaddock is now out, and ready to be followed. Go check it [...]
October 17th, 2008 at 7:14 pm
My enemies stay after after I die.
how do I fix that?
October 31st, 2008 at 8:33 pm
How do you add music to it and make the music loop?
November 2nd, 2008 at 12:43 pm
Hmm… maybe you could teach us how to make the enemies fall faster with time too? :P
November 2nd, 2008 at 2:05 pm
Well, I’ll keep that in mind for part 6 :P
But for now if you want to try it yourself, something like a variable that is always added to the random speed that is set at 0 to begin, then with the enemy number variables this one gets incremented too :)
November 2nd, 2008 at 2:08 pm
Riley:
That depends on your code I gues how does your code look?
Poisonsky:
1. you can add music with the attachSound function,
musicvar.attachSound(yournumber,99)
musicvar.play();
2. try this yourself it isn’t that hard, for help you could make a topic on the forum.
Brart
November 2nd, 2008 at 2:09 pm
damn tazzydevil same topic… same time
November 8th, 2008 at 7:47 pm
Is there any way to dynamicise (get more difficult and stuff) without deleting all the onstage enemies? I don’t feel very confident with using frame actions :(
November 8th, 2008 at 7:51 pm
So is there a code that I can just add straigh to the onstage enemy to add the dynamicising thingy?
November 9th, 2008 at 3:06 pm
Hey Taz here’s a suggestion: why not use this code:
onClipEvent (load) {
this._x = random(500)
this._y = random(-50)
dynamisis = 5
speed = random(dynamisis)+1
}
onClipEvent (enterFrame) {
this._y += speed
if (this.hitTest(_root.player)) {
_root.gotoAndStop(10)
}
if (this._y>=510) {
dynamisis = dynamisis +0.5
this._x = random(500)
this._y = random(-50)
speed = random(dynamisis)+1
}
}
Well, you can change dynamisis to a name of your choice but, with that in theory I suppose you could…. make the enemies fall progressively harder! :D
November 9th, 2008 at 3:08 pm
… just realised, sort of.
November 11th, 2008 at 7:59 pm
[...] 2007 in Flash, Games, Tutorials with 60 Comments The avoiding game tutorial series: Part 1 Part 2 Part 3 Part 4 Part 5 Part [...]
November 23rd, 2008 at 1:48 pm
I’ve changed my game so that is tells you how many enemies you’ve dodged but now that i’ve added the dynamicisism thing, it always adds 4 onto the end result. any help?
December 1st, 2008 at 2:13 am
um, i had tutorial one and two working fine, and then i added tutorial three to my game and it screwed everything up. Now no enemies fall and its just the player and a coin. I have no idea what i did wrong. and i tried downloading the source and it took me to this page with some random stuff on it, i couldnt download anything, but granted, im using safari.
also the first two tutorials were very easy to read and understand, they were great, however this one was very nague and that might be part of my problem. Could you please clear it up
December 1st, 2008 at 3:33 am
ok, i fixed the first problem, i spelled one of the variables wrong. but now my character wont die, it touches the enemy and stays alive and doesnt lose a life
December 1st, 2008 at 3:46 am
ok, i fixed all my problems, this was a great turorial. kudos to you, tazzydevil
just wondering though, what code would you need to put in to make a new object appear every 50 seconds,
December 3rd, 2008 at 8:49 pm
In my game i have levels and i want it to go to the next frame once i get a certain amount of coins. The buttons work fine but once i pass the level the enemies never go away in the next frames. This doesn’t happen in your game because you have
if (_root.lives<=0) {
_root.gotoAndStop(3);
this.removeMovieClip();
set on the enemies.
so do you know how i could make it so that once i collect my last coin it goes to the next frame and all the enemies go away.
December 3rd, 2008 at 9:55 pm
if(_root.coins>=NumberOfCoinsToWin){
_root.gotoAndStop(WinningFrameNumber);
this.removeMovieClip();
}
That should work nicely (Or something along those lines)
:)
December 8th, 2008 at 8:49 pm
thanks alot, that solved my problem