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
I got a bit confused by the speed and angle. In your code speed is the horizontal movement and angle is the vertical movement. I expected angle to actually describe an angle of travel for the ball at a defined speed.
The four functions to detect edge collisions is in my book a clear case of DRY (don’t repeat yourself). For every function you make a call to go.get_position(). You could also reduce the return value from:
if p.x >= worldRightLimit then
return true
end
return false
to:
return p.x >= worldRightLimit
Personally I’d probably cut the whole script down to:
local W = tonumber(sys.get_config("display.width"))
local H = tonumber(sys.get_config("display.height"))
function init(self)
self.direction = vmath.vector3(1, 1, 0)
self.speed = 200
self.size = go.get("#sprite", "size")
end
function update(self, dt)
local p = go.get_position()
p = p + (self.direction * self.speed * dt)
go.set_position(p)
-- left or right edge?
if p.x <= (self.size.x / 2) or p.x >= (W - self.size.x / 2) then
self.direction.x = -self.direction.x
end
-- top or bottom edge?
if p.y <= (self.size.y / 2) or p.y >= (H - self.size.y / 2) then
self.direction.y = -self.direction.y
end
end
yeah you are right. Sorry about that. when i have started to write this scrip i was considering use angle to change the ball direction. but after many unsuccessful tests I handled to did it using Y values. but i had forget to update the variable name.
This is a super logic hint. Thankyou.
Did you write a Defold Book? Please share the link.
wow a super hint. with this i can dynamically get sprite size values. but there is a problem.
If I change the ‘scale’ at Game Object [go.get(“#sprite”, “size”)] continues returning the sprite size (image size) and not the the image size updated by scale values. Is there a way to get correct image size values (after changed the scale value on Game Object)?
Haha. thanks for this new english expression. Was fun.
This instruction didn’t work to get the GO scale, but with your hint I searched at API and find go.get_scale(), it is not the same thing but works. because i’m using the same scale to X and Y.
Thanks for all. Now i’m going to study Factories component and Detect Collisions. See you.