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.
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.
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.
Now when you play this in the emulator or the device the character will respond immediately to the keypressed without any pause.
Labels: Flash Lite, Flash Lite Tutorial
5 Comments:
Mariam, thanks for taking the time to post about this.
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
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[...]
@scottjanousek - Thanks for reading it Scott
@marcos - Glad you liked the tutorial..and thanks for mentioning it on your blog :)
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