Create your own graphics, background, and bullets. Points here are for original, excellent graphics.
Once you have done that, we can start on the ActionScript. First is the player:
onClipEvent(load){ var framecount = 0; var int1 = 0; }
All that does is initialise two variable: framecount and int1; we shall come to them now:
onClipEvent(enterFrame){ framecount++;
Create a basic "timer". We can use it to create a new enemy every 40 frames like this:
if((framecount / 40) == Math.round(framecount / 40)){ duplicatemovieclip(_root.mine,"mine" + int1,_root.getNextHighestDepth()) ; int1++ }
We have seen the duplicateMovie Clip() before, but the line before it? If you think through what that is saying, you realise the if will only get executed once every 40 frames - 24/40 for instance does not make a whole number. That is the importance of the framecount variable. Next comes the movement section, again what we have seen before:
if (Key.isDown(Key.RIGHT)) { if(!this.hitTest(this._parent.right)){ this._x += 4; } } if (Key.isDown(Key.LEFT)) { if(!this.hitTest(this._parent.left)){ this._x -= 4; } } }
The ! mark makes it negative, if NOT hitTest. Hence, if you are not touching the left barrier, you can move left, and the same with the right. Following? If you need refreshing, look back over past tutorials. That is it for the player - the final tailing } is the end of that Movie Clip event. Now, after a short pause, make a quick change to the actions for the frames: add to frame 1:
var notallowedtoduplicate = false;
And to frame 2:
notallowedtoduplicate = true }
All that does is make a variable true in frame 1 and false in frame , so we know where we are. Now we move on to the enemy's coding:
onClipEvent (load) { this._y = -50; this._x = random(550); }
Well, when the mine/enemy/falling thingy is created it moves off to the top of the screen, where it cannot be seen. It then moves to a random x location along the top of the screen, ready to fall down like a snow flake:
onClipEvent (enterFrame) { this._y += 3;
Every frame, every "snowflake" will move down 3 pixels. Not too hard. Now comes the if statements:
if (this._y > 420){ this.removemovieclip(); }
"If(I[the "snowflake"] am off the bottom of the screen) then remove me" - so it doesn't clog up memory. Okay it won't take up much, but better nothing than anything. Remeber the frame code that asked whether we were in frame two or not? Here is its use:
if(_root.notallowedtoduplicate==true){ this.removemovieclip(); }
By the way, the "==true" is unnecessary by default - that is what Flash assumes. Basically that three lines means that if we are in frame 2, destroy all the "snowflakes". Or rather they will all self-destruct. Of course, these bombs falling from the sky need to do something - in this case, it's game over when they touch you:
if (this.hitTest(this._parent.player)) { _root.gotoAndStop(2); } }
Oh yes, frame 2. This will be the game over screen. Add some text, "Game Over" or whatever, and a retry button, with the simple ActionScript:
on(press){ _root.gotoAndStop(1); }
Back to frame one, to start the game again.
Now we are going to add the ability to fire.
That wasn't so much, was it? Now we can start on the ActionScript. Let's think through the firing process. First comes the "trigger", which is triggered in the player Movie Clip, for simplicity's sake. Add at the bottom:
on(keyPress "<space>") { duplicateMovieClip(this._parent.bulletfirst, "bullet"+_root.int1, _root.getNextHighestDepth()); }
You don't have to change much to interpret that:
on(space is pressed down) { duplicate a new bullet }
That creates the bullet. We hit a problem soon, and I will attempt to explain it before it happens: you cannot remove the first/head Movie Clip, from which you duplicate others. My personal response to this is to have a different name for the first one than they will be generated to - the first mine is called "mine", but all the others have numbers on the end; the first bullet is "bulletfirst", all the others are "bullet1" etc. This can be used to keep the first one which cannot be removed out of play, off stageThe _name property carries the Movie Clip's name. The bullet's ActionScript should comprise of two parts like this:
onClipEvent (load) { if(this._name != "bulletfirst"){ this._x = _root.player._x; this._y = 310.1 } }
If the name of the bullet is not the first, then move to the location of the player, and to just above it (you might want to change that one), ready to be lauched:
onClipEvent (enterFrame) { if(this._name != "bulletfirst"){ this._y -= 10; if (this._y < - 20){ this.removeMovieClip(); } } }
Again, if it not the first bullet, then move upwards (-10 per frame), but once it get off stage at the top (-20 should be enough), remove it. And so we continue with our gun story: it has been fired, but now it needs to hit something - in this case a snow flake. So go onto the snow flake and change "this._y += 3;" to:
if(this._name != "mine"){ this._y += 3; }
So the first one doesn't move, otherwise it would be invincible when hit, as it could not be removed. The first major syntax/language comes now - a for loop. Make your for loop (and put it within the onClipEvent(enterFrame) section):
for (var i = 0; i<=_root.int1; i++) { if (this.hitTest(_root["bullet"+i])) { this.removeMovieClip(); } }
The hittest now takes a hittest of all the bullets bullet1, bullet2 and such, using the [ ] square brackets to say that the text coming up is actually the name of an object we want to refer to. So if, while looping through hittesting all the bullets, it gets a hit, then it removes itself.