PDA

View Full Version : [AS3] How do i make a character move and turn using arrow keys?


rawrimarabbit
12-19-2010, 10:09 PM
I'm really terrible with actionscript and no matter where i look i just cant seem to find a tutorial for as3 in which it explains how to make an animated character move and turn using the arrow keys. Would appreciate any help i can get or even a link to a tutorial if anyone has found a good one. Thanks!

franky87
12-19-2010, 10:47 PM
Very simple setup here. I made a object and made it a symbol and converted to symbol(named it Ball). Then exported it for ActionScript.

I then made a actionscript file and named it Main.as and insterted the following under.

Main.as

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;

public class Main extends Sprite
{
private var ball:Ball;
private var speed:Number = 5;
public function Main ():void
{

ball = new Ball();
ball.x = stage.stageWidth/2;
ball.y = stage.stageHeight/2;
addChild(ball);

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
}
private function KeyDown (key:KeyboardEvent):void
{
switch(key.keyCode)
{
case Keyboard.LEFT:
ball.x -= speed;
break;

case Keyboard.RIGHT:
ball.x += speed;
break;

case Keyboard.UP:
ball.y -= speed;
break;

case Keyboard.DOWN:
ball.y += speed;
break;
}

}
}
}


Bare in mind that this is NOT the best way to do it. It is just a simple way to get you started.

If you want to have more smooth flow in the movement and add some physic, check out Foundation Actionscript 3.0 Animation: Making Things Move! great book to start with.

Good luck with your scripting!

rawrimarabbit
12-19-2010, 11:00 PM
thanks so much! this is an awesome starting point for me to work from ^_^