orthopedic pain management

Flashuser

  • Pin It

Tips & Tricks 1: Moving an object with arrow keys

Beggining with today we start a series of posts called Day trick. We want to share some useful and inspire methods that can help you in the process of learning Flash.

If you want to contribute, just send us, an email, with your Day trick, and we will make it public. Instead of it we will put a link to the contributor’s web-site.

So for today’s trick, you will learn how to move an object using arrow keys.

1. Create a shape(square, circle) and convert it to a MovieClip. Get him a instance name.(I called it circle)

2. Now all you have to do is place this code in the first frame of stage.

[as3]this.onEnterFrame = function() {
listen = new Object();// create a listener object for the arrow keys
listen.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
circle._xscale = -100;//makes object flip left
circle._x -= 0.1;//move with 0.1 pixels to the left
} else if (Key.getCode() == Key.RIGHT) {
circle._xscale = 100;//makes object flip right
circle._x += 0.1;//move with 0.1 pixels to the right
} else if (Key.getCode() == Key.UP) {
circle._y -= 0.1;//move with 0.1 pixels to the up
} else if (Key.getCode() == Key.DOWN) {
circle._y += 0.1;//move with 0.1 pixels to the down
}
};
Key.addListener(listen); //start listening
};
[/as3]

Author: Alin

Passionate about web and graphic design, photography and climbing. Follow me at Twitter , Facebook and Google+