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 spinz89

Hi everyone!
My first post here. I recently purchased impactjs and I'm exploring it a little bit now.
Therefor I'm making a little platform game. The player will be able to use an elevator to get to another platform. The elevator should only move up or down when the player stands in it. So I used the .check function to enable it (elvCollision = true) but I found no way to set the variable back to false.
I thought of something like 'isn't colliding with entity', but I found nothing in the forum nor the documentation.
Thanks in advance! :)

// CHECK IF THE PLAYER (TYPE.A) COLLIDES WITH THE ELEVATOR
	checkAgainst: ig.Entity.TYPE.A,
	check: function( other ){
		elvCollision = true;
	},

1 decade ago by lazer

Perhaps .touches may be what you're after?
http://impactjs.com/documentation/class-reference/entity#touches

1 decade ago by spinz89

Thanks! I tried looking at that and it sounds pretty good. But I'm not really familiar with this syntax. I don't really know where to put it.
I tried a while loop
while (this.touches(other)) {...};

But that didn't really work haha.

Edit:
Allright I managed to get it working!
var playa = ig.game.getEntitiesByType(EntityPlayer);
if(this.touches( playa[0] )){
	console.log('yay!');
}

With the help of his post http://impactjs.com/forums/help/problem-with-touches.
I really don't get why I have to add [0] after my variable. Is it the first EntityPlayer object in this array?
But it's working now yay!

1 decade ago by lazer

I suspect it would be because without the [0], you are referring to the array itself as opposed to the first element in the array :) So you should be able to do something like this if you wanted:

var playa = ig.game.getEntitiesByType(EntityPlayer)[0];
if(this.touches( playa)){
    console.log('yay!');
}

(which would allow you to refer to the 'playa' variable directly, without having to remember to add the [0] to playa)

1 decade ago by spinz89

Unfortunately it seems that doing it this way makes my cpu fan go nuts! :/
I put this code in the update function, but that doesn't seem to be performance friendly.

1 decade ago by lazer

I'm not knowledgeable enough on performance, but why not define the playa variable in the init function? Eg:

in init function:

this.playa = ig.game.getEntitiesByType(EntityPlayer)[0];

in update function:

if(this.touches(this.playa)){
    console.log('yay!');
}

This way you're not setting the 'playa' variable over and over again in the update.

I usually just make a global variable when creating the player (so in the player's init function: ig.game.player = this;

Then I can refer to "ig.game.player" from any other entity at any time. But then, global variables are meant to be less than ideal..

1 decade ago by spinz89

Thank you lazer!
I really don't know why I put the variable into the update function haha...
You really helped me. :)

1 decade ago by lazer

No problem, glad I could help!
Page 1 of 1
« first « previous next › last »