In my collision code where I make heavy usage of for in loops to cycle through an array of wall names I have been experiencing some buggy behaviour.
To check collision I check the player object against every wall to see if there is a collision. The array stores the names of the walls so that they can be retrieved from the project object when I want to check collision. Whenever I do this in the loop i get an error.
“Line null: TypeError: project.getObject(…) is null”
Here’s a code excerpt from the player object:
speed = 4;
function update() {
prevx = this.x;
prevy = this.y;
hitHorizontal = false;
hitVertical = false;
walls = project.getObject("Data").walls
project.getObject("Debug").setText(walls[0])
if(keyIsDown("Up")) {
this.y -=speed;
}
if(keyIsDown("Down")) {
this.y +=speed;
}
for (var wall in walls) {
if (this.hitTest(wall)) {
hitVertical = true;
}
}
if(hitVertical) {
this.y = prevy;
}
if(keyIsDown("Left")) {
this.x -=speed;
}
if(keyIsDown("Right")) {
this.x +=speed;
}
for (var wall in walls) {
if (this.hitTest(wall)) {
hitHorizontal = true;
}
}
if(hitHorizontal) {
this.x = prevx;
}
}
and here’s the array in question
project.getObject("Data").walls = ["Wall1"]
And here’s a screenshot of the scene in question