im trying to do the “War battles” tutorial and i can’t get the rocket’s shooting although i i have gone through the tutorial several times
here’s my script for player movement i think that i messed up something there
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
local angle = math.atan2(self.dir.y, self.dir.x)
local rot = vmath.quat_rotation_z(angle)
local props = { dir = self.dir }
factory.create("#rocketfactory", nil, rot, props)
end
self.input.x = 0
self.input.y = 0
self.moving = false
self.firing = false
end
function on_input(self, action_id, action)
if action_id == hash("up") then
self.input.y = 1
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
self.dir = vmath.normalize(self.input)
end
end
Ok, first thing’s first: do you see any errors in the console? Next: check the z-value of the rocket game object and the sprite. Could they be outside the default -1 to 1 range or below the tilemap?
function update(self, dt)
-- Add update code here
-- Remove this function if not needed
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
local angle = math.atan2(self.dir.y, self.dir.x)
local rot = vmath.quat_rotation_z(angle)
local props = {dir = self.dir}
factory.create("#rocketfactory", nil, rot, props)
end
self.input.x = 0
self.input.y = 0
self.moving = false
self.firing = false
end
One thing to keep in mind here is that the second argument is the position and when you pass in nil the spawned object will get the same position as the game object with the factory. That combined with the rocket sprite having a position of 0.8 in your first screenshot could mean that you end up outside the -1 to 1 range (can be changed using a custom render script) and thus not see the rocket.