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.
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
};
Tags: actionscript, day trick, motion
FREE Image Scroller Gallery XML AS2
X-Treme Accordion XML
3D Image Rotator Carousel AS2
XML Image Scroller Gallery AS2