Quantunet.com

My Account

Joins Us
Flash 8 Actionscript 2.0 Knowledgebase
Using The Arrow Keys III (Listener Method)  

Download .fla

How to create following effect.


Actionscript

var dist:Number = 5;
var keyListener:Object = new Object();

keyListener.onKeyDown = function() {
if (Key.isDown(Key.LEFT)) {
ball_mc._x -= dist;
} else if (Key.isDown(Key.RIGHT)) {
ball_mc._x += dist;
} else if (Key.isDown(Key.UP)) {
ball_mc._y -= dist;
} else if (Key.isDown(Key.DOWN)) {
ball_mc._y += dist;
}
};
Key.addListener(keyListener);


How The Actionscript Works

Although this is more efficient than checking conditions each time the movie enters a new frame it still can only move the ball in one direction at a time as the key listener can only broadcast one keydown signal at a time. One way around this is to create different listeners for differenkeys. To learn more see [Moving Objects With Keys IV].

© 2008 Quantunet LLC All Rights Reserved | Intellectual Property | Terms of Use | Privacy
Home | About Quantunet | FAQ's | Contact Us