Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Error when using array

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?

Comments

  • dave1707dave1707 Mod
    edited February 2017 Posts: 7,612

    @Eustace Here's your code with changes. If you have questions about what I did, just ask.

    function setup()   
        colorArray = {}
        colorArray[1] = color(255, 255, 0)
        colorArray[2] = color(255, 0, 255)
        colorArray[3] = color(0, 255, 255)
        colorArray[4] = color(0, 32, 255)
        colorArray[5] = color(255, 0, 32)
        colorArray[6] = color(0, 255, 32)
    
        balls={}
        for ci=1,#colorArray do
            table.insert(balls,Ball(ci))
        end
    end
    
    function draw()
        background(0)    
        for a,b in pairs(balls) do
            b:draw()
        end
    end
    
    Ball=class()
    
    function Ball:init(ci)
        self.x = math.random(50,WIDTH-50)
        self.y = math.random(50,HEIGHT-50)
        self.ci=ci
    end
    
    function Ball:draw()
        fill(colorArray[self.ci])
        ellipse(self.x, self.y, 100)
    end
    
  • Posts: 2,020

    It looks like you're not storing ci. You pass it into the initialiser, but you don't store it as an instance variable, so it will pass out of scope at the end of the initialiser, and by the time you call draw it will be nil. You could add a print statement to the start of draw for debugging purposes, to check that this is what's happening.

  • @Eustace This line in Ball.draw appears to be the culprit.

    fill(colorArray[ci][1],colorArray[ci][2],colorArray[ci][3],255)
    

    It looks like you meant to set self.ci = ci in Ball.init and then use self.ci in Ball.draw instead of ci.

    Did you know that Codea has a specific color type which you could use with fill like this instead. It may help you to simplify your code a little bit.

    local col = color(255,0,0,255)
    fill(col)
    
  • Thanks for all the help. I'm happy to learn that my problem wasn't in how I was making and using the array, but a problem with variable scope. Once I made those changes (adding self.ci = ci, and replacing ci with self.ci in the fill) everything worked the way I expected.
    I didn't use the color type because I will also be making separate changes to the alpha values in a different method. It seems easier and clearer to just do it all in the fill line. Each time I make a Ball object it will be assigned the next color in the array, but then it will stay that color. Each time the ball is drawn, it will be drawn with a different alpha value (the balls fade over time).
    I am studying your code, dave1707, and if I am understanding it correctly, you are using #colorArray to get the length of the array, then making a table of Balls, one of each color. You then draw them by taking each element of the array, assigning the index to a and the object to b, then drawing b. Amirite?

  • dave1707dave1707 Mod
    Posts: 7,612

    @Eustace That's correct. Since it looked like you were creating a class for Ball, I figured you would create several balls. So I just created a table of balls for the amount of colors you defined.

Sign In or Register to comment.