Here is a comparison video:
As for the code, here is the one from the course:
Paddle:
function init(self)
msg.post(".","acquire_input_focus")
self.initPositionY = go.get_position().y
self.position = go.get_position()
end
function update(self, dt)
local sizeRaquette = go.get("#sprite", "size")
if self.position.x < sizeRaquette.x/2 then self.position.x = sizeRaquette.x/2 end
if self.position.x > 1280 - sizeRaquette.x/2 then self.position.x = 1280 -sizeRaquette.x/2 end
go.set_position(self.position)
end
function on_input(self, action_id, action)
self.position = vmath.vector3(action.x,self.initPositionY,0)
end
Ball:
function init(self)
self.currentState = "init"
self.velocity = vmath.vector3(1,1,0)
self.speed = 5
msg.post(".","acquire_input_focus")
end
function update(self, dt)
if self.currentState == "init" then -- SEE HERE FOR THE PROBLEM
local positionRaquette = go.get_position("raquette")
local sizeRaquette = go.get("raquette#sprite", "size")
local sizeBalle = go.get("balle#sprite", "size")
local y = sizeRaquette.y / 2 + sizeBalle.y / 2
local offset = vmath.vector3(0,y+1,0)
self.position = positionRaquette+offset
elseif self.currentState == "move" then
self.position = self.position + self.velocity * self.speed
end
go.set_position(self.position)
end
--[[ I did not put the rest because it concerns the collision of the walls and the start of the movement of the ball in space ]]
And what I have coded:
Paddle:
function init(self)
msg.post(".", "acquire_input_focus")
self.size = vmath.mul_per_elem(
go.get("#sprite", "size"),
go.get("#sprite", "scale")
)
self.ox = self.size.x/2
self.oy = self.size.y/2
self.pos = go.get_position()
end
function update(self, dt)
-- Screen border collision --
if self.pos.x > G_WIDTH-self.ox then
self.pos.x = G_WIDTH-self.ox
elseif self.pos.x < self.ox then
self.pos.x = self.ox
end
-- Set object position --
go.set_position(self.pos)
end
function on_input(self, action_id, action)
self.pos.x = self.pos.x + action.dx
end
Ball:
function init(self)
msg.post(".", "acquire_input_focus")
self.size = vmath.mul_per_elem(
go.get("#sprite", "size"),
go.get("#sprite", "scale")
)
self.ox = self.size.x/2
self.oy = self.size.y/2
self.vel = vmath.vector3(0,1,0)
self.speed = 200
self.move = false
self.query_move_timer = false
end
function update(self, dt)
local pos;
if self.move then
--[[ I do not put this passage to shorten because it is about the movement in space ]]
else
-- Timer count for moving request (quick tapping of the screen) --
if self.query_move_timer then
self.query_move_timer = self.query_move_timer + dt
end
-- Set position to player --
local player_pos = go.get_position("player")
local player_size = vmath.mul_per_elem(
go.get("player#sprite", "size"),
go.get("player#sprite", "scale")
)
pos = player_pos+vmath.vector3(
0,(player_size.y+self.size.y)/2,0
)
-- Set direction before start --
self.vel.x = (pos.x < G_H_WIDTH) and 1 or -1
end
go.set_position(pos)
end
Even if I wrote it very differently, at the level of the position of the ball I do not understand what makes this difference?