xronn Posted March 4, 2014 Share Posted March 4, 2014 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 More sharing options...
Heppell08 Posted March 4, 2014 Share Posted March 4, 2014 In Update try:if(trap.frame === 2){ this.game.physics.overlap(hero, trap, this.onTrapHit, null, this);} Link to comment Share on other sites More sharing options...
xronn Posted March 4, 2014 Author Share Posted March 4, 2014 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 More sharing options...
Heppell08 Posted March 4, 2014 Share Posted March 4, 2014 Try this then:if(trap.frame === 2){hitSound.play(); if (cursors.left.isDown) { hero.animations.play('dmgLeft'); hero.health -=1; } if (cursors.right.isDown) { hero.animations.play('dmgRight'); hero.health -=1; } } Link to comment Share on other sites More sharing options...
xronn Posted March 4, 2014 Author Share Posted March 4, 2014 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 More sharing options...
Heppell08 Posted March 4, 2014 Share Posted March 4, 2014 Ok I see now. Set it like this:this.physics.overlap(player, TrapGroup)Then in update use the code I gave you. If you have the group set correctly and have:TrapGroup.add(trap1); TrapGroup.add(trap2); etc etc then all should work well. Link to comment Share on other sites More sharing options...
Heppell08 Posted March 4, 2014 Share Posted March 4, 2014 Scrap the above. Use this:this.game.physics.overlap(hero, trapGroup, this.onTrapHit, trap.frame == 2, this); Link to comment Share on other sites More sharing options...
Heppell08 Posted March 4, 2014 Share Posted March 4, 2014 Scrap the above. Use this:this.game.physics.overlap(hero, trapGroup, this.onTrapHit, trap1.frame === 2, this); Link to comment Share on other sites More sharing options...
Heppell08 Posted March 4, 2014 Share Posted March 4, 2014 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 More sharing options...
xronn Posted March 4, 2014 Author Share Posted March 4, 2014 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 More sharing options...
Heppell08 Posted March 4, 2014 Share Posted March 4, 2014 this.game.physics.overlap(hero, trapGroup);Then:if(trap1.frame === 2){// hurt player in update}Don't worry about the onTraphit function. Use the update and test that. Link to comment Share on other sites More sharing options...
xronn Posted March 4, 2014 Author Share Posted March 4, 2014 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 More sharing options...
Heppell08 Posted March 4, 2014 Share Posted March 4, 2014 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) overlappingB)the frame is 2.This one should work everytime Link to comment Share on other sites More sharing options...
xronn Posted March 5, 2014 Author Share Posted March 5, 2014 Hi, I gave that one ago but no damage is taken on either frame 1 or frame 2, I can't believe nothing we tried has worked. I cant be the only one trying to run events based on a frame of a sprite Link to comment Share on other sites More sharing options...
Heppell08 Posted March 5, 2014 Share Posted March 5, 2014 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 More sharing options...
Heppell08 Posted March 5, 2014 Share Posted March 5, 2014 Last option that should work:this.physics.overlap(hero,trap1,hurtPlayer);hurtPlayer: function(){ if(trap1.frame === 2} { console.log('Working!'); hero.health -=1; }} Link to comment Share on other sites More sharing options...
xronn Posted March 5, 2014 Author Share Posted March 5, 2014 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 More sharing options...
Heppell08 Posted March 5, 2014 Share Posted March 5, 2014 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 More sharing options...
Heppell08 Posted March 5, 2014 Share Posted March 5, 2014 Disregard the {// hurt } and just use the hurtplayer function. Typing on my phone and keep writing like that sometimes. Link to comment Share on other sites More sharing options...
xronn Posted March 5, 2014 Author Share Posted March 5, 2014 I'm using; hurtPlayer: function() { if(trap1.frame === 2) { console.log('Working!'); hero.health -= 1; } }, Link to comment Share on other sites More sharing options...
Heppell08 Posted March 5, 2014 Share Posted March 5, 2014 Doesn't the console not log anything? Link to comment Share on other sites More sharing options...
xronn Posted March 5, 2014 Author Share Posted March 5, 2014 nope nothing at all which is so strange, I'm gonna see if we can get Rich to take a look at this thread see if he can see whats up Link to comment Share on other sites More sharing options...
Heppell08 Posted March 5, 2014 Share Posted March 5, 2014 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 More sharing options...
Heppell08 Posted March 5, 2014 Share Posted March 5, 2014 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 More sharing options...
xronn Posted March 6, 2014 Author Share Posted March 6, 2014 No I don't understand why its saying that I have changed a few things but its still showing I'm not sure Link to comment Share on other sites More sharing options...
Recommended Posts