I want to have a plane move on screen and drop off “minions” as it moves. I have it working, but it never stops. I can’t delete the object when it hits the end of screen nor have it stop after dropping a certain # of minions. I have this script attached to the plane as it flys from left to right.
go.property(“acceleration”, 200)
go.property(“max_speed”, 400)
go.property(“right_left”, 1)
local spawn_timer = nil
local i = 1
local numMinions = 3
function init(self)
– movement input
self.input = vmath.vector3()
-- the current direction of movement
self.direction = vmath.vector3()
-- the current speed (pixels/second)
self.speed = 0
--spawn minions
if i<numMinions then
spawn_timer = timer.delay(.25, true, spawnMinion)
i=i+1
end
end
function update(self, dt)
self.input.x = self.right_left
self.direction = self.input
– increase speed
self.speed = self.speed + self.acceleration * dt
– cap speed
self.speed = math.min(self.speed, self.max_speed)
-- move the game object
local p = go.get_position()
p = p + self.direction * self.speed * dt
go.set_position(p)
-- reset input
self.input = vmath.vector3()
end
function spawnMinion(self)
local p = go.get_position()
local pos = vmath.vector3(p.x, p.y, 0)
local minion_id = factory.create("#minionfactory", pos, nil, nil, .4)
end
function final(self)
– Cancel the timer when the script is deleted
timer.cancel(spawn_timer)
go.delete()
end