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>
function final(self)
msg.post(".", "acquire_input_focus")
self.direction = vmath.vector3(0,0,0)
self.current_animation = nil
self.can_fire = true
end
Just tried it, and it gave me this
ERROR:SCRIPT: main/player1.script:40: attempt to perform arithmetic on field ‘direction’ (a nil value)
stack traceback:
main/player1.script:40: in function <main/player1.script:27>
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")
self.direction = vmath.vector3(0,0,0)
self.current_animation = nil
self.can_fire = true
end
ERROR:SCRIPT: main/player1.script:41: bad argument #2 to ‘__mul’ (number expected, got nil)
stack traceback:
[C] in function __mul
main/player1.script:41: in function <main/player1.script:28>
You’re defining a bunch of local variables in your init function. All of those are local to the init function, so they won’t be accessible in other functions. If those are going to stay the same for all players, then you should just move them outside of init() to the base level of the script. Things that may change for each instance should be put on ‘self’.