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.


    Need Help With Scene 2d and Buttons

    Pythonista
    4
    10
    4356
    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.
    • PyNewb94
      PyNewb94 last edited by ccc

      So in my code I have a script which shows a button on screen , when I press the button it’s suppose to make a sound but it doesn’t , eventually I want the button to control the character here’s the code

      from scene import *
      import ui
      import sound 
      import os 
      
      x, y = get_screen_size()
      
      button_font = ('Avenir Next' , 20)
      
      
      class ButtonNode (SpriteNode):
      	def __init__(self, title, *args, **kwargs):
      		SpriteNode.__init__(self, 'pzl:Button1', *args, **kwargs)
      		button_font = ('Avenir Next', 20)
      		self.title_label = LabelNode(title, font=button_font, color='black', position=(0,1), parent=self)
      		self.title = title
      		
      
      
      class Game(Scene):
      	def setup(self):
      		
      		
      		self.background_color = '#000000'
      		self.btn = ButtonNode ('Rotate')
      		self.btn.position =(100,80)
      		self.add_child(self.btn)
      		
      		def touch_began(self, touch):
      		 self.handle_touch(touch)
      		 
      		 
      		def touch_moved(self, touch):
      			pass
      		
      		def touch_ended(self, touch):
      			pass 
      		
      		def handle_touch(self, touch):
      		 if touch.location in self.btn.frame:
      		 	sound.play_effect('8ve:8ve-beep-attention')
      		
      		self.player = SpriteNode('spc:PlayerShip2Red')
      		self.player.anchor_point = (0.5, 0)
      		self.player.position = (self.size.w/2, 120)
      		self.player.size = (20,20)
      		self.add_child(self.player)
      		
      		
      		
      if __name__=='__main__':
      			run(Game(),LANDSCAPE, show_fps = True, multi_touch = True )
      

      Can anyone point me in the right direction ?

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @PyNewb94 last edited by cvp

        @PyNewb94 you have a problem of indentation of your def's.

        class Game(Scene):
          def setup(self):
        
            self.background_color = '#000000'
            self.btn = ButtonNode ('Rotate')
            self.btn.position =(100,80)
            self.add_child(self.btn)
            
          def touch_began(self, touch):
             self.handle_touch(touch)
        
        1 Reply Last reply Reply Quote 0
        • cvp
          cvp last edited by

          Thus

          from scene import *
          import ui
          import sound 
          import os 
          
          x, y = get_screen_size()
          
          button_font = ('Avenir Next' , 20)
          
          
          class ButtonNode (SpriteNode):
              def __init__(self, title, *args, **kwargs):
                  SpriteNode.__init__(self, 'pzl:Button1', *args, **kwargs)
                  button_font = ('Avenir Next', 20)
                  self.title_label = LabelNode(title, font=button_font, color='black', position=(0,1), parent=self)
                  self.title = title
                  
          
          
          class Game(Scene):
              def setup(self):
                  
                  
                  self.background_color = '#000000'
                  self.btn = ButtonNode ('Rotate')
                  self.btn.position =(100,80)
                  self.add_child(self.btn)
                  
              def touch_began(self, touch):
                  self.handle_touch(touch)
                   
                   
              def touch_moved(self, touch):
                  pass
                  
              def touch_ended(self, touch):
                  pass 
                  
               
              def handle_touch(self, touch):
                  if touch.location in self.btn.frame:
                      sound.play_effect('8ve:8ve-beep-attention')
                  
                  self.player = SpriteNode('spc:PlayerShip2Red')
                  self.player.anchor_point = (0.5, 0)
                  self.player.position = (self.size.w/2, 120)
                  self.player.size = (20,20)
                  self.add_child(self.player)
                  
                  
                  
          if __name__=='__main__':
                      run(Game(),LANDSCAPE, show_fps = True, multi_touch = True )
          
          PyNewb94 2 Replies Last reply Reply Quote 1
          • PyNewb94
            PyNewb94 last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • PyNewb94
              PyNewb94 @cvp last edited by

              @cvp how would I set the code up to make the player rotate when I press the button ?

              mikael 1 Reply Last reply Reply Quote 0
              • mikael
                mikael @PyNewb94 last edited by

                @PyNewb94, check the Action class (and consider where you have the anchor point).

                E.g.

                self.player.run_action(Action.rotate_by(math.pi, 1))
                

                Rotates the node by a relative 180 degrees in 1 second.

                1 Reply Last reply Reply Quote 0
                • PyNewb94
                  PyNewb94 @cvp last edited by

                  @cvp Okay got it to rotate left and to rotate right , how can I go about applying force now like a thrust , I tried action_by But spaceship jumps across the screen ,

                  cvp mikael 2 Replies Last reply Reply Quote 0
                  • cvp
                    cvp @PyNewb94 last edited by

                    @PyNewb94 I'm not a specialist of Scene, action.. and the guy who adviced "action rotate" is @mikael

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

                      https://github.com/encela95dus/ios_pythonista_examples contains some scene examples (look at the scene section of the readme ).

                      1 Reply Last reply Reply Quote 0
                      • mikael
                        mikael @PyNewb94 last edited by mikael

                        @PyNewb94, you can

                        run_action(Action.forever(Action.move_by(*speed)), "thrust")
                        

                        where speed is an (x,y) tuple or a Point, giving the relative change in ship’s position in 0.5 seconds. You change the speed by starting the action again with the same key, "thrust", or stop it with Node.remove_action.

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