Someone can explain to me the role of this.

I’ve been trying to swapp images for 6 hours following this tutorial: https://coronalabs.com/blog/2013/11/26/tutorial-techniques-for-swapping-images/

I can successfully put all my images in the group following this code:

local ballGroup = display.newGroup() local balls = {} local ballImages = {    "images/red-ball.png",    "images/blue-ball.png",    "images/green-ball.png",    "images/yellow-ball.png",    "images/pink-ball.png"    } for i = 1, #ballImages do    balls[i] = display.newImageRect( ballGroup, ballImages[i], 128, 128 )    balls[i].x = display.contentCenterX    balls[i].y = display.contentCenterY    balls[i].isVisible = false end ballGroup.currentBall = 1 balls[ballGroup.currentBall].isVisible = true

But I can not make them change using this code in my widget button function event

local idx = t.currentBall balls[idx].isVisible = false idx = idx + 1 if ( idx \> #balls ) then    idx = 1 end balls[idx].isVisible = true t.currentBall = idx

I know t = event.target, Is written above in the “handleTouch” function event.

I moved my touch event into the create scene because I was having a lot of local and global variable errors.

Now I’m stuck in the balls[idx].

I know it’s a scope problem.

I only have 12 different color images that I would like to be able to change them when I touch 12 different widget buttons. Each widget button would have a different color assigned.

Thanks in advance!

I would not use that technique.  Just use an image fill (bitmap paint).
 
https://docs.coronalabs.com/daily/api/type/BitmapPaint/index.html
 
 
 
Here is some code using yours as a basis to demonstrate the concept

local ballGroup = display.newGroup() local ballImages = { "images/red-ball.png", "images/blue-ball.png", "images/green-ball.png", "images/yellow-ball.png", "images/pink-ball.png" } local ball = newRect( ballGroup, display.contentCenterX, display.contentCenterY, 128, 128 ) local testCount = 1 local function showItWorking() ball.fill = { type = "image", filename = ballImages[testCount] } testCount = testCount + 1 end timer.performWithDelay( 1000, showItWorking, 6 )

Note: I did not test that sample for syntax errors (‘compiled in my head only’), so if any errors occur simply read the console messages and fix them.

There is nothing specifically wrong with the technique in the article, but if all you need to do is change the image of a rectangle, use the fill code I demonstrated, but coded do do what you need.

That article was written before Corona supported bitmapPaint fills.  Corona is always getting better.

Ed’s suggestion for doing fills is a better way to go. You only have one display object to manage. But to address your scope problems, since we don’t see the entire scene, I’m going to speculate…

Take this block of code:

local ballGroup = display.newGroup() local balls = {} local ballImages = { "images/red-ball.png", "images/blue-ball.png", "images/green-ball.png", "images/yellow-ball.png", "images/pink-ball.png" }

And put it at the top of your scene outside of any scene:* function. I would put it at the very top after any “require” statements that you have and the statement to create the scene.

Have this block of code inside scene:create()

for i = 1, #ballImages do balls[i] = display.newImageRect( ballGroup, ballImages[i], 128, 128 ) balls[i].x = display.contentCenterX balls[i].y = display.contentCenterY balls[i].isVisible = false end ballGroup.currentBall = 1 balls[ballGroup.currentBall].isVisible = true

Then put your touch handler function above scene:create() but below the first block of code.

That should address your scope issues.

Rob

Thanks for the support. The api that you gave me was very helpful. I managed to do what I wanted.
As you know, I’m new at this, so instead of using tables I used the “bitmap paint” individually and it worked perfectly.
Thanks again for your support.

Thanks Rob

My mind is very clear right now. That solved the scope problem.

@Rob Miracle and @roaminggamer

you are amazing!!!

I would not use that technique.  Just use an image fill (bitmap paint).
 
https://docs.coronalabs.com/daily/api/type/BitmapPaint/index.html
 
 
 
Here is some code using yours as a basis to demonstrate the concept

local ballGroup = display.newGroup() local ballImages = { "images/red-ball.png", "images/blue-ball.png", "images/green-ball.png", "images/yellow-ball.png", "images/pink-ball.png" } local ball = newRect( ballGroup, display.contentCenterX, display.contentCenterY, 128, 128 ) local testCount = 1 local function showItWorking() ball.fill = { type = "image", filename = ballImages[testCount] } testCount = testCount + 1 end timer.performWithDelay( 1000, showItWorking, 6 )

Note: I did not test that sample for syntax errors (‘compiled in my head only’), so if any errors occur simply read the console messages and fix them.

There is nothing specifically wrong with the technique in the article, but if all you need to do is change the image of a rectangle, use the fill code I demonstrated, but coded do do what you need.

That article was written before Corona supported bitmapPaint fills.  Corona is always getting better.

Ed’s suggestion for doing fills is a better way to go. You only have one display object to manage. But to address your scope problems, since we don’t see the entire scene, I’m going to speculate…

Take this block of code:

local ballGroup = display.newGroup() local balls = {} local ballImages = { "images/red-ball.png", "images/blue-ball.png", "images/green-ball.png", "images/yellow-ball.png", "images/pink-ball.png" }

And put it at the top of your scene outside of any scene:* function. I would put it at the very top after any “require” statements that you have and the statement to create the scene.

Have this block of code inside scene:create()

for i = 1, #ballImages do balls[i] = display.newImageRect( ballGroup, ballImages[i], 128, 128 ) balls[i].x = display.contentCenterX balls[i].y = display.contentCenterY balls[i].isVisible = false end ballGroup.currentBall = 1 balls[ballGroup.currentBall].isVisible = true

Then put your touch handler function above scene:create() but below the first block of code.

That should address your scope issues.

Rob

Thanks for the support. The api that you gave me was very helpful. I managed to do what I wanted.
As you know, I’m new at this, so instead of using tables I used the “bitmap paint” individually and it worked perfectly.
Thanks again for your support.

Thanks Rob

My mind is very clear right now. That solved the scope problem.

@Rob Miracle and @roaminggamer

you are amazing!!!