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
-
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 codefrom 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
-
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)
-
@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
-
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()