【AS3代码】用键盘方向键控制影片拖动
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
public class Main extends Sprite
{
var mascot:Bo = new Bo();
var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
public function Main():void
{
init();
}
private function init():void
{
this.addChild(mascot);
stage.addEventListener(KeyboardEvent.KEY_DOWN,downs);
stage.addEventListener(KeyboardEvent.KEY_UP,ups);
stage.addEventListener(Event.ENTER_FRAME,moves);
}
public function downs(e:KeyboardEvent):void
{
if(e.keyCode == 37)
{
leftArrow = true;
}
else if(e.keyCode == 38)
{
upArrow = true;
}
else if(e.keyCode == 39)
{
rightArrow = true;
}
else if(e.keyCode == 40)
{
downArrow = true;
}
}
public function ups(e:KeyboardEvent):void
{
if(e.keyCode == 37)
{
leftArrow = false;
}
else if(e.keyCode == 38)
{
upArrow = false;
}
else if(e.keyCode == 39)
{
rightArrow = false;
}
else if(e.keyCode == 40)
{
downArrow = false;
}
}
public function moves(e:Event):void
{
var speed:Number = 15;
if(leftArrow && upArrow)
{
mascot.x -= speed;
mascot.y -= speed;
}
if(leftArrow)
{
mascot.x -= speed;
}
else if(upArrow)
{
mascot.y -= speed;
}
if(rightArrow && downArrow)
{
mascot.x += speed;
mascot.y += speed;
}
if(rightArrow)
{
mascot.x += speed;
}
else if(downArrow)
{
mascot.y += speed;
}
}
}
}
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
public class Main extends Sprite
{
var mascot:Bo = new Bo();
var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
public function Main():void
{
init();
}
private function init():void
{
this.addChild(mascot);
stage.addEventListener(KeyboardEvent.KEY_DOWN,downs);
stage.addEventListener(KeyboardEvent.KEY_UP,ups);
stage.addEventListener(Event.ENTER_FRAME,moves);
}
public function downs(e:KeyboardEvent):void
{
if(e.keyCode == 37)
{
leftArrow = true;
}
else if(e.keyCode == 38)
{
upArrow = true;
}
else if(e.keyCode == 39)
{
rightArrow = true;
}
else if(e.keyCode == 40)
{
downArrow = true;
}
}
public function ups(e:KeyboardEvent):void
{
if(e.keyCode == 37)
{
leftArrow = false;
}
else if(e.keyCode == 38)
{
upArrow = false;
}
else if(e.keyCode == 39)
{
rightArrow = false;
}
else if(e.keyCode == 40)
{
downArrow = false;
}
}
public function moves(e:Event):void
{
var speed:Number = 15;
if(leftArrow && upArrow)
{
mascot.x -= speed;
mascot.y -= speed;
}
if(leftArrow)
{
mascot.x -= speed;
}
else if(upArrow)
{
mascot.y -= speed;
}
if(rightArrow && downArrow)
{
mascot.x += speed;
mascot.y += speed;
}
if(rightArrow)
{
mascot.x += speed;
}
else if(downArrow)
{
mascot.y += speed;
}
}
}
}