Hellow Guys, the next step we will be moving our Spaceship. We are gonna do 2 different kind of “moving”, the first is with the keyboard if you prefer to make a game for desktop and the second is a simple “touch” moving. You can read the previous tutorial HERE. Well… time to Begin.

I – Posicioning your Spaceship.

First of all let’s position our spaceship in our Screen, if you don’t remember the last tutorial, we have a Spaceship in a symbol Movie Clip with the name “myShip”. So, go to your Spaceship.as, where the main code are. bellow the trace(“inited”); add: init(); out of the public function Spaceship() let’s create our method init().

the x and y is the position of the myShip over the Flash Scene, the .rotation, makes you rotate the ship, so I rotated it to face the right side, the scaleX and the scaleY makes you rescale your object, in other words I’m rescalling myShip’s height for 60% of his total value and width to 40%. If you put a value over 1, it will be bigger than the original value. The full code should look like:

If you test your “Movie”, you should be able to see the ship “better” positioned for a top-shooter game. 😉

Now let’s make the first move.

II – Keyboard Movement for your Spaceship.

For keyboard movement, you can do it with a lot of different ways. If you are used to program games in other languages, in flash you don’t really need to create a Draw/Tick method, but we are gonna do this way. In flash the Draw/Tick method can be made by many different ways, the most usual is to use “onEnterFrame”, you could use a Time call event or a proper Tick method and I will talk more about it in the future. First of all import:

import flash.events.*;

import flash.ui.Keyboard;

put both import just bellow package{ Now, let’s create some private variable under public class Spaceship:

//Keyboard events:
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown); //Now, when a key is press the method KeyDown will be called
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
stage.addEventListener(Event.ENTER_FRAME, Draw); //every frame will call this method

Like i said before, the Draw method will be called every frame, so make your changes and make the best and most appropriated code for your shi’s movements, because really have too many way to do that.

Your code now should look like:

The next step is if you don’t want to move your ship with a keyboard and prefer to use “Touch” or “Mouse” to do all the movement.

So let’s begin.

III – Simple Touch Press Movement for your Ship

First, I must say, in Actionscript 3 you really may use touch, but despite of it, we are going to use click with mouse, the great difference is, with touch, we can have mult touch events and it will work in a different and heavy way,

How it will work: when you click in the screen above the myShip, it will move up and when you click bellow the ship, it will move up, yes only up and down movements, no left and right, just the y-axis will work. You can see it working in one of my game that is in Play Store for free “When My Cat Fall“.

**UPDATED**

Here we had a problem, when we clicked with the mouse, our ship went to the direction and never stopped, to fix that:

Add a new var in public class Spaceship:

private var lastyclicked:Number=0;

Now see bellow the red text (where I updated), the idea is the ship stop around we clicked for last. The updated code is not in the final code, but it is in the zip file, so be aware. 🙂

For this we will add one new private var inside of our blackcat class: