Posted on June 9th, 2009Rasmus Wriedt Larsen4 comments
First of all, I want to say: If you are completely new at ActionScript, then you should check out Michael James Williams tutorials, starting here
Now one of the problems I see in many games are the movement of the main character. Just take a look at two games at ArmorGames: Medieval Rampage, Hands of War RPG.
I think this is rather strange as it is very easy to manage. First of all I want to show how you make a character move, this is really not complicated. We’re going to make a game with correct movement with WASD and arrow keys.
First of all, we need something to move, I drew a little man for this purpose:
Make him a symbol and call the instance man. And I made a little part of the checked floor so we can keep in mind how much he moved, more on this later. I placed my little guy on the middle of the floor.
Now we need to write some come for this little guy to move.
First I want you to make a new AS file and call this main, link to this as the Documentclass in Flash. Let Main extend MovieClip and remember to import.
Then we’re going to add some Boolean variables for each direction and a variable to hold our speed, take a look:
Now, all we have is some Boolean variables telling us if we’re pressing one of the direction buttons. That’s not fun. Let’s add a loop to handle the movement. First of, lets add a timer (once again, remember to import flash.utils.Timer):
Now the reason of that high a delay is that we’re going to move the character a lot, so having a small delay would make him jump off the screen, not good! Let’s add an eventlistener and start the timer (remember to import flash.events.TimerEvent):
function onTick(even:TimerEvent):void{var xSpeed:Number=0;
var ySpeed:Number=0;
if(leftIsPressed){
xSpeed-=speed;
}if(rightIsPressed){
xSpeed+=speed;
}if(upIsPressed){
ySpeed-=speed;
}if(downIsPressed){
ySpeed+=speed;
}
man.x+=xSpeed;
man.y+=ySpeed;
}
Notice that I made to variables, xSpeed and ySpeed, you could have just updated the mans x and y position, but if both up and down are pressed flash has to update the players position twice, with this code it’s only once (this will also have an other benefit later on). I also made an if statement to all our buttons, you could have made it like this:
But if the player pressed both left and an other button at the same time, he would always move left, now he is just not moving at all. This is great. Anyway, check it out. (click to it to activate, use arrow keys or WASD to move – you have to hold them for 0.5 second) (Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
Now we made a simple movement, just as almost every other game out there, this may be fine for some games (fx platformers), but for a top-down game we need to do a little more of work. Can you find the error yourself?
What if I told you to take one sped forward, and afterwards take a step where you moved both forward and just as much to the left, would you have taken a 40 % lager step? I think not, but our little guy is doing just exactly this right now. Many times walking diagonally like this could change the gameplay a lot! I’ve made a green circle to show where the player actually should be going.(click to it to activate, use arrow keys or WASD to move – you have to hold them for 0.5 second) (Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
How to fix this you ask?
Phythagoras. If you don’t know what this is, then I would surges you checked out Michael James Williams post on this
Now in our case we know what the speed is, but we do not know how much the guy has to walk horizontally or vertically. How to figure that out?
Now we just need to do this:
And there we are, everything is done. You can check it out here: (click to it to activate, use arrow keys or WASD to move – you have to hold them for 0.5 second) (Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
[...] you haven’t read my Character Movement tutorial yet, do so here. Second of all, you should have read the base tutorials by Michael James Williams, do that here. [...]
AS3: Diagonal Movement in the Avoider Game June 7th, 2009 at 13:05