How do I prevent click "double trigger"?

0 favourites
  • 3 posts
From the Asset Store
High quality sound effect pack, in the following categories: Simple, negative, pozitive
  • So I'm stuck on this issue and I can't find a solution for the life of me.

    I'm trying to prevent double clicking on an object, but it seems that everything I try is either not reliable or straight out hacky.

    Here's a very bare-bones code of what would cause a double trigger:

    - We have an object with instance variable "clicked".

    - On the first click we set the variable to true.

    - On the second click if "clicked" is "true" we destroy the object.

    What would happen however is when we click once, the code will be read in sequence, therefore setting "clicked" to "true" and destroying the object instantly.

    A simple way of fixing this very example would be to move the second click event at the top so it gets checked first and that way it will prevent the double click, however this is easier said then done on a larger scale project with multiple files, functions, etc. so I label this method as "unreliable"

    Second solution would be to create a tick timer and compare against that (skipping the current trigger check), this is how this version would look in code:

    This method will work for most cases and on a larger scale, however to me at least it seems "hacky" and like a disaster waiting to happen. Not to mention if we start using "on click" and "on release" trigger then the click timer would simply not work, since we can hold the button down for longer than the timer.

    I've played with the idea switching layers and visibility, but I'm determined that there is a better solution.

    Does anyone have any clue how to tackle this problem differently?

    Edit: In a way, I want "On click", "Mouse button is down" and "On release" to be treated as one entity and only one event can occur during this period.

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I would do something like this:

    I added the Pick top instance just in case you have overlapping instances, because a mouse click over multiple objects will trigger the clicked event for each object the mouse is over.

  • I have tried that before and it would work in the example above, however there are a couple of issues I personally found with this method:

    - It only checks one object and even if you add multiple objects into a family, not all object types can share the same family (like sprites, tiles and 9-patch, etc). So if two different objects overlap while being clicked, then both clicks will fire.

    - Also this logic will be ignored on "button down" and "on release" triggers, unless specific conditions for those are added as well. Haven't fully explored this route though.

    I do believe however that I managed to rubber duck debug my problem by writing my initial post. This is what I came up with 30 minutes later. I haven't fully tested it, but it does work in theory. Here's a screenshot:

    There are two dependencies in order for this code to run correctly:

    1. After a mouse click trigger event we must set allow_clicks to false.

    2. At the very end, after all mouse trigger events we must reset allow_clicks to true.

    - As a fail-safe I also make sure that allow_clicks is true at the start too.

    If someone finds an issue with this solution and thinks it might break at some point I would love to hear it!

    One potential problem is that I'm relying on this code running synchronously, but as of right now I can't find a situation where it wont.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)