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
-
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? -
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(...)
-
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