Check if a button is not clicked

I would like to check if a button is not clicked, somewhat like the below code.

if (//button clicked) {
    //do something
}

else {
    //do something else
}

Can I do this in PC?

What do you mean by ‘clicked’? Did you mean toggle like a tick box?

Or is this more of, if someone clicks in the game and it wasn’t the button, do something?

As in, an else to the below code.

this.entity.element.on('click', function (event) {
    // stop bubbling
    event.stopPropagation();

    console.log('The element ' + event.element.entity.name + ' was clicked.');
}, this);

Can you give an example of how it would be used in your game? Perhaps there is a different way of doing what you need?

I am working with @DevilZ on his racing game. If you go to this scene, you will notice a gas pedal. Previously, you had helped me out in this thread with my problems letting a car’s rpm decrease in your third post on the thread. Is there any way to do the same here, the button being the control to rev the car up; the car’s rpm decreasing if the button is not pressed? I need mobile controls as well.

Ok, that makes sense.

The way I would do this is to have a boolean variable that keeps track of when the button is pressed and released (listening to events pressedstart and pressedend).

In the update loop, I could then use that boolean to perform the acceleration or deceleration logic based on it’s value.

2 Likes

Could you please send an example code of what you mean, especially about the events?

Example: https://playcanvas.com/editor/scene/843492

Pretty much the same logic as the keyboard example I shared before but needs to use events to listen for the press/release callbacks on the button in the UI

I do recommend reading this page about using PlayCanvas events: https://developer.playcanvas.com/en/user-manual/scripting/communication/

2 Likes

Thanks. I’ll take a look, and work on it.