Jump to content

Use overlap to switch once when stepped on tile, but overlap keeps firing


BdR
 Share

Recommended Posts

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

phaser_switch_question.png

Link to comment
Share on other sites

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 here
https://phaser.io/sandbox/edit/VTenTwgh

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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