Need Help with Variable and Functions

I made a ball game, and it uses a function to make balls where you drag your finger.

That was successful, but I’m making a button to change the ball color to something random.

I changed the ball variable to _G. global, but when I click the random color butoom, it only changes one ball to that color, not all of the spawned balls. I think its using the last ball spawned.

Hi @shacx.77,

Welcome to Corona. You’ll probably need to post some code for staff or other developers to help you. When doing so, please surround the code with “lua” tags for clarity:

[lua] --code [/lua]

In any case, you should really avoid global variables in Lua. They cause all kinds of problems and there are always better ways to structure your code without using any globals whatsoever. Please check out this tutorial for some tips on this topic:

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Take care,

Brent

Hi @shacx.77,

Welcome to Corona. You’ll probably need to post some code for staff or other developers to help you. When doing so, please surround the code with “lua” tags for clarity:

[lua] --code [/lua]

In any case, you should really avoid global variables in Lua. They cause all kinds of problems and there are always better ways to structure your code without using any globals whatsoever. Please check out this tutorial for some tips on this topic:

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Take care,

Brent