我正试图通过按键从图书馆播放一个动作片,我已经将其链接但它仍然不起作用(这只是代码的一部分,有事件监听器和处理程序到位且角色移动很好)
stage.addEventListener(Event.ENTER_FRAME, Move);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
var aP:Boolean = false;
var dP:Boolean = false;
//creating a new Character_right instance
var character_right:Character_right = new Character_right();
function Move(vet:KeyboardEvent)
{
if(aP)
{
char.x -= 5;
char.scaleX = -0.55;
}
if(dP)
{
char.x += 5;
char.scaleX = -0.55;
}
}
function keyPress(evt:KeyboardEvent)
{
switch(evt.keyCode)
{
case Keyboard.A:
{
aP = true;
break;
}
case Keyboard.D:
{
dP = true;
character_right.play();
break;
}
}
}
function keyUp(evt:KeyboardEvent)
{
switch(evt.keyCode)
{
case Keyboard.A:
{
aP = false;
break;
}
case Keyboard.D:
{
dP = false;
break;
}
}
}
答案 0 :(得分:0)
检查this post,提供一个关于键盘事件的精彩教程,我相信对您理解概念并实施逻辑非常重要。
简单地说,它类似于以下示例,但请记住为您的范围实现所有必要的逻辑。
var isRight:Boolean;
var isLeft:Boolean;
var isUp:Boolean;
var isDown:Boolean;
//creating a new Character_right instance
var character_right:Character_right = new Character_right();
stage.addEventListener(KeyboardEvent.KEY_DOWN, downKeyHandler, false, 0, true);
function downKeyHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.RIGHT )
{
isRight = true;
}
if (event.keyCode == Keyboard.LEFT)
{
isLeft = true;
}
if (event.keyCode == Keyboard.UP)
{
isUp = true;
}
if (event.keyCode == Keyboard.DOWN)
{
isDown = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, upKeyHandler, false, 0, true);
function upKeyHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.RIGHT)
{
isRight = false;
}
if (event.keyCode == Keyboard.LEFT)
{
isLeft = false;
}
if (event.keyCode == Keyboard.UP)
{
isUp = false;
}
if (event.keyCode == Keyboard.DOWN)
{
isDown = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, loopHandler, false, 0, true);
function loopHandler(event:Event):void
{
if (isRight)
{
//do something here…
character_right.play();
}
if (isLeft)
{
//do something here...
}
if (isUp)
{
//do something here...
}
if (isDown)
{
//do something here...
}
}
答案 1 :(得分:0)
在您的代码中,您在Character_right.play()
之后执行函数break
并且break会停止执行其余的循环代码,因此在Character_right.play()
之前执行break
,例如:
function keyPress(evt:KeyboardEvent)
{
switch(evt.keyCode)
{
case Keyboard.A:
{
aP = true;
break;
}
case Keyboard.D:
{
dP = true;
Character_right.play();
break;
}
}
}