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?
Forum Archive
Scene Module: control multiple sprites with the same name
Drizzel
Mar 06, 2018 - 06:05
JonB
Mar 06, 2018 - 06:17
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(...)
JonB
Mar 06, 2018 - 07:01
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