Online: 103   Members: 4,232

Using the arrow keys

Have you ever wanted to be able to move an object with just your arrow keys? Perhaps the beginning steps to creating your very own game. The steps are very simple. First of all, create a new file and either draw or import some image that you wish to move. In my case, to keep things simple, I just drew a square.

Now, convert whatever you have chosen into a movie clip (Right Click > Convert To Symbol) and add the following actioscript.

on (keyPress "<Left>") {
	currentX = this._x;
	this._x = currentX - 4;
	_root.move._rotation = 270;
}

on (keyPress "<Right>") {
	currentX = this._x;
	this._x = currentX + 4;
	_root.move._rotation = 90;
}

on (keyPress "<Up>") {
	currentY = this._y;
	this._y = currentY - 4;
	root.move._rotation = 360;
}

on (keyPress "<Down>") {
	currentY = this._y;
	this._y = currentY + 4;
	_root.move._rotation = 180;
}

And viola! As you can see from the code I have set up each arrow to move the square by 4 pixels in the respective direction. You can easily change this to be whatever size you need for your own applications. Here is my final result.

Related Tutorials

Useful Resources