Blog

Kenney
Spaceship Tutorial Part 1: Moving it
February 17th, 2010

So you would like to know how to make a game? Well, I can teach you but not in one sitting, that’s why this is part 1 of the Spaceship Tutorial.

Spaceship

Once you’re finished with these tutorials you’re gonna have a full Flash game (ActionScript 2.0, I’m old fashioned :P ), but there’s a lot to be learned so let’s get started!

Step 1. Preparation

First create a new document (CTRL + N, select Flash Document) and change the frame rate to 45 in the properties pane, this will make sure the game will run nice and smooth.

Step 2. Drawing the ship

First things first we’re going to need the spaceship to move, draw one yourself or use mine (download below).

Ship (drawn)

Once you’ve drawn your spaceship, select it and hit F8. In the new window tick MovieClip and press OK. Now your ship is a MovieClip, what that means is that it’s going to be interactive or animated.

MovieClip

Select the MovieClip and hit F9, that’s going to open the code screen where you can punch in some code that the spaceship is gonna execute.

Step 3. Making it move

Enter this code in the code window:

1
2
3
4
5
6
7
onClipEvent(enterFrame){
	if(Key.isDown(Key.LEFT)){
		_x -= 6;
	}else if(Key.isDown(Key.RIGHT)){
		_x += 6;
	}
}

Now the explanation of the code:

onClipEvent(enterFrame){

This line tells the code that the following actions have to constantly be executed.

if(Key.isDown(Key.LEFT)){

Now it checks if the left arrow key is held down, if so, then it executes the following line:

_x -= 6;

This tells the spaceship to subtract 6 pixels from it’s X axis.

}else if(Key.isDown(Key.RIGHT)){

This makes sure the left arrow key isn’t held and checks if the right arrow key is held, if so, it executes the next line:

_x += 6;

This adds 6 pixels to the X axis of the spaceship. And last but not least we close all open tags:

}
}

Step 4. Testing and finishing

Press CTRL + ENTER to test your game, now you should be able to move the spaceship left and right with the corrosponding keys! :) If you had problems feel free to ask questions below, if you want a sample download the source file here:

Source
Source

Next up we’re gonna make the spaceship shoot some lasers! Click here to read part 2!

comment
Replies

B4rtj4h says:

Very Cool :D !
Kenney is the best

Anonymous says:

Awesome ur great!!

Jc says:

We need Part 2 :D

Kenney says:

Working on it! :)

zeus ganlapo says:

send me some of your samples on my email
panglaoa@yahoo.com
ThANk YoU

dedi says:

great.. but how my plane did not come out the stage?
please help me.

Tileon says:

Thanks, this was perfect.

Tileon says:

Thanks this is perfect for starting out.

Serkan says:

How do you get a flash document? Thanks if you reply

John says:

I got this message;

1087: Syntax error: extra characters found after end of program.

What am I doing wrong?

thanks

Kenney says:

@Serkan
How do you get a Flash document? Well you create a new one by pressing CTRL + N if you’re on Windows.

@John
That actually looks like an ActionScript 3 error, make sure you’re coding in ActionScript 2.

adam says:

i went to ActionScript 2 but i got 1087: syntax error: extra charactars found after end of program on onClipEvent(enterFrame){

Kenney says:

@adam
That is absolutely an ActionScript 3 error, make sure your project gets published in ActionScript 2 format (it’s under publish settings).

sophie says:

when i tried to render my ship to test my game it said that it was only permitted for movie clip instances, but i made my ship a variable

Hi says:

This is great, because of your reasoning for each line, I worked out to make the ship go up and down too.

Kenney says:

Yeah, some tutorial just throw a block at code at you and are like “figure it out yourself”. I hate that.

gerald says:

wow!! I never thought the capabilities of this software.
thanks for the tutorial anyway.

shishir says:

onClipEvent(enterFrame){
if(Key.isDown(Key.LEFT)){
_x -= 6;
}else if(Key.isDown(Key.RIGHT)){
_x += 6;
}
if(Key.isDown(Key.UP)){
_y -= 6;
}else if(Key.isDown(Key.DOWN)){
_y += 6;
}
}

//this is the entire code to move the space ship in ne direction.

shishir says:

onClipEvent(enterFrame){
if(Key.isDown(Key.LEFT)){
_x -= 6;
}else if(Key.isDown(Key.RIGHT)){
_x += 6;
}
if(Key.isDown(Key.UP)){
_y -= 6;
}else if(Key.isDown(Key.DOWN)){
_y += 6;
}
}

//this is the code to move the space craft in ne direcction

gerald says:

does macromedia flash 8 has a actionscript 2 on it

Kenney says:

Yes, Macromedia Flash 8 uses ActionScript 2.

Kasey says:

Great tutorials how do you make it so the ship can’t go over the side. Cheers

ZedMaster says:

Im a visual lerner and i didnt understand any of this =( i need a video



Leave a Reply