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 Module: control multiple sprites with the same name

    Pythonista
    motion scene
    2
    3
    1853
    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.
    • Drizzel
      Drizzel last edited by

      Hi guys,
      I'm creating a fairly simple game at the moment where I need multiple enemies to walk over my screen. As I do not want to waste hundreds of lines just to position and create my enemy sprites put the code to create and position an enemy into a loop that runs for 20 times.

      For x in range(20):
         self.enemy = SpriteNode(textures[0])
         self.enemy.size = self.size / 10
         self.enemy.position = (x,y)
         self.add_child
      (self.enemy)
      

      The idea was, that I could now control all 20 sprites with a singe command like: self.enemy.run_action(Action.move_to(x, y, time))
      Well, I can't, so pythonista must be renaming the old sprite if I create a new sprite with the same name. Anyone know how the old sprite is named?

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

        you should create a list of enemies. you keep overwriting the self.enemy variable!

        self.enemies=[]
        for i in range(10):
            self.enemies.append(SpriteNode(...))
           ...
        
        for enemy in self.enemies:
            enemy.add_action(...)
        
        1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

          also.. if you always want all enemies to move together, you can add the enemies as children of a Node. then the entire node can be positioned together

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