How about this slight modification to the original -- should prevent the lasers from disappearing when touch ends. Also, keeps track of the lasers for use in hit detection
from scene import *
import sound
import random
import math
class MyScene (Scene):
def setup(self):
self.background_color = 'midnightblue'
self.ship = SpriteNode('spc:PlayerShip1Orange')
self.ship.position = self.size / 2
self.add_child(self.ship)
self.lasers=[]
def update(self):
x, y, z = gravity()
pos = self.ship.position
pos += (x * 15, y * 15)
# Don't allow the ship to move beyond the screen bounds:
pos.x = max(0, min(self.size.w, pos.x))
pos.y = max(0, min(self.size.h, pos.y))
self.ship.position = pos
def touch_began(self, touch):
laser_animation=Action.repeat_forever(Action.sequence(Action.call(self.fire_laser), Action.wait(0.1) ) )
self.run_action(laser_animation, 'laser')
def touch_ended(self,touch):
self.remove_action('laser')
def remove_laser(self,laser):
del self.lasers[laser]
def fire_laser(self):
a=random.random()*500-250
laser = SpriteNode('spc:LaserBlue9', position=self.ship.position, z_position=-1, parent=self)
self.lasers.append(laser)
laser.run_action(Action.sequence(Action.move_by(a, 1000), Action.remove(), Action.call(lambda:self.remove_laser(laser))))
sound.play_effect('arcade:Laser_1')