Forum Archive

Scene.update() for classes

Bjucha

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

JonB

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

@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

JonB

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