You can play this game, and others, at Maximized Games.
onClipEvent(enterFrame){
if (Key.isDown(Key.RIGHT)){
if (this._x<500){
this._x+=10;
}
}else if (Key.isDown(Key.LEFT)){
if (this._x>50){
this._x-=10;
}
}else if(Key.isDown(Key.UP)){
if (this._y>50){
this._y-=10;
}
}else if(Key.isDown(Key.DOWN)){
if (this._y<350){
this._y+=10;
}
}
}
onClipEvent(enterFrame){
if(this.hitTest(_root.spaceship)){
_root.gotoAndStop(2);
}
}
We could increase the length of our game by making another asteroid appear when the first one disappears off the screen.
The following code should be added to the instance of the asteroid to get this to happen. Notice the comments in the code that explains what each section does.
This actionscript uses the random number function which can be used to add an element of surprise to your games.
A user defined function ‘reset()’ is also used, as is the ‘load’ clip event.
onClipEvent(load){
// position the asteroid at a random position
function reset(){
this._x=random(400)+50;
this._y=-50;
}
reset();
}
onClipEvent(enterFrame){
// test for a collision
if(this.hitTest(_root.spaceship)){
_root.gotoAndStop(2);
}
// move the asteroid
this._y+=10;
/* check if asteroid is off screen
and if so, reset it */
if (this._y>450){
reset();
}
}
Insert a new Movie Clip symbol and call it ‘backdrop’. Draw a black rectangle and resize it to 550 x 400 pixels (the same size as your movie). Use the Brush tool to paint some white coloured stars and planets onto this backdrop.
Now copy the backdrop graphic and paste the copy onto the stage. Align the copy with the top of the original – creating a long backdrop 550px wide by 800px high. Select the whole backdrop, Edit – Select All, and set it’s X and Y position to 0,0 so that its reference point is in the top left hand corner.
Go back to the Main movie timeline and drag the backdrop from the Library into frame 1 of the layer you created earlier. Set the backdrop’s position to -400,0.
Add the following Actions to the backdrop object, which will cause it to scroll continuously (this is actually an illusion).
onClipEvent(enterFrame){
// move the backdrop 5 pixels down
this._y+=5;
// move the backdrop back up when its Y position is greater than zero
if(this._y>0){
this._y=-395;
}
}
Draw a Text box in the top right hand corner of the stage in the new layer and type the number 0 (zero) in it. Change the default font and size by selecting Window – Panels – Character. Choose Window – Panels –Text Options and select Dynamic Text. Unselect the ‘Selectable’ checkbox. Enter the word ‘score’ in the Variable text box.
We will increment the score every time an asteroid reaches the bottom of the screen. To do this, add the highlighted code below to your asteroid instances.
onClipEvent(load){
function reset(){
this._y=-50;
this._x=random(400)+50;
}
reset();
}
onClipEvent(enterFrame){
if(this.hitTest(_root.spaceship)){
_root.gotoAndStop(2);
}
this._y+=10;
if (this._y>450){
_root.score++;
reset();
}
}
You could also add an explosion to the ship when an asteroid strikes it. This could be achieved by adding extra frames to the spaceship movie clip which play back when the ship is struck.