BdR Posted November 1, 2017 Share Posted November 1, 2017 I'm working on an action game where the player can touch certain switch tiles to turn other enemies off and back on. So the way it is supposed to work is like this: 1. The player overlaps a switch tile 2. Execute the switch action only once 3. Ignore further overlapping with that tile until player moves off 4. When player moves off the switch should wait for the next overlap 5. Repeat from step 1 when player overlaps again I'm using arcade physics and the overlap function and it's working.. sort of. The problem is, the overlap keeps firing over and over again. What would be the best way in Phaser to get the desired result? See screenshot below of what I mean, and I've created a sandox of my code example here:https://phaser.io/sandbox/edit/zEVOQfgA Quote Link to comment Share on other sites More sharing options...
BdR Posted November 3, 2017 Author Share Posted November 3, 2017 I've been playing around some more and stumbled on a solution. The trick is to use 2 global variables. One to check if there is an overlap, and one to see if the switch action was already done. Then it can all be handled in the update() function. function update() { // assume no overlap frameoverlap = 0; // do arcade.overlap game.physics.arcade.overlap(mushroom, theswitch, handleCollide, null, this); // check flags after arcade.overlap if (frameoverlap == 1) { if (doswitch == 0) { doswitch = 1; // remember the switch was done doSwitch(); } } else { if (doswitch == 1) { doswitch = 0; // stepping off the switch tile } } } See updated code herehttps://phaser.io/sandbox/edit/VTenTwgh Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.