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.
Timer method for scene?
Bjucha last edited by ccc
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_actionfor 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
flight_714 last edited by
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.
[scene setup code]
self.timer = 100
self.timer -= 1
if self.timer <= 0:
self.timer = 100
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 last edited by
IIRC, it might be better to subtract down self.dt's, which would then account for frame rate differences.
Bjucha last edited by
Thx. Gonna see if I can get it to work