23 January 2006

Coding : Best Practice for Key Detection in Flash Lite 2

Tips using key Detection from luar Using Button

In Flash Lite 1.1, if we want to detect a key press (single press or continuous press), we need to attach the code to a button:

keyPress “{
no += 1;
}


In Flash Lite 2, we can use back modern ActionScript, use Movie Clip or Object to do the key detection, for example:

Using Movie Clip as Key Listener

mc.onKeyDown = function(){
switch(Key.getCode()) {
case Key.UP :
no += 1;
break;
}
};
Key.addListener(mc);
Using Object as Key Listener
obj = new Object();
obj.onKeyDown = function(){
switch(Key.getCode()){
case Key.UP:
no += 1;
break;
}
}
Key.addListener(obj);

However, using key listener has a side effect, you cannot detect continuous key press (press the key without release). If you want to detect continuous key press, you need to use an

MovieClip.onEnterFrame:

mc.onEnterFrame = function(){
if(Key.isDown(Key.UP)){
no += 1;
}
};
//Key.addListener(obj);

By the way, I find the Flash Lite 2 emulator is even worse than Flash Lite 1.1 emulator because the latter one support pressing keyboard for testing (single press or continuous press), no need to use the mouse to click the phone button.

Here is the summary of the above testing:

Continuous Pressing Detecting Method Press without Release in Handset Press phone button without Release Press the key in the PC keyboard without Release
FL 1.1 Emulator FL 2 Emulator FL 1.1 Emulator FL 2 Emulator
Button Support (Slower) Not Support Not Support Support Not Support
Object Not Support N/A Not Support N/A Not Support
Movie Clip Not Support N/A Not Support N/A Not Support
Movie Clip onEnterFrame Support (Faster) N/A Support N/A Not Support

PPL IAIN Syekh Nurjati Cirebon

Jika ditanya apa yang kau suka dari kehadiran mereka? Mereka punya jawabannya : semangat yang tinggi dengan keingintahuan yang tipikal mahas...