function init(self)
-- Speed of the player ship
local speed = 250
-- Input bindings
local move_left = hash("left")
local move_right = hash("right")
local fire = hash("fire")
-- Animation sets
local animation_idle = hash("forward")
local animation_left = hash("bank_left")
local animation_right = hash("bank_right")
--missile
local missile = hash("/missile")
end
function final(self)
msg.post(".", "acquire_input_focus")
self.direction = vmath.vector3()
self.current_animation = nil
self.can_fire = true
end
function update(self, dt)
-- update player position
local pos = go.get_position()
-- prevent the ship leaving the screen
if pos.x < 25 then
pos.x = 25
end
if pos.x > 935 then
pos.x = 935
end
-- set new position
go.set_position(pos + self.direction * speed * dt)
-- animate the ship
local animation = animation_idle
if self.direction.x > 0 then
animation = animation_right
elseif self.direction.x < 0 then
animation = animation_left
end
-- reset animation only if new direction started
if animation ~= self.current_animation then
msg.post("#sprite", "play_animation", { id = animation })
self.current_animation = animation
end
-- only continue to move the ship if the key is held
self.direction.x = 0
end
function on_input(self, action_id, action)
-- Set direction based on input
if action_id == move_left then
self.direction.x = -1
elseif action_id == move_right then
self.direction.x = 1
end
end
There’s an example of what the code looks like and this is the error message
ERROR:SCRIPT: main/player1.script:42: attempt to perform arithmetic on field ‘direction’ (a nil value)
stack traceback:
main/player1.script:42: in function <main/player1.script:29>