omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Making interactive buttons with duration

    Pythonista
    touch began update touch scene
    2
    3
    3766
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • rex_noctis
      rex_noctis last edited by

      I don’t really know how to explain this but I’ll try.
      I’m making a game using scene. I am using four buttons to control the movement of the player, however currently, if I hold down on the movement buttons, the character only moves one step in the given direction. The reason for this is because in def touch_began(self, touch): I have only told it to move the player one step. I tried implementing a variable that changed depending on what button was pressed and then in def update(self): telling it to move in whichever direction the variable ( move) specifies. When I try this, the buttons don’t even respond. How can I get it so the player ( self.player) moves in the direction of the button pressed until the user is no longer pressing the button?
      Here is my game code at the moment (I have only programmed the up button in touch_began):

      from scene import *
      import time
      
      class Game(Scene):
      	def setup(self):
      		self.background_color = '#ffffff'
      		
      		#Up button
      		self.upCtrl = SpriteNode('2018-07-21 7.32.39 pm.png')
      		self.upCtrl.x_scale = 100/2048
      		self.upCtrl.y_scale = 100/2048
      		self.add_child(self.upCtrl)
      		self.upCtrl.position = (self.size.x/2), 175
      		
      		#Left button
      		self.leftCtrl = SpriteNode('2018-07-21 7.17.22 pm (1).png')
      		self.leftCtrl.x_scale = 100/600
      		self.leftCtrl.y_scale = 100/600
      		self.add_child(self.leftCtrl)
      		self.leftCtrl.position = (self.size.x/2)-100, 75
      		
      		#downButton
      		self.downCtrl = SpriteNode('2018-07-21 7.17.22 pm (2).png')
      		self.downCtrl.x_scale = 100/600
      		self.downCtrl.y_scale = 100/600
      		self.add_child(self.downCtrl)
      		self.downCtrl.position = self.size.x/2, 75
      		
      		#Right button
      		self.rightCtrl = SpriteNode('2018-07-21 7.17.22 pm (3).png')
      		self.rightCtrl.x_scale = 100/600
      		self.rightCtrl.y_scale = 100/600
      		self.add_child(self.rightCtrl)
      		self.rightCtrl.position = (self.size.x/2)+100, 75
      		
      		#Game map
      		self.gameMap = SpriteNode('2018-07-21 8.06.38 pm.png')
      		self.gameMap.x_scale = 2
      		self.gameMap.y_scale = 2
      		self.add_child(self.gameMap)
      		self.gameMap.position = self.size.x/2, (self.size.y/2)+100
      		
      		#Player sprite
      		self.player = SpriteNode('2018-07-21 9.05.27 pm.png')
      		self.player.x_scale = 2
      		self.player.y_scale = 2
      		self.add_child(self.player)
      		self.player.position = (self.size.w/2)-415, (self.size.h/2)+100
      	
      	def touch_began(self, touch):
      		touch_loc = touch.location
      		if touch_loc in self.upCtrl.frame:
      			self.player.position = self.player.position.x, self.player.position.y+5
      
      run(Game(), LANDSCAPE, show_fps=True)```
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        you want to set flags in touch_began, and clear them in touch_ended, that are then used by update to add to motion. sort of like what you said you tried, but you didnt post that attempt. likely you just had a bug in your implementation.

        Or, for a "joystick" type operation, you would compute the vector between the touch and center of the joystick sprite, which you would set to a variable in touch_began, update in touch_moved, and set to zero in touch_ended. Then you would scale that to some desired joystick sensitivity, and then treat as a delta position or delta velocity depending on how you want your physics to work.

        1 Reply Last reply Reply Quote 0
        • rex_noctis
          rex_noctis last edited by

          Thanks, it’s working now!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors