Page 1 of 1

[Solved] Activate button on press (not release)

Posted: Sun Jul 07, 2019 9:09 am
by goldo
Hi everyone,

I am making a minigame where the player must click on a moving button.

However, the clicking only 'happens' upon releasing the mouse button, not pressing it down, which leads to player frustration as they have to release it really quick or nothing happens. It also leads to a strange situation where it works better to always hold the mouse button down then release it on target, instead of just clicking.

So, long story short, is there any way to create a button that activates on pressing down the left mouse button?

Re: Activate button on press (not release)

Posted: Sun Jul 07, 2019 11:49 am
by Per K Grok
goldo wrote:
Sun Jul 07, 2019 9:09 am
Hi everyone,

I am making a minigame where the player must click on a moving button.

However, the clicking only 'happens' upon releasing the mouse button, not pressing it down, which leads to player frustration as they have to release it really quick or nothing happens. It also leads to a strange situation where it works better to always hold the mouse button down then release it on target, instead of just clicking.

So, long story short, is there any way to create a button that activates on pressing down the left mouse button?

key mousedown acts on press, so you can use than to build your own 'button' that reacts to on press.

You could do it with the code below.

bX,bY is the position of the button
bW,bH is width and height.

Code: Select all


screen checkMouse():
   key "mousedown_1" action Jump("checkClick")

---------

label checkClick:
   python:
       mX=renpy.get_mouse_pos()[0]
       mY=renpy.get_mouse_pos()[1]

    if bX<mX<bX+bW and bY<mY<bY+bH:
        --- god things happen ---
   else:
        --- bad things happen ---

    jump gamelabel
One thing you have to watch out for with this time of solution is 'side rollback'. You need to have that turned off or make certain the 'button' never is in the side rollback area.

If by moving you mean jumping from point to point in an instance you will always know bX and bY, so that will not be a problem.

If you mean moving like in a Duck Hunt type game that can be more problematic dependent on how the movement is coded. But that can be handled to.

Re: Activate button on press (not release)

Posted: Sun Jul 07, 2019 12:37 pm
by Andredron
Per K Grok wrote:
Sun Jul 07, 2019 11:49 am
goldo wrote:
Sun Jul 07, 2019 9:09 am
Hi everyone,

I am making a minigame where the player must click on a moving button.

However, the clicking only 'happens' upon releasing the mouse button, not pressing it down, which leads to player frustration as they have to release it really quick or nothing happens. It also leads to a strange situation where it works better to always hold the mouse button down then release it on target, instead of just clicking.

So, long story short, is there any way to create a button that activates on pressing down the left mouse button?

key mousedown acts on press, so you can use than to build your own 'button' that reacts to on press.

You could do it with the code below.

bX,bY is the position of the button
bW,bH is width and height.

Code: Select all


screen checkMouse():
   key "mousedown_1" action Jump("checkClick")

---------

label checkClick:
   python:
       mX=renpy.get_mouse_pos()[0]
       mY=renpy.get_mouse_pos()[1]

    if bX<mX<bX+bW and bY<mY<bY+bH:
        --- god things happen ---
   else:
        --- bad things happen ---

    jump gamelabel
One thing you have to watch out for with this time of solution is 'side rollback'. You need to have that turned off or make certain the 'button' never is in the side rollback area.

If by moving you mean jumping from point to point in an instance you will always know bX and bY, so that will not be a problem.

If you mean moving like in a Duck Hunt type game that can be more problematic dependent on how the movement is coded. But that can be handled to.
Else:

viewtopic.php?f=51&t=51263

Re: Activate button on press (not release)

Posted: Sun Jul 07, 2019 1:21 pm
by goldo
Thanks! It is a kind of duck hunt type game.

Branching to a jump will not be practical as I will have several moving buttons that must keep running at once, but I guess a Function would work just as well.

The only problem is: how can I get the button's coordinates at the time of clicking to make sure the cursor is over it? (the button is being moved around with ease transforms)

Re: Activate button on press (not release)

Posted: Sun Jul 07, 2019 3:00 pm
by Per K Grok
goldo wrote:
Sun Jul 07, 2019 1:21 pm
Thanks! It is a kind of duck hunt type game.

Branching to a jump will not be practical as I will have several moving buttons that must keep running at once, but I guess a Function would work just as well.

The only problem is: how can I get the button's coordinates at the time of clicking to make sure the cursor is over it? (the button is being moved around with ease transforms)
I did a duck hunt type game a while back in ren'py (with a bit of extra pygame)

viewtopic.php?f=11&t=51931&p=496272&hil ... ok#p496272

What I did was that I created a small loop in the script in which I updated the position of the ducks (or pterodactyls in my case). I also checked for hits in that loop (using pygame's keydown). So I always was updated on the position of each hitbox.

Code: Select all


label timerlab:
    $renpy.pause(delay=0.02, hard=True)


label runner:
    python:
        mx, my = pygame.mouse.get_pos()
        
    --- check hit ---
        
    --- update position of 'ducks' ---
        
   --- update and show 'hunter' (direction the weapon is pointed)---
   
   jump timerlab     

I rendered the 'ducks' in a screen. If I remember correctly because they where animated images and the loop interfered with the animation.

An admittedly hacky solution. But it worked for me.

Thanks for mentioning functions. That suddenly made me realize that a function could be the solution to a completely different problem I have struggled to find a good solution to. :)

Re: Activate button on press (not release)

Posted: Sun Jul 07, 2019 5:24 pm
by Imperf3kt
You could always use imagebuttons, you can move those and they can be clicked on even while moving with no need to track position.

Re: Activate button on press (not release)

Posted: Fri Jul 12, 2019 6:49 am
by goldo
Imperf3kt wrote:
Sun Jul 07, 2019 5:24 pm
You could always use imagebuttons, you can move those and they can be clicked on even while moving with no need to track position.
Yeah but so do normal buttons, I am back to my original problem which is how to click them when the button is pressed, not released. I am thinking I might look in a different direction if I can't get it to work. I'll keep you updated on the issue.

Re: Activate button on press (not release)

Posted: Sat Jul 13, 2019 6:37 am
by goldo
Hi guys, still trying to work on a solution.

Do you know if it's possible to create a renpy button as an object and store it as a variable? Then, could I ask that object for its coordinates at a point in time?

The logic would be like:

Code: Select all


buttons = [button1, button2, ...]

while running:
    --- make buttons move ---
    --- checks for mouse button down ---
    
    if mouse button down:
        --- ask for cursor coordinates ---
        for button in buttons:
            --- if cursor coordinates == button coordinates ---
            --- CLICK HAPPENS ---
            break
    

Re: Activate button on press (not release)

Posted: Sun Jul 21, 2019 12:57 pm
by goldo
Hi guys,

I'm happy to report I found a way, it's actually surprisingly easy.

Before running the mini-game, I use this code to change button behavior:

Code: Select all

    $ config.keymap['button_ignore'] = []
    $ config.keymap['button_select'] = ['mousedown_1', 'K_RETURN', 'K_KP_ENTER', 'K_SELECT']
Then, after the mini-game is over, I use this to go back to regular button behavior:

Code: Select all

    $ config.keymap['button_ignore'] = ['mousedown_1']
    $ config.keymap['button_select'] = ['mouseup_1', 'K_RETURN', 'K_KP_ENTER', 'K_SELECT']
While changing config values mid-game is not necessarily a good idea, this doesn't seem to cause any negative side effects, so it works for my purposes.