mondord Posted May 5, 2014 Share Posted May 5, 2014 Is it possible to have setTileIndexCallback only trigger the callback when a specific sprite is over the tile? I've got a game I'm building where the player wants to get to an exit to end a level, and that exit is marked as a special tile index in the tileset map. At the moment it seems that this kind of code:map.setTileIndexCallback(2, this.finishLevel, this);will get triggered when *any* sprite goes over the tile -- and because I've got other sprites flying around the game area, whenever one of them connects with the exit square the level ends (even though the player didn't get there). Is it possible to only trigger the callback when the player is over the tile? Or, to check a parameter in the callback itself to determine what sprite caused the function to fire, and act differently depending on that? Link to comment Share on other sites More sharing options...
Heppell08 Posted May 5, 2014 Share Posted May 5, 2014 Just set true false on the sprite when on the tile within the tile callback. Eg:function: FinishLevel(){spriteTile1 = true;if(sprite1Tile === true){ // finish level } else return;},// that's a rough way anyway but that's how I'd approach that with the sprite1Tile variable set to false until on the tile and then its true and then finishes the level. If its not sprite1 on the tile, it just returns. You could put another statement there for anything you required though. Link to comment Share on other sites More sharing options...
mondord Posted May 5, 2014 Author Share Posted May 5, 2014 Sorry, not sure I follow. How do I get a handle on the sprite that triggered the callback (by colliding with the tile in question)? Is it passed as a named parameter of some kind? Link to comment Share on other sites More sharing options...
Heppell08 Posted May 6, 2014 Share Posted May 6, 2014 My only example would really be to modify the true false within the tile function, then in your update set an if() to detect if something is true to end the game: var spriteTile1 = false;function: finishLevel(){ spriteTile1 = true;if(spriteTile1 === true) { // next level loaded in here } else return; // return because spriteTile1 is false or wrong sprite overlap},Totally psuedo code and you would have to compensate for setting the variable flag on the specific sprite to call the change and end the level but it down to how you're wanting to achieve this really. Is the game multiplayer and hence why only a certain sprite may achieve the end game? Link to comment Share on other sites More sharing options...
mondord Posted May 6, 2014 Author Share Posted May 6, 2014 I guess the problem I have is that right now, the function that determines if a sprite is over the top of the tile -- map.setTileIndexCallback -- doesn't seem to be able to tell me which sprite it is. It gets triggered by any sprite, such as enemies or bullets that get fired. So in the callback I need a handle on the sprite that caused the function to be called. It's not multiplayer, it's single player, but there are sprites that get fired at or chase the player and their movements obviously can go over the same tiles as the player, such as the exit square. So question is really, if I have the callback on the map tile as above, it needs to do something like this (pseudo code only):finishLevel: function(spriteThatTriggeredMe) { if(spriteThatTriggeredMe === player) game.state.start('nextLevel'); }Is that possible? Link to comment Share on other sites More sharing options...
Heppell08 Posted May 6, 2014 Share Posted May 6, 2014 Yes that's possible. Set it up as sprite within the function but nothing else. It would go:function: finishLevel(sprite){ spriteTile1 = true;if(spriteTile1 === true) { // next level loaded in here } else return; // return because spriteTile1 is false or wrong sprite overlap},So you declare only the sprite and only then should it fire on just the sprite player you want and no other sprites. Link to comment Share on other sites More sharing options...
Heppell08 Posted May 6, 2014 Share Posted May 6, 2014 function: finishLevel(sprite){ // next level loaded in here},Scrap the above post, you can remove 90% of that function and just use this one. Link to comment Share on other sites More sharing options...
mondord Posted May 6, 2014 Author Share Posted May 6, 2014 Actually, I just tried my pseudocode in my game and it works! That callback does pass the sprite that triggered it, so a check of if(sprite === player) does the trick. Thanks for all the replies! I'm constantly impressed with Phaser, just an amazing library. Link to comment Share on other sites More sharing options...
Larzan Posted September 1, 2014 Share Posted September 1, 2014 I was looking for this info (callback gets sprite as a parameter) for a while (well, ok, just a few minutes, but still) because it is not in the docs.Did i just not find it or could we extend the docs to include this info?Here it only says callback, but not with which parameters: http://docs.phaser.io/Phaser.Tilemap.html#setTileIndexCallback Link to comment Share on other sites More sharing options...
Larzan Posted September 1, 2014 Share Posted September 1, 2014 And the callback gets actually the sprite and the tile that have participated in the collision (same as the arcade collide callbacks link) Typescript example for callback:_myCallback( sprite: Phaser.Sprite, tile: Phaser.Tile) { ...} Link to comment Share on other sites More sharing options...
Recommended Posts