# Function thinks self is local

I have a function that behaves quite odd. Its a function to initialize my ballons speeds based on their color.

``````function Ballon:initSpeed()
if self.col == RED then
self.ballonSpeed = self.START_RED_SPEED

elseif self.col == BLUE then
self.ballonSpeed = self.START_BLUE_SPEED

elseif self.col == YELLOW then
self.ballonSpeed = self.START_YELLOW_SPEED
end
end
``````

The error is that the first self is nil.I have deduced that that the function thinks that self is a local variable because when I place the code where I would call the function, it works just fine so its something funky with the function. Maybe it doesn’t think it belongs to ballon. Below is the full code for my ballon class. Any ideas?

``````
Ballon = class()

RED = 0
BLUE = 1
YELLOW = 2
FIRST = RED
LAST = YELLOW

function Ballon:init(x,colour)

self.col = colour

self.START_BLUE_SPEED = 2
self.START_RED_SPEED = 10
self.START_YELLOW_SPEED = 5

if self.col == RED then
self.ballonColor = vec4(198, 53, 25, 123)

elseif self.col == BLUE then
self.ballonColor = vec4(43, 33, 223, 123)

elseif self.col == YELLOW then
self.ballonColor = vec4(202, 223, 33, 123)
end

self.initSpeed()

self.ballonX = x
self.ballonY = 10
self.ballonHeight = 150
self.ballonWidth = 100

end

function Ballon:initSpeed()
if self.col == RED then
self.ballonSpeed = self.START_RED_SPEED

elseif self.col == BLUE then
self.ballonSpeed = self.START_BLUE_SPEED

elseif self.col == YELLOW then
self.ballonSpeed = self.START_YELLOW_SPEED
end
end

function Ballon:draw()
-- Codea does not automatically call this method
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 and
pos.y >= self.ballonY - self.ballonHeight and
pos.x <= self.ballonX + self.ballonWidth and
pos.x >= self.ballonX - self.ballonWidth then

self.ballonSpeed = self.ballonSpeed + .5
self.ballonY = 10

ret_val = true
end

return ret_val
end

function Ballon:reset()
if self.col == RED then
self.ballonSpeed = self.START_RED_SPEED

elseif self.col == BLUE then
self.ballonSpeed = self.START_BLUE_SPEED

elseif self.col == YELLOW then
self.ballonSpeed = self.START_YELLOW_SPEED
end
self.ballonX = math.random(200, WIDTH - 50)
end

function Ballon:touched(touch)
-- Codea does not automatically call this method
end
``````

The colon strikes again. Replace self.initSpeed() with self:initSpeed(). A period means it’s calling the function with self as the instance specified in the first parameter. It’s thinking you’re going to call Ballon.initSpeed(self), but it says self is nil because you didn’t have self as the first parameter.

Thats such a silly mistake! Thanks