Repeated UI/Scene actions
I’m looking for a way to have a push button action be performed repeatedly until a button is released. I’m making a game where the player character will move by pressing buttons, and would like them to move continuously in a direction according button they’re pressing I.e. pressing the left button will make the character move left until it is released, or pressing the right button will make the character move right until it is released. I’ve followed these forums since fall 2016 since I’ve been unable to find any questions like this or pythonista games that implement this feature, I’ve finally made an account.
Looping touch_began and touch_ended functions for the scene module, with the exit condition being whether a stack containing the most recent touch action was a press or release.
Integrating these conditions into the update function.
Using a SceneView. However upon finding the delegates for the uiButton, I realised there are no delegate functions besides button_tapped.
if your touch-begin, but don't move, touch_moved doesn,t get called, as you have found.
Scene keeps track of active touches in
Scene.touches. So, you can simply check the status in Scene.update, and then take action there.
Here I converted the Game Tutorial Tutorial3.py to use touch input, instead of gravity input. I am showing a linear joystick like control, but you could just use a fixed speed, and check if touch in right_button.bbox, etc.
@JonB thank you for the swift response. I’ve been trying to figure out the box for the last hour or so. Any advice on bboxes? So far all attempts of the classic “< x < “ tuple comparison don’t work, as it’s giving me an error about how a dict values object does not support indexing, even though it displays as a tuple.
__contains__so you can do things like
if touch.location in self.right_button.bbox: #do something
See example here, refactored dor movement buttons
@JonB thank you! I actually ended up spending an hour just finding the bounds of the buttons I made manually and then having the update method check the touch’s location against the bounds of the buttons in a 4 part if-elif-else block. Thank you for the solution earlier, it helped immensely! I will test this one as well tonight/tomorrow and get back to you on my results!
@JonB it works perfectly! Thank you!