Smooth MovieClip Key Control in AS3
In Flash Actionscript 3.0 it is not as simple as AS2 to create a smooth control of a movieclip’s movement. You may find the key engages, then pauses for half a second, then continues. This is no good for gaming. Also the basic KeyboardEvent.KEY_DOWN event listener will only handle one key at a time, so you can not for example hold down the up, and right arrow keys to move diagonally.
The following is a class called Smooth.as. To use it you need an fla 300×300 pixels and with a spaceship MovieClip called Spaceship (in the linkage call the class Spaceship). You also need to save Smooth.as to the same folder and make Smooth the the stage’s document class under properties.
Or just download the source file: smooth
Use the arrow keys to move the ship.
package { import flash.events.*;
import flash.display.*; public class Smooth extends MovieClip {
// Set vars for key codes. Arrow up is 38 for example
static const upKey = 38;
static const downKey = 40;
static const rightKey = 37;
static const leftKey = 39;
// Set vars for current ship speed and maximum speed
private var speed:Number = 0;
private var maxspeed:Number = 4;
// Set vars to keep track of which keys are pressed. This is a Boolean true or false. True if key is pressed.
private var keyPressedRight:Boolean;
private var keyPressedLeft:Boolean;
private var keyPressedUp:Boolean;
private var keyPressedDown:Boolean;
// Create the spaceship MovieClip from a pre-made Spaceship() MovieClip in flash
var spaceship:MovieClip = new Spaceship();
public function Smooth() {
// Set the game/animation loop
stage.addEventListener(Event.ENTER_FRAME, StageController);
// Set event listeners to track key presses for both up and down
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
// Add the spaceship to the stage and give it starter settings
addChild(spaceship);
spaceship.x = 150;
spaceship.y = 150;
spaceship.rotation = -90;
}
// This function sets the Boolean vars based on whether the relevant key has been pressed
public function keyDownListener(e:KeyboardEvent):void {
switch (e.keyCode) {
case leftKey:
keyPressedRight=true;
break;
case rightKey:
keyPressedLeft=true;
break;
case upKey:
keyPressedUp=true;
break;
case downKey:
keyPressedDown=true;
break;
} }
// This function sets the Boolean vars based on whether the relevant key has not been pressed
public function keyUpListener(e:KeyboardEvent):void {
switch (e.keyCode) {
case leftKey:
keyPressedRight=false;
break;
case rightKey:
keyPressedLeft=false;
break;
case upKey:
keyPressedUp=false;
break;
case downKey:
keyPressedDown=false;
break;
}
}
// This is the main game/animation loop
public function StageController(e:Event):void {
// This rotates the ship if key is pressed
if(keyPressedLeft && speed != 0){
spaceship.rotation -=4;
}
if(keyPressedRight && speed != 0){
spaceship.rotation +=4;
}
// This increases the ship's speed if the up key is pressed
if(keyPressedUp && speed < maxspeed){
speed +=0.1;
if(speed > maxspeed){speed = maxspeed;}
}
// This decreases the ship's speed if the down key is pressed
if(keyPressedDown && speed > -maxspeed/2){
speed -=0.1;
}
// This decreases the ship's speed if neither up nor down key are pressed.
if(!keyPressedDown && !keyPressedUp){
if(speed < 0){
speed +=0.1;
}
if(speed > 0){
speed -=0.1;
}
}
// This moves the ship forward based on ship's angle
var scangle:Number = spaceship.rotation * Math.PI / 180; // Ship's angle in radians
spaceship.x += speed*Math.cos(scangle); // Move the ship on the X axis
spaceship.y += speed*Math.sin(scangle); // Move the ship on the y axis
// This makes sure the ship never leaves the viewable stage
if(spaceship.x < 0){spaceship.x = 300;}
if(spaceship.x > 300){spaceship.x = 0;}
if(spaceship.y < 0){spaceship.y = 300;}
if(spaceship.y > 300){spaceship.y = 0;}
}
}
}
AS3 Tips
AS3 Tutorial








Leave a Reply