Hello guys
I’m still studying Defold and i finished a new mission. So it is time to ask for help to improve my code or Defold usage.
If you dont know the first post click here to visit it.
Project Configuration:
info about my project:
Window: 540x435
Ball Objetc: 128x128
Mission
Do a ball move through the canvas from any direction to any direction, bouncing when touch any of four edges of canvas.
My Game object script. (it is working. but im sure that can be improved) thank you guys!
Gif of project:
Game Object Code
local WorldWidth = tonumber(sys.get_config("display.width"))
local WorldHeigth = tonumber(sys.get_config("display.height"))
function init(self)
msg.post(".", "acquire_input_focus")
self.speed = 360
self.angle = 360
self.myWidth = 64
self.myHeight = 64
end
local function atRightEdge(self)
local p = go.get_position()
local worldRightLimit = WorldWidth - self.myWidth/2
if p.x >= worldRightLimit then
return true
end
return false
end
local function atLeftEdge(self)
local p = go.get_position()
local worldLeftLimit = self.myWidth/2
if p.x <= worldLeftLimit then
return true
end
return false
end
local function atTopEdge(self)
local p = go.get_position()
local worldTopLimit = WorldHeigth -self.myHeight/2
if p.y >= worldTopLimit then
return true
end
return false
end
local function atBotEdge(self)
local p = go.get_position()
local worldTopLimit = self.myHeight/2
if p.y <= worldTopLimit then
return true
end
return false
end
local function changeDirection(self)
if atRightEdge(self) or atLeftEdge(self) then
self.speed = self.speed * -1
end
if atTopEdge(self) or atBotEdge(self) then
self.angle = self.angle * -1
end
end
local function move(self,dt)
local p = go.get_position()
p.x = p.x + self.speed * dt
p.y = p.y + self.angle * dt
go.set_position(p)
end
function update(self, dt)
move(self, dt)
changeDirection(self)
end