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 Hazneliel

Hello.
How and where in the code I put instructions for something to happen when an entity collides with another entity??

I see you can use the .type property in conjuction with the .checkAgainst function to do this, but there are only 2 types and I need my entity to react in different ways depending which entity hits, eg, to explode if its the player, to make a sound if its an enemy, to make another sound if its another kind of enemy.

Im looking forward your help.
Thank you.

1 decade ago by dominic

The .type property is meant for fast but coarse checks. E.g. you can put all friendly entities in group A and all hostile entities in group B.

If you want to further specify things, you could handle the different cases in the Entity&039;s #check function:
check: function( other ) {
	if( other instanceOf EntityBlob ) {
		// handle collisions with the EntityBlob
	}
	else if( other instanceOf EntityGrunt ) {
		// handle collisions with the EntityGrunt
	}
	// ... etc
}

Or, a bit more elegant - assuming that your different types of enemies or items don't interact with each other, but only with the player - you could implement the behavior in the specific enemy/items entities themselfs. E.g. something like this:

EntityPlayer = ig.Entity.extend({
	type: ig.Entity.TYPE.A,
	
	// checks are handled in the enemy classes, not here
	// in the player class
	checkAgainst: ig.Entity.TYPE.NONE,
	
	…
});


EntityExploder = ig.Entity.extend({
	type: ig.Entity.TYPE.B,
	
	// handle checks with group A
	checkAgainst: ig.Entity.TYPE.A,
	
	check: function( other ) {
		// If the only entity with the type A is the player, we can safely 
		// assume here that 'other' is always the player entity.

		// You can always do some further checks here.
		
		// give the player some damage on collision:
		other.receiveDamage( 10 ); 
	}
});

// Like EntityExploder, but with different behavior:
EntityHealthPickup = ig.Entity.extend({
	type: ig.Entity.TYPE.B,
	checkAgainst: ig.Entity.TYPE.A,
	
	check: function( other ) {
		// give the player some health:
		other.health += 50;
	}
});

1 decade ago by Hazneliel

Thank you, it is very helpfull.


Just one last thing and I will try to not bother you a lot.
How do you prevent on the iPhone when you drag on the screen, the entire page moves like if it would scroll to the next page, and in the Biolab game for the iphone the drag thing is disabled. How do you do that?

1 decade ago by dominic

Don't worry, this forum exists so you can ask questions :)

Biolab uses this code snippet - put it at the end of you HTMLs body tag:
<script type="text/javascript">
document.body.addEventListener('touchmove', function(event) {
	// Don't allow scrolling
	event.preventDefault();
	
	// Additionally, this will scroll the screen to 0,0  whenever the 
	// user tries to scroll - effectivly hiding  the address bar. 
	// This may be a bit irritating for the user?!
	window.scrollTo(0,0); 
});
</script>

1 decade ago by Hazneliel

Thank you
Page 1 of 1
« first « previous next › last »