i was followin the defold’s tutorial and i found this error. Somebody help pls
function init(self)
msg.post(".", "acquire_input_focus")
self.moving = false
self.firing = false
self.input = vmath.vector3()
self.dir = vmath.vector3(0, 1, 0)
self.speed = 50
end
function final(self)
msg.post(".", "release_input_focus")
end
function update(self, dt)
if self.moving then
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
end
if self.firing then
factory.create("#rocketfactory") -- [1]
end
self.input.x = 0
self.input.y = 0
self.moving = false
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end
function on_input(self, action_id, action) -- [14]
if action_id == hash("up") then
self.input.y = 1 -- [15]
elseif action_id == hash("down") then
self.input.y = -1
elseif action_id == hash("left") then
self.input.x = -1
elseif action_id == hash("right") then
self.input.x = 1
elseif action_id == hash("fire") and action.pressed then
self.firing = true
end
if vmath.length(self.input) > 0 then
self.moving = true -- [16]
self.dir = vmath.normalize(self.input) -- [17]
end
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end
i was copying the turorial’s code and the rockets doesnt appear. Can somebody explain me where’s the mistake?
You need to destroy the rockets after they have lived long enough otherwise the buffer of allowed objects at once gets full.
If you keep a table of rockets as you create them then you can manage each rocket’s lifespan from the manager. Otherwise each rocket should go.delete() itself after so long / it hits a target.
3 Likes
There are a few things to mention here:
- As @pkeod mentioned, the sprite buffer fills up when you create lots of game objects and don’t destroy them. The tutorial later goes through destruction of the rockets in the section “Making the rockets explode”.
Normally you would not hit this limit since you spawn one rocket per key press, but you have missed one crucial detail, the self.moving = false
line at the end of update()
.
function update(self, dt)
if self.moving then
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
end
if self.firing then
factory.create("#rocketfactory") -- this line will spawn each frame "self.firing" is true.
end
self.input.x = 0
self.input.y = 0
self.moving = false
self.firing = false -- you MUST reset "self.firing" or 60 rockets will spawn each second.
end
- Since the game object buffer fills up you know that objects are actually spawned. The reason you don’t see them is most likely that the contain graphics with locations that is not visible on screen. Check that the rocket sprites are set at position 0, 0, 0. Chances are that you have a Z position of your sprites that cause the sprites to be rendered outside of the -1 to 1 Z depth interval that the default render script draws within.
5 Likes