Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by skel1

Here is a condensed version of my code:

var player = ig.game.getEntitiesByType( EntityPlayer )[0];
		// don't move if you're stunned from being hit
		if(player && (player.pos.x > 0) && !this.beenhit) {
		  var myangle= this.angleTo(player);
 		  this.vel.x = (Math.cos(myangle) * this.accel);
 		  this.vel.y = (Math.sin(myangle) * this.accel);
 		  this.currentAnim.angle= myangle;
		  }

This all works fine until the "player" entity dies. If the player entity dies the game locks up and the javascript console says "Can not read property pos of undefined". The weird thing is, if I comment out the x velocity, it works fine. Put x back in and it crashes. I've tried all sorts of combinations of if statements and x vs y, and it is only the x movement that crashes.

What confuses the problem even further, is that this block of code doesn't get called if the player entity isn't active!

Can anyone offer some insight? I've lost three evenings of my very little dev time to this bug and I'm at wits end. Is it something obvious I'm just over looking?

Thanks!

1 decade ago by skel1

ahh just some more info, this code is on a bad guy entity that always moves towards the player. Everything works fine except this small snippet, and only when the player dies.

1 decade ago by drhayes

A couple of things: this.accel is an object with two properties, x and y. You probably want to reference the properties rather than the object? Multiplying a number by an object in JS gives you NaN.

Also, if the EntityPlayer is no longer in the game then that array is going to be empty (e.g. []). You should check for that before grabbing the first element.

Try fixing those and see what happens.

Are you testing in Chrome? What file and line number is it reporting the error on? In Chrome you can set breakpoints in your JS code so that execution stops at a certain point so you can check values.

You can conditionally launch the debugger by using the statement debugger; in your code.

1 decade ago by skel1

A couple of things: this.accel is an object with two properties, x and y.


For as many times as I've looked at this piece of code, I never caught that. That was exactly the problem. Thank you! Sometimes it takes a second set of eyes to find the proverbial missing semicolon.
Page 1 of 1
« first « previous next › last »