由于某些我不知道的原因,它认为我的数组按键没有定义。错误是:"访问未定义的属性pressedKeys。"但是这个数组在我的onKeyDown和Up函数中运行良好。有什么想法吗?
以下是代码:
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
var Entities:Array = new Array();
var PressedKeys:Array = new Array();
//Create player ...more code
function onEnterFrame(event:Event):void
{
updateVel();
//...
}
function updateVel()
{
if (pressedKeys[37]) //getting error here
{
// left
player.velX -= player.speed;
}
else if (pressedKeys[65])
{
player.velX -= player.speed;
}
if (pressedKeys[38])
{
// up
player.velY -= player.speed;
}
else if (pressedKeys[87])
{
player.velY -= player.speed;
}
//more code...
}
function onKeyDown(event:KeyboardEvent):void
{
PressedKeys[event.keyCode] = true; //works fine here
trace("Keycode: " + event.keyCode + " is: " + PressedKeys[event.keyCode]);
}
function onKeyUp(event:KeyboardEvent):void
{
PressedKeys[event.keyCode] = false;
trace("Keycode: " + event.keyCode + " is: " + PressedKeys[event.keyCode]);
}
function rand(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
答案 0 :(得分:0)
您需要使用PressedKeys
而不是pressedKeys
- 请注意大写/小写。