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 Donzo

Hi.

Pardon me for being an idiot,
but I can't figure out why this code isn't working:


check:function(other){

//declare the snake counter
var snakes;

//slow down the player because he's in the water entity
if(other instanceof EntityPlayer){
other.vel.x=1;}

//if snake counter is less than 5 spawn snakes.
if (snakes < 5) {
ig.game.spawnEntity(EntitySnake2,this.pos.x,this.pos.y);

//increase snake counter
snakes++;
}
}

What am I doing incorrectly?

1 decade ago by Arantor

So, anything in the error log/console of the browser?

Also, when you say "isn't working", what is it doing that it shouldn't be doing, or what isn't it doing that it should be?

'isn't working' doesn't tell me much, you see.

1 decade ago by Donzo

No, I don't see errors.

By not working, I mean, snakes don't appear.

But if I do it like this:

check:function(other){
if(other instanceof EntityPlayer){
other.vel.x=1;
ig.game.spawnEntity(EntitySnake2,this.pos.x,this.pos.y);
}
}

So many snakes come that they form a huge overlapping line.
I'd really like to just spawn a few, hence the counter,
but I must be doing something incorrectly,
because no snakes are spawning.

1 decade ago by UltimateBrent

Try moving where your counter is:

snakes: 0,
check: function(other) {
//slow down the player because he's in the water entity
if(other instanceof EntityPlayer){
other.vel.x=1;}

//if snake counter is less than 5 spawn snakes.
if (this.snakes < 5) {
ig.game.spawnEntity(EntitySnake2,this.pos.x,this.pos.y);

//increase snake counter
this.snakes++;
}
}

What I think was happenign is that you're declaring your snake counter every frame, but no setting it. Just callling 'var snakes' doens't set it to zero. So that might be why your snakes<5 if statement is failing. We've moved the variable outside of the function and attached it to the entity, that way it'll still be there next frame. This is called "scope". Things declared inside a function are only available inside that function, and destroyed when the function finishes (basically).

1 decade ago by Donzo

I appreciate your reply.

I suppose I should have told you that I also declared the snakes variable in my water entity init function as so:

(sorry I don't know how to format the code properly)

init: function( x, y, settings ) {
this.parent( x, y, settings );
var snakes;
},

When I remove the "var snakes;" from the check: function
the game freezes when I hit the water.

All entities continue moving, though animation is frozen.

It will unfreeze if I jump, but it is not behaving properly.

1 decade ago by UltimateBrent

Again, you cannot declare it like that in a function and have it be available elsewhere. Try to use this.snakes instead, so it's an object property.

1 decade ago by Donzo

Sorry.

I'm a total noob.

That totally worked.

Thanks for the help, Ultimate Brent!

1 decade ago by UltimateBrent

:) No problem. Glad to help!
Page 1 of 1
« first « previous next › last »