RSS link icon

Right left character movement 

 

In this Flash actionscript tutorial we will make a simple smiley character move around left and right, and limit by ground width with flash actionscript

I know this could be done far easier then I do it, but there is a lot of great reasons for doing it my way, we have all checks and movements in the enter_frame event, which makes sure that we can actually have two separate movements at once, so if you hold down left key and if we also could set it to jump, it will not jump straight up but up to the left.

Also its easy to apply new movement rules (like up and down just by applying a new if statement in the move function below.

 

But for this example we only set it to go to the right and left, as in this example.

First we need to set up some elements on stage, we need to make a ground movie clip, give it the instance name ground_mc, then make a man or some kind of graphic that you want to walk, and convert it into a movie clip, give it the instance name man_mc.

Now we are ready to do some code, so open up the actionscript panel and type in the following code.

I have written the description between the code so you can follow along and understand it better (I hope).

 

Declare a variable to hold the keycode for the left and right arrows

var keyPressed:Number;
// I will try to explain how it works here, as long as one of these boolean values are true the man will walk.
var rightKey:Boolean;
var leftKey:Boolean;
// This variable controls the walking speed
var speed:Number = 15;
// a function we run when flash opens the project.

 

Function to start

function start_():void {
    // sets both boolean values to false
    rightKey = false;
    leftKey = false;
    // we add eventlisteners to listen if a key is pressed and released, calling functions pressKey and releaseKey
    stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
    stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
}

 

The pressKey function

function pressKey(event:KeyboardEvent):void {
    keyPressed = event.keyCode;
    //saying if the keypressed equals to the keycode for the left key
    if (keyPressed == Keyboard.RIGHT) {
        // if so, then set rightkey is true and it will walk
        rightKey = true;
	}

// the same for left key
if (keyPressed == Keyboard.LEFT) {
	leftKey = true;
	}
    
    // Now we add an eventlistener with the enter_frame event to actually do the walking.
myMan.addEventListener(Event.ENTER_FRAME, manMove);
}

 

The actual function to make it walk.

function manMove(event:Event):void {
    // in here we set some limitations we set it only to walk th the right edge of the ground.
    if (rightKey && myMan.x < ground_mc.x + ground_mc.width - myMan.width) {
    myMan.x += speed;
}

 

Same limitation for the ground left edge which is far easier to calculate :-)

if (leftKey && myMan.x > ground_mc.x) {
	myMan.x -= speed;
	}
}
// this last function is to check is a key is released so we stop walking
function releaseKey(event:KeyboardEvent):void {
    var Key_:Number = event.keyCode;
    // if we release left key, we set the boolean value back to false
    if (Key_ == Keyboard.RIGHT) {
    	rightKey = false;
	}
    // if we release right key, we set the boolean value back to false
    if (Key_ == Keyboard.LEFT) {
    	leftKey = false;
    }
}

 

Of cause we need to start the function to have an event

start_();

Test your flash movie and you should have a man walking on the ground. you could now set an if statement if the man walks out of the edge so he dies.


psp lover says: Wednesday, November 12, 2008

it doesn't work and I tried 5 - 10 times and the same old thing came up


Admin Bob says: Sunday, August 17, 2008

Hi Tim, thats a typical qustion when you are changing from actionscript 2 to 3, you cant add script to movie clips, you add the script only to the frames, and then call the movieclips by their instance names. eg. car.height


Tim says: Monday, August 11, 2008

hi, ive had some problems with my action scripts. im new at using flash, and ive been trying to make very very basic movement scripts. but for some reason it says i cant add scripts to my movie clips. like...i can add action to my frame, by when i hit f9 on my mcChr(34)s to add action, where i type in the box it says i cant add action..and i cant type anything in that box...please help


   


 

 

 

 3

 
 
   Web Premium
 
 

All rights reserved, Copyright 2008. Contact