omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    Timer method for scene?

    Pythonista
    time timer scene
    3
    4
    3623
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Bjucha
      Bjucha last edited by ccc

      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

      1 Reply Last reply Reply Quote 0
      • flight_714
        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.

        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.

        1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

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

          1 Reply Last reply Reply Quote 0
          • Bjucha
            Bjucha last edited by

            Thx. Gonna see if I can get it to work

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors