Basic Of Object Movement

BasicOfObjectMovement100x100In this tutorial we are going to learn the basic of object movement. You can use arrow keys to drive ship in four directions up, down, left and right

 

Step 1: First we need to find a background and an object to perform with. You can use the images below:+ Background:

sea

 

+ Ship:

cruise_ship_cartoon.gif

Step 2: Download these two images and import them to the Library by going to File>Import>Import to Library and select them. Press Ctrl-J to resize your document to fit with your background and set the frame rate to 24 to make the movement smoother.

BasicOfObjectMovement1a

Press Ctrl-L to see your images in the library, place them on your document on separate layers

BasicOfObjectMovement1

Step 3: Click on the ship on your document to select it and press F8 to convert it into a movie clip. In the Convert to Symbol dialog, select Movie clip type and name it mc_ship

 BasicOfObjectMovement2

Step 4: Press Ctrl-F3 to open up the Properties dialog, in the Instance name input type in “ship”

BasicOfObjectMovement3

Step 5: Click on the ship again to make sure it is selected. Press F9 to open up the Action panel, type in these lines:

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

Press Ctrl-Enter to test it, use the arrow keys to move your ship around. Now let’s see how it works:

The onClipEvent(enterFrame) event will trigger its code every time the flash player enters this frame.

The if statement checks if there is any arrow key is being pressing. If LEFT or RIGHT arrow key is being pressing, the code will move our ship to the left or right by increasing its _x property (left: decrease _x; right: increase _x).  Same thing with UP or DOWN arrow keys with _y property.

If you want your program can recognise two simutaneously pressed keys then remove “else”  word.

 

It’s all with the movement. Let’s have a look at the result:

 

If you want your ship move smoother then try to increase the frame rate a bit!