Forum Archive

Timer method for scene?

Bjucha

Hello again

In my "game" I have added powerups that when player catches on green lasers will shoot from the spaceship. However they only fire once and I want them to keep fire for about 10 sec. I have tried using a counter that counts down when collision with powerups appear. But that made pythonista crash. So is there a way to add a timer or similar for functions in scene?
In my mind there should be a way when using the run_action for the "laser" to make it last about 10 sec

Here is the code for the laser function:

    def multi_laser(self):
        mlasers = SpriteNode('spc:LaserGreen13', parent=self)
        mlasers.position = self.ship.position + (10, 30)
        mlasers.z_position = -1
        actions = [A.move_by(500, self.size.h, 1.3 * self.speed), A.remove()]
        mlasers.run_action(A.sequence(actions))
        self.mlasers.append(mlasers)

    def laser_multi(self):
        mlasers = SpriteNode('spc:LaserGreen13', parent=self)
        mlasers.position = self.ship.position + (10, 30)
        mlasers.z_position = -1
        actions = [A.move_by(-500, self.size.h, 1.3 * self.speed), A.remove()]
        mlasers.run_action(A.sequence(actions))
        self.mlasers.append(mlasers)

As you can see I made one function for each of the two lasers (only way for me to get it to work)

Im greatful for any help or tips

//
B

flight_714

Full disclosure: I'm a perpetual noob both with Pythonista and programming in general, but I have wrestled with this issue quite a few times tooling around with game ideas.

The simplest solution is to set up a manual timer that uses the Scene update method to subract a number from a starting point.

Ex:
[scene setup code]
self.timer = 100

def update(self):
self.timer -= 1
if self.timer <= 0:
self.timer = 100
do_something()

Obviously, once this makes sense you can make a timer class and have multiple instances running for different events.

Alternatively, you can mess around with threading, but I've found that this route is best for simpler tasks.

Obviously, frame rate is an issue and I'm sure the more experienced folks around here will have a more elegant solution.

JonB

IIRC, it might be better to subtract down self.dt's, which would then account for frame rate differences.

Bjucha

Thx. Gonna see if I can get it to work