Quantunet.com

My Account

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

Download .fla

How to create following effect.

Actionscript

var dist:Number = 5;
var keyListenerUpDown:Object = new Object();
var keyListenerLeftRight:Object = new Object();

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

Key.addListener(keyListenerUpDown);
Key.addListener(keyListenerLeftRight);


How The Actionscript Works

The multiple listeners ensures that when specific keys are pressed in combination dual signals can be sent sent allowing the ball to move up and left at the same time or right and down. In this example left and right or up and down cannot be pressed and the same time as they would cancel each other out anyway.

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