dmauro / Keypress

A keyboard input capturing utility in which any key can be a modifier key.
http://dmauro.github.io/Keypress/
Apache License 2.0
3.18k stars 313 forks source link

im getting "TypeError: target is null" on line 111 in the keypress.js file #85

Closed kapperbeast closed 9 years ago

kapperbeast commented 9 years ago

when running this code: var monsterPos = [0]; var monsterList= [0]; var singleMonster = [0]; var frame = 0; var isAttacking = false; var listener = new window.keypress.Listener(); // var l = 1;

$(document).ready(function() { setInterval(moveRight3, 100);

//document.addEventListener("keydown", attack1);
//document.addEventListener("keyup", function(){isAttacking=false;});
var monsterList = document.getElementsByClassName('monster');
var lenght = monsterList.length
for (var i = 0 ; i < length; i++)
{
    singleMonster = getPosition(monsterList[i]);
    monsterPos[monsterPos.length] = singleMonster.x;
}
// $(".monster").each(singleMonster = findPos(this);
// monsterPos[monsterPos.length] = singleMonster[0];
// monsterList[monsterList.length] = l; l++;);

});

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)//currently not in use { 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")) - 40))}); }

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

}}

listener.register_combo({ "keys" : "a", "on_keydown" : function(){$("#hero").attr("src","attack.png"); isAttacking = true; for (q=0, len = monsterPos.length; len > q; q++){ if (400 > monsterPos[q] > 560){ $("monsterList["+q+"]").css(opacity, 0); } } }, on_keyup : function(){isAttacking = false;} });

dmauro commented 9 years ago

Are you running this in a headless something or other? It sounds like document.body is evaluating to null.