代码
// initialize arrow variables
var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
var mascot:Mascot = new Mascot;
mascot.x = 200;
mascot.y = 200;
addChild(mascot);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveMascot);
stage.addEventListener(Event.DEACTIVATE, missingStage);
// set arrow variables to true
function keyPressedDown(event:KeyboardEvent)
{
if (event.keyCode == 37)
{
leftArrow = true;
}
else if (event.keyCode == 39)
{
rightArrow = true;
}
else if (event.keyCode == 38)
{
upArrow = true;
}
else if (event.keyCode == 40)
{
downArrow = true;
}
}
// set arrow variables to false
function keyPressedUp(event:KeyboardEvent)
{
if (event.keyCode == 37)
{
leftArrow = false;
}
else if (event.keyCode == 39)
{
rightArrow = false;
}
else if (event.keyCode == 38)
{
upArrow = false;
}
else if (event.keyCode == 40)
{
downArrow = false;
}
}
// set arrow variables to false
function missingStage(e:Event)
{
leftArrow = false;
rightArrow = false;
upArrow = false;
downArrow = false;
}
// move every frame
function moveMascot(event:Event)
{
var speed:Number = 5;
if (leftArrow)
{
mascot.x -= speed;
}
if (rightArrow)
{
mascot.x += speed;
}
if (upArrow)
{
mascot.y -= speed;
}
if (downArrow)
{
mascot.y += speed;
}
}
var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
var mascot:Mascot = new Mascot;
mascot.x = 200;
mascot.y = 200;
addChild(mascot);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveMascot);
stage.addEventListener(Event.DEACTIVATE, missingStage);
// set arrow variables to true
function keyPressedDown(event:KeyboardEvent)
{
if (event.keyCode == 37)
{
leftArrow = true;
}
else if (event.keyCode == 39)
{
rightArrow = true;
}
else if (event.keyCode == 38)
{
upArrow = true;
}
else if (event.keyCode == 40)
{
downArrow = true;
}
}
// set arrow variables to false
function keyPressedUp(event:KeyboardEvent)
{
if (event.keyCode == 37)
{
leftArrow = false;
}
else if (event.keyCode == 39)
{
rightArrow = false;
}
else if (event.keyCode == 38)
{
upArrow = false;
}
else if (event.keyCode == 40)
{
downArrow = false;
}
}
// set arrow variables to false
function missingStage(e:Event)
{
leftArrow = false;
rightArrow = false;
upArrow = false;
downArrow = false;
}
// move every frame
function moveMascot(event:Event)
{
var speed:Number = 5;
if (leftArrow)
{
mascot.x -= speed;
}
if (rightArrow)
{
mascot.x += speed;
}
if (upArrow)
{
mascot.y -= speed;
}
if (downArrow)
{
mascot.y += speed;
}
}