Jump to content

Can collide with sprite frame x but not with frame y


xronn
 Share

Recommended Posts

Hi,

 

I have a sprite trap that loops very slowly frame 0 its safe but frame 1 it's deadly, can you make a player collide with the same sprite but a different frame?

 

Also am I right in thinking that if you can do this you can make it remove health from the player on a certain frame?

thanks

Link to comment
Share on other sites

You could add an if-statement in the update loop that checks the frame of the trap sprite and then do a collision:

update() {...if(trap.frame == 1) game.physics.collide(trap, player, onTrapHit, null, this);...}function onTrapHit(trap, player) {   // do trap damage}

In this example you can consider that the trap is deadly on frame 1 (you can test against any other frame or frames). The onTrapHit() is a collision callback in which you do all the damage logic. 

Link to comment
Share on other sites

Thank you, extremely useful thanks again for your help, could someone tell me what each parameter does; 

game.physics.collide(trap, player, onTrapHit, null, this);

is this correct 

trap = sprite variable name
player = sprite variable name 

onTrapHit = call back for the next function 
null = not sure
this = to tell the game object what we refer too? 

Link to comment
Share on other sites

Sure.

this.game.physics.collide(this.game.player.ref, this.fadingBlocks, function(ref, block) {    if (ref.body.touching.down) {      return _this.game.player.onBlock = true;    }  };}, function(ref, block) {    return block.currentFrame.index === 0;  };});

This will not work verbatim, it's edited from my compiled coffeescript and likely isn't semantically correct. As for how it works, I make usage of the fourth parameter in the function to determine if the collision can happen with the group "fadingBlocks". Hopefully that helps.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...