类型错误b未定义

时间:2015-03-05 10:23:57

标签: javascript jquery for-loop

上下文: 我正试图制作一个游戏,一群怪物向着英雄移动,然后他必须按下" a"。我拥有所有带有ID =" monsterX"的怪物,其中X是HTML5代码中怪物的编号

错误:TypeError:b未定义 它似乎是最后一个函数,即attack1中的for循环,它打破了整个函数,就像我已经注释掉了,我可以播放攻击动画,虽然它确实在任何键盘输入上触发了攻击动画,而不仅仅是""

代码:

    var monsterPos = [0];
var monsterList = [0];
var singleMonster = 123;
var frame = 0;
var isAttacking = false;
var l = 1;

$(document).ready(function()
{
setInterval(moveRight3, 100);
document.addEventListener("keydown", attack1);
document.addEventListener("keyup", function(){isAttacking=false;});
$(".monster").each(monsterLister($(this)));
});

function getPosition(element) {
   var xPosition = 0;
   var yPosition = 0;

while(element) {
    xPosition += (element.offsetLeft - element.scrollLeft +           element.clientLeft);
    yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
    element = element.offsetParent;
}
return { x: xPosition, y: yPosition };
}

function monsterLister(element)
{
singleMonster = getPosition($(this));
monsterPos[monsterPos.length] = singleMonster.x;
monsterList[monsterList.length] = l;
l++;
}


 function moveRight3()
{
  frame = (frame+1)%4;
    if (!isAttacking) {$("#hero").attr("src","move"+(frame+1)+".png");}
$('.monster').each(function(index, img){$(this).css("left",  (parseInt($(this).css("left"))  - 20))});
}

function attack1(event)
{
if ($(this).keyCode == 97){
    isAttacking = true;
for (q=1, len = monsterPos.length; len > q; q++){
if (400 > monstersPos[q] > 560){
$("monsterList["+q+"]").css(opacity, 0);
}
    $("#hero").attr("src","attack.png");
    console.log("attack");
}
}}

0 个答案:

没有答案