That looks pretty good to me. A few notes:
-
Updating the position of a game object can be done in two ways, either by calling
go.set_position()
or by “tweening” (or animating) the “position” property. When you want to set the position from input or when you have logic tied to the movement your solution is good. -
The
local speed
on the first line is unused so you can safely drop that. -
You might want to look into using the variable “dt” that is sent to
update()
. It contains the delta time since last frame. Usually this is 1/60 (0,0166666) seconds if you run at 60 frames per seconds. Your speedself.speed
is currently expressed as “6 pixels per frame” which works fine as long as you don’t drop any frames. It is good practice to express movement in units per second instead so:
function init(self)
...
self.speed = 360 -- 360 pixels per second
...
end
local function goToRight(self, dt)
local p = go.get_position()
p.x = p.x + self.speed * dt -- <---- multiply with the delta t value
...
end
function update(self, dt)
goToRight(self, dt)
end