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
ActionScript Code:
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!