This is my first attempt at making and using an array, and I need help to figure out what I’m doing wrong. I want to use an array of RGB values to cycle an object through different colors. In main setup I put:
colorIndex = 1
colorArray = {}
colorArray[1] = {255, 255, 0}
colorArray[2] = {255, 0, 255}
colorArray[3] = {0, 255, 255}
colorArray[4] = {0, 32, 255}
colorArray[5] = {255, 0, 32}
colorArray[6] = {0, 255, 32}
…to create an iterator variable and an array of values. I create a Ball object with this code (also in main):
--make a new ball
balls[ballIndex] = Ball(math.random(50, WIDTH - 50), HEIGHT - 200, colorIndex)
…the Ball object is constructed and drawn with this code:
function Ball:init(x, y, ci)
– you can accept and set parameters here
self.x = x
self.y = y
self.rate = 1
end
function Ball:draw()
–method to draw ball
pushStyle()
fill(colorArray[ci][1], colorArray[ci][2], colorArray[ci][3], 255)
ellipse(self.x, self.y, 100)
popStyle()
end
…but when I try to run this I get the error “attempt to index a nil value” that traces from the fill line in Ball:draw()
What am I doing wrong?