Jump to content

trap.frame == X Collision


xronn
 Share

Recommended Posts

Hi, 

 

I have the following code and I'm trying to enable collision between my player and frame 2 of a sprite. This is what I have so far;

this.game.physics.overlap(hero, trap, this.onTrapHit, trap.frame == 2, this);    onTrapHit: function(hero, trap){        hitSound.play();        if (cursors.left.isDown)        {            hero.animations.play('dmgLeft');            hero.health -=1;        }        if (cursors.right.isDown)        {            hero.animations.play('dmgRight');            hero.health -=1;         }    },

The code doesn't throw any errors but on both frames hurt the player when it should only be frame 2

Link to comment
Share on other sites

Hi, thanks for your reply I gave this a go 

Now when I walk over the trap in either frame 1 or frame 2 nothing happens. It doesn't appear to run anything in the callback function at all so I'm not sure that function is being run using this if statement 

Link to comment
Share on other sites

Still nothing same result as the above no damage being taken on either frame, just a little extra info i'm using Phaser 1.1.3, and this is how I set my traps;

 

        //Spike Trap        trapGroup = this.add.group();        trap1 = trapGroup.create(240, 208, 'spikes');        trap1.animations.add('spike', [1,2], 1, true);        trap = trapGroup.create(240, 224, 'spikes');        trap.animations.add('spike', [1,2], 1, true);        trap.body.immovable = true;

I possibly should call the group? I'm not sure I don't see why with the statement you provided above their is still no output. 

Link to comment
Share on other sites

You were missing the call for overlapping with the group and the frame won't reference as trap.frame it needs to be trap1.frame as you have the sprite in the group called trap1. You can use update code for sprites inside a group but the name is always as you declare within the group and creation. As for the group overlap, whatever is put into the group, trap1 or 2 or 100, they will ALL overlap with the code called for overlapping player and group and any other function you may have in place. So just make sure all names are the same and that groups are called for physics stuff if used in the manner you are using them. Hope this helps :)

Link to comment
Share on other sites

Hi, I tried this but it still takes health on frame 1 as well as frame 2, I created a live link to see if that would help anymore. The traps are the spikes that come up from the ground the live link can be found here -

www.xronn.co.uk/game

 

Thanks for your help so far 

Link to comment
Share on other sites

Nope, no damage taken within the if statement there I just set it to remove health just for testing quicker to write than all the if statements inside too.

 

live code updated 


I don't see how the last snippet would relate to each other, you say overlap, trapGroup but have no call back so that doesnt do anything? Then you got if trap1.frame ===2 hurt player which means every time the frame is 2 it should hurt the player regardless of the position of the player. 

 

Link to comment
Share on other sites

The overlap covers most of it. Maybe the way I explained wasn't right. The way I code for certain things is mainly in update so if I was doing what you are trying to do I'd wrap it in if() statement and it would work. So obviously the overlap will occur and something will happen. Now you want the 2nd frame to damage but not the first. This could be coded in the update with the physics and overlap, then check for frame 2 then damage if all works out so you would have the if working in an a way that under them special set of rules that it will do the damage you require :-

if(this.physics.overlap(hero, trapGroup) && trap1.frame === 2) { // inflict pain here in a certain way }

So the above works only if the player is:

A) overlapping

B)the frame is 2.

This one should work everytime :)

Link to comment
Share on other sites

Not sure if its because you're code is minified and its changed a bit but you need to put:

 

if(this.game.physics.overlap(hero,trapGroup) && trap1.frame===2){hero.health-=1;console.log('Hurts!');}

Put that line exactly as it is in the update. If that doesn't work then theres something deeper going on. 

 

Also for the sake of testing can you also try:

if(this.game.physics.overlap(hero,trap1) && trap1.frame===2){hero.health-=1;console.log('Hurts!');}
Link to comment
Share on other sites

Hi,

 

I gave this a go, I changed a few things the if statement it was ( } and I had to make it "hero, trap1, this.hurtPlayer);

But when I walk over the traps in either frame 1 or 2 it doesn't console.log anything out so that function defiantly isn't being run which is very odd

Link to comment
Share on other sites

Yeah I've been writing them in my phone so was a typo lol. Based on the fact that the function isn't being called means the overlap is not happening or something deeper. Try:

this.physics.overlap(hero,trap1,hurtPlayer,null,this)

{ //hurt here }

If none of that works then something is going very wrong. I can't see why the numerous different codes haven't yielded even a single positive result. If that doesn't work then I have no idea why it doesn't work.

Link to comment
Share on other sites

Put a new version on the website but non-minified. Just the raw code as you see it. Maybe there is something deeper going on but I don't really know. If you try removing the trapgroup and just overlapping with trap1.

Not sure if anyone else can see a way around thisd but I'm stumped.

Link to comment
Share on other sites

In your game on the website there is this caution error:

 

Phaser.AnimationParser.spriteSheet: width/height zero or width/height < given frameWidth/frameHeight

 

That refers to this:

//  Zero or smaller than frame sizes?        if (width === 0 || height === 0 || width < frameWidth || height < frameHeight || total === 0)        {            console.warn("Phaser.AnimationParser.spriteSheet: width/height zero or width/height < given frameWidth/frameHeight");            return null;        }

pulled directly from phaser.

 

I see you're adding the spritesheet like this:

this.load.spritesheet('spikes','assets/tiles/spikeTrap.png',16,32);

Have you looked into that warning for any reason why it is warning and why the fram code you want to work is not working?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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