Aug 6, 2007

Flash Lite Gaming Tutorial : Continuous KeyPress for Moving Characters in Side-Scrollers

Action or adventure based games may require characters to move around the screen. We use the Key Object for interacting with the game characters. While keypresses and keyreleases respond quickly when the swf file runs on the desktop, these events work differently for swfs running on a mobile. A valid reason is that mobiles have lesser processing speeds thus making it slower for them to recognize keypresses or keyreleases before implementing them.

Thus, when you try to move a character with a keypress, you will observe a slight pause before the character starts moving. A solution that I mostly use is, making the keypress event interact with a loaded Movieclip.






The Key Events

var keyList:Object = new Object();
var dir:Number = 0;
var keyDownState:Boolean = false;

We begin by declaring 3 variables –
keyList of Object type to receive the KeyDown and KeyUp notifications,
dir of type Number to store the direction of the character based on the keys pressed
keyDownState of type Boolean to check whether the key is pressed down or up.

keyList.onKeyDown = function(){
  if(not keyDownState){
    var keyCode = Key.getCode();
    if(keyCode == 37){
      dir = -1;
    }else if(keyCode == 39){
      dir = 1;
    }
    keyMover.gotoAndPlay(2);
    char.gotoAndPlay(2);
    char._xscale = 100*dir;
    keyDownState = true;
  }
}

keyList.onKeyUp = function(){
  dir = 0;
  keyDownState = false;
  char.gotoAndStop(1);
  keyMover.gotoAndPlay(1);
}

The code above declares 2 events – The KeyDown event and the KeyUp event.

A KeyDown event is executed when a key is pressed. This event runs a block of code as long as the key remains pressed. Our code on the other hand, runs the statements only once and not continuously because the condition (if not keyDownState) fails to be recognized as false after its first run. The reason why we have set this condition is, because we don’t want to loop through the same code multiple times for the same key pressed.

var keyCode = Key.getCode();

The getCode() method return the key code value of the last key pressed. Based on the value returned, the direction of the character is set. This code checks for only 2 keys – the Left key and the Right key. Any other key pressed is ignored.

keyMover.gotoAndPlay(2);
char.gotoAndPlay(2);
char._xscale = 100*dir;

If you look at the script above, in the first line keyMover refers to a loaded MovieClip on the stage that contains the script to move the character.

The second line refers to the character MovieClip. This MC initially has the character in a standing posture on frame 1. When a key is pressed the character moves to the next frame which has it animating.

Lastly, we need to flip the character based on the key pressed. So we use the _xscale property to flip the character based on the key pressed.

A KeyUp event is called when a key is released. In our code, all the variables are reset to their original value and Movieclips proceed to halt on the first frame of their timeline at the time of a KeyUp event.

KeyMover Movie Clip

The KeyMover MovieClip consists of 3 frames

Frame 1
var mc:MovieClip = _root.char;
var dir:Number = 0;
var speed:Number = 3;
stop();


The first frame sets the necessary variables and also sets a reference to the character MovieClip on stage.

Frame 2
dir = _root.dir;
if(mc._x < mc._width/2 and dir < 0){
  dir = 0;
}else if(mc._x > (Stage.width – mc._width/2) and dir > 0){
  dir = 0;
}
mc._x += speed*dir;

The second frame sets a condition to check to see if the character is moving outside the screen area. If the condition is met to true then the direction is set to 0

Finally the character moves at a value which is direction X velocity

Frame 3
gotoAndPlay(2);

The third frame loops the Movieclip between frame 2 and 3.

The Final Run

Now when you play this in the emulator or the device the character will respond immediately to the keypressed without any pause.

The source file has been uploaded for your reference here

Labels: ,

5 Comments:

At 8:41 AM , Anonymous Scott Jnaousek said...

Mariam, thanks for taking the time to post about this.

 
At 12:27 PM , Blogger Blue Chi said...

Hello Mariam, sorry to hijack this post. I have written a very detailed tutorial on how to create a Flash Lite game which I think would be of interest to your readers, hope you can plug it in an upcoming post.

http://www.oman3d.com/tutorials/flash/litegame/

Thanks,

Riyadh Al Balushi

 
At 2:08 AM , Anonymous Marcos said...

Thanks Mariam, a great tutorial.

[...]Mariam Dholkawala, ha publicado un interesante tutorial de como enfrentarse al control de las pulsaciones continuadas en un juego para móviles, debido a las limitaciones de prestaciones y procesamiento que tienen nuestros queridos dispositivos[...]

 
At 11:23 AM , Blogger Mariam Dholkawala said...

@scottjanousek - Thanks for reading it Scott

@marcos - Glad you liked the tutorial..and thanks for mentioning it on your blog :)

 
At 11:46 PM , Anonymous Anonymous said...

Hi Mariam, I have a question for you...how do you handle two simultaneous key presses in flashlite? You will find this kind of requirement in the games like a bike taking a curve

Would be great if you can make a simple example and show me.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home