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.


    Scene.update() for classes

    Pythonista
    update scene
    2
    4
    3034
    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.
    • Bjucha
      Bjucha last edited by

      Hello everybody, Im trying to get a SpriteNote class to have a function with a nested Scene.update() funnction. However it does not work, Does anybody know why or maybe is not possible for classes to have update()?
      Here is the code

      from scene import *
      A = Action
      
      class Enemy (SpriteNode):
      	def __init__(self, **kwargs):
      		SpriteNode.__init__(self, 'spc:EnemyBlack4', **kwargs)
      		
      		
      		
      
      	
      	
      class Galaxy (Scene):
      	def setup (self):
      		self.background_color = '#0c1037'
      		self.test = []
      		self.blasers = []
      			
      	def update(self):
      		
      		self.enemy_appear()
      	
      	def enemy_appear(self):
      		enemy = Enemy(parent=self)
      		enemy.position = (355,540)
      		self.test.append(enemy)
      		
      		def update(self):
      			blasers = SpriteNode('spc:LaserBlue11', parent=self)
      			blasers.position = enemy.position + (0, 30)
      			blasers.z_position = -1
      			
      			actions = [A.move_to(5, 100, 2 * 5), A.remove()]													
      			blasers.run_action(A.sequence(actions))
      			self.blasers.append(blasers)
      
      
      run(Galaxy())```
      
      Happy for any help
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        your class has an update method. that gets called every frame of the scene.

        then, you define a function called update, within enemy_appear, which is local to enemy_appear, and never referenced or called by anybody.

        Maybe what you meant to do is have an update method inside the SpriteNode? You can do that, but then you need to call it from your scene (i.e, loop over enemies and call each of their updates)

        Bjucha 1 Reply Last reply Reply Quote 0
        • Bjucha
          Bjucha @JonB last edited by

          @JonB Yeah I thought that having a update within the enmey appear function would work but nothing happens, But the update for Galaxy(Scene) does work and updates the function so I use it. Thanks

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

            Update is automatically called for presented Scenes. A standard approach is then to do within your scene update:

            for node in self.children:
               if hasattr(node,'update'):
                  node.update()
            
            1 Reply Last reply Reply Quote 1
            • First post
              Last post
            Powered by NodeBB Forums | Contributors