Okay, my last code works as expected :
function init(self)
math.randomseed(os.time())
self.speed = 50
self.dir = vmath.vector3()
self.dir_bkp = vmath.vector3()
self.count = 2
end
function final(self)
-- Add finalization code here
-- Remove this function if not needed
end
function updateanimation(self)
if self.dir ~= self.dir_bkp then
if self.dir.y > 0 then
msg.post("#sprite", "play_animation", { id = hash("tank-up") })
elseif self.dir.y <= 0 then
msg.post("#sprite", "play_animation", { id = hash("tank-down") })
end
if self.dir.x > 0 then
msg.post("#sprite", "play_animation", { id = hash("tank-right") })
elseif self.dir.x <= 0 then
msg.post("#sprite", "play_animation", { id = hash("tank-left") })
end
end
end
function update(self, dt)
local position = go.get_position()
self.count = self.count - dt
if self.count < 0 then
self.dir = vmath.normalize(vmath.vector3(math.random(-50,50),math.random(-50,50),0))
self.count = 2
end
if position.x > 720 or position.x < 0 then
self.dir.x = self.dir.x * -1
elseif position.y > 720 or position.y < 0 then
self.dir.y = self.dir.y * -1
end
local movement = position + self.dir * self.speed * dt
go.set_position(movement)
updateanimation(self)
self.dir_bkp = self.dir
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)
-- Add input-handling code here
-- Remove this function if not needed
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end