Things are not growing. Please help!

Here is my code. I want it to be a circle that gradually gets bigger, but it is not working. Every time it tell it to get bigger (last line, it happens every tenth of  a second) it gives me an error saying that spawnedObjects[1].radius is a nil value. However, it can print out spawnedObjects[1].radius on lines 10 and 15 just fine, so it seems that It DOES recognize that the radius is 100, but when I set it as the radius of a new circle, it interprets it as nil. Why is this?

local spawnedObjects = {} local function spawnBubble(bubbleSize) local bubble = display.newCircle(0, 0, bubbleSize) bubble:setFillColor(.5, .5, .5) bubble.radius = bubbleSize -- add bubble to spawnedObjects table for tracking purposes spawnedObjects[#spawnedObjects+1] = bubble print(spawnedObjects[1].radius) end local function circleGrow() --spawnedObjects[1].radius = spawnedObjects[1].radius + 1 print( spawnedObjects[1].radius ) spawnedObjects[1] = display.newCircle(0, 0, spawnedObjects[1].radius) end spawnBubble(100) timer.performWithDelay( 100, circleGrow, 0)

It’s nil because you are creating a new object called spawnedObjects[1] which does not have a field called radius yet. 

Try this instead:

local function circleGrow() local storedRadius = spawnedObjects[1].radius + 1 print( storedRadius ) spawnedObjects[1] = display.newCircle(0, 0, storedRadius) end

or alternatively just scale the object (though it won’t remain as sharp depending on how big it grows):

local function circleGrow() spawnedObjects[1].xScale = spawnedObjects[1].xScale + 0.1 spawnedObjects[1].yScale = spawnedObjects[1].yScale + 0.1 end

or use a transition:

transition.to(spawnedObjects[1], {time = 2000, xScale = 10})

Hope that helps.

p.s your title made me laugh, it sounds like the name of a sex-ed video.

You could also use (object).path.radius instead of (object).radius … (object).radius is a “custom” field that you’ve created and thus you have to manage it yourself (as Alan noted).  (object).path.radius is managed by Corona.

Thanks to Alan QuickTiz for the help, but your first suggested code does not fix my program, unfortunately. I am still getting the error “main.lua:15 attempt to perform arithmetic on field ‘radius’ (a nil value)” I do not see why I should be getting this, as the fixed program now prints 100, and then 101. Does anybody know how I could fix this.

To jbp1: I like your idea, and yes, that would likely work. However, I am still confused why my code does not work. I know there might be a slightly better way, but I am confused because my code should be working and it is not.

Thank you for the replies and I am open to more advice.  :slight_smile:

Can you post your code as it looks after adding my changes, so I can see if something has been overlooked?

Something else I’ve just noticed is that you are going to have lots of circles drawn one on top of the other. This is because you are not scaling the original, you are creating a new larger circle in place of the original.  Adding this to your circleGrow() function will stop that from happening:

spawnedObjects[1]:removeSelf() --remove prev object spawnedObjects[1] = display.newCircle(0, 0, spawnedObjects[1].radius) --draw new one

I know what the nil error is. It’s because when you create the new object, you don’t give that a property called “radius”. This fixes it:

local function circleGrow() local storedRadius = spawnedObjects[1].radius + 1 print( storedRadius ) spawnedObjects[1]:removeSelf() spawnedObjects[1] = display.newCircle(0, 0, storedRadius) spawnedObjects[1].radius = storedRadius end 

To be honest, unless you really need to do this whole “replace old object with new object” for some reason, I think the method you are using is probably the “least-good” way of doing it. You would be better off altering the properties of the object that already exists, using either the .path.radius property that jbp1 suggested, or scaling the object as I suggested. 

The way you are doing it now just creates extra objects needlessly (and since the old object wasn’t being deleted you would end up using more and more memory), and the new object does not retain the properties of the old one, as you have discovered.

Code is working now, thank you very much, Alan!

It’s nil because you are creating a new object called spawnedObjects[1] which does not have a field called radius yet. 

Try this instead:

local function circleGrow() local storedRadius = spawnedObjects[1].radius + 1 print( storedRadius ) spawnedObjects[1] = display.newCircle(0, 0, storedRadius) end

or alternatively just scale the object (though it won’t remain as sharp depending on how big it grows):

local function circleGrow() spawnedObjects[1].xScale = spawnedObjects[1].xScale + 0.1 spawnedObjects[1].yScale = spawnedObjects[1].yScale + 0.1 end

or use a transition:

transition.to(spawnedObjects[1], {time = 2000, xScale = 10})

Hope that helps.

p.s your title made me laugh, it sounds like the name of a sex-ed video.

You could also use (object).path.radius instead of (object).radius … (object).radius is a “custom” field that you’ve created and thus you have to manage it yourself (as Alan noted).  (object).path.radius is managed by Corona.

Thanks to Alan QuickTiz for the help, but your first suggested code does not fix my program, unfortunately. I am still getting the error “main.lua:15 attempt to perform arithmetic on field ‘radius’ (a nil value)” I do not see why I should be getting this, as the fixed program now prints 100, and then 101. Does anybody know how I could fix this.

To jbp1: I like your idea, and yes, that would likely work. However, I am still confused why my code does not work. I know there might be a slightly better way, but I am confused because my code should be working and it is not.

Thank you for the replies and I am open to more advice.  :slight_smile:

Can you post your code as it looks after adding my changes, so I can see if something has been overlooked?

Something else I’ve just noticed is that you are going to have lots of circles drawn one on top of the other. This is because you are not scaling the original, you are creating a new larger circle in place of the original.  Adding this to your circleGrow() function will stop that from happening:

spawnedObjects[1]:removeSelf() --remove prev object spawnedObjects[1] = display.newCircle(0, 0, spawnedObjects[1].radius) --draw new one

I know what the nil error is. It’s because when you create the new object, you don’t give that a property called “radius”. This fixes it:

local function circleGrow() local storedRadius = spawnedObjects[1].radius + 1 print( storedRadius ) spawnedObjects[1]:removeSelf() spawnedObjects[1] = display.newCircle(0, 0, storedRadius) spawnedObjects[1].radius = storedRadius end 

To be honest, unless you really need to do this whole “replace old object with new object” for some reason, I think the method you are using is probably the “least-good” way of doing it. You would be better off altering the properties of the object that already exists, using either the .path.radius property that jbp1 suggested, or scaling the object as I suggested. 

The way you are doing it now just creates extra objects needlessly (and since the old object wasn’t being deleted you would end up using more and more memory), and the new object does not retain the properties of the old one, as you have discovered.

Code is working now, thank you very much, Alan!