I have a function to create a random number for my x value whenever I call it but it oddly doesn’t reconize the ballonWidth class vairable when I try to set the parameters for the random number. Here is the function. I treid swapping the ballon width for a global vairbal and that worked fine. Any clues?
function Ballon:randomX()
return math.random(400, WIDTH - self.ballonWidth)
end
Here is the full ballon class
Ballon = class()
function Ballon:init(colour)
self.col = colour
self.START_BLUE_SPEED = 2
self.START_RED_SPEED = 10
self.START_YELLOW_SPEED = 5
dawidth = 100
self:initChar()
end
function Ballon:initChar()
if self.col == RED then
self.ballonSpeed = self.START_RED_SPEED
self.ballonColor = vec4(198, 53, 25, 123)
self.ballonX = self:randomX()
self.ballonY = 10
self.ballonHeight = 150
self.ballonWidth = 100
elseif self.col == BLUE then
self.ballonSpeed = self.START_BLUE_SPEED
self.ballonColor = vec4(43, 33, 223, 123)
self.ballonX = self:randomX()
self.ballonY = 10
self.ballonHeight = 150
self.ballonWidth = 10
elseif self.col == YELLOW then
self.ballonSpeed = self.START_YELLOW_SPEED
self.ballonColor = vec4(202, 223, 33, 123)
self.ballonX = self:randomX()
self.ballonY = 10
self.ballonHeight = 150
self.ballonWidth = 100
end
end
function Ballon:draw()
pushStyle()
fill( self.ballonColor.x,
self.ballonColor.y,
self.ballonColor.z,
self.ballonColor.w )
ellipse(self.ballonX,
self.ballonY,
self.ballonWidth,
self.ballonHeight)
popStyle()
end
function Ballon:update()
-- Moves ballon up
self.ballonY = self.ballonY + self.ballonSpeed
-- Makes the ballon lope up the screen
if self.ballonY - self.ballonHeight >= HEIGHT then
self.ballonY = 0 - self.ballonHeight
end
end
function Ballon:hittCheck(pos)
local ret_val = false
if pos.y <= self.ballonY + self.ballonHeight/2 and
pos.y >= self.ballonY - self.ballonHeight/2 and
pos.x <= self.ballonX + self.ballonWidth/2 and
pos.x >= self.ballonX - self.ballonWidth/2 then
self.ballonSpeed = self.ballonSpeed + .5
self.ballonY = 10
ret_val = true
end
return ret_val
end
function Ballon:reset()
self.col = math.random(FIRST,LAST)
if self.col == RED then
self.ballonSpeed = self.START_RED_SPEED
self.ballonColor = vec4(198, 53, 25, 123)
self.ballonX = self:randomX()
self.ballonY = 10
self.ballonHeight = 150
self.ballonWidth = 100
elseif self.col == BLUE then
self.ballonSpeed = self.START_BLUE_SPEED
self.ballonColor = vec4(43, 33, 223, 123)
elseif self.col == YELLOW then
self.ballonSpeed = self.START_YELLOW_SPEED
end
end
function Ballon:randomX()
-- return math.random(400, WIDTH - self.ballonWidth)
return math.random( WIDTH/2, WIDTH - dawidth)
end
function Ballon:touched(touch)
-- Codea does not automatically call this method
end