Saturday, 26 September 2009

move your objects with arrow keys using action script

var speed:Number = 4;
object_mc.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
this._x = this._x+speed;
} else if (Key.isDown(Key.LEFT)) {
this._x = this._x-speed;
}
if (Key.isDown(Key.UP)) {
this._y = this._y-speed;
} else if (Key.isDown(Key.DOWN)) {
this._y = this._y+speed;
}
};

//have fun



Action Script explanation:

1.information_txt.text = "Use your arrow keys to move the balloon!";
1. Display instructions on the screen

2 .var speed:Number = 4;
2. Set the speed for the object

3. object_mc.onEnterFrame = function() {
3. Capture the onEnterFrame event of the object_mc movie clip

4. if (Key.isDown(Key.RIGHT)) {
this._x = this._x + speed;
} else if (Key.isDown(Key.LEFT)) {
this._x = this._x - speed;
}

4. Move horizontally if the RIGHT or LEFT arrow keys are pressed

5. if (Key.isDown(Key.UP)) {
this._y = this._y - speed;
} else if (Key.isDown(Key.DOWN)) {
this._y = this._y + speed;
}
};

5. Move vertically if the UP or DOWN arrow keys are pressed

We're done!

Test your Movie (Ctrl+Enter).

No comments: