The basics: a pointer/reference being orphaned...

I’m used to programming in RealStudio. I’m new to LUA, and I’m having trouble grasping the basics/concept.

consider the following code:

local myImage myImage = display.newImage( "red.jpg", system. ResourceDirectory, (math.random (0,320)), (math.random (0, 240)),true ) myImage = display.newImage( "blue.jpg", system. ResourceDirectory, (math.random (0,320)), (math.random (0, 240)),true )

Now, after the code has executed, everything I am used to tells me I should have one image pointed to by myImage. What I in fact have is two images.
Why doesn’t line 3 *change* myImage to be “blue.jpg”, instead it leaves “red.jpg” and creates a new instance with “blue.jpg”, thereby orphaning the original image.

So, once all 3 lines are executed, how do I reference the first image, to make it invisible for example?
How do I change the first image, rather than create a new image?

[import]uid: 74957 topic_id: 26667 reply_id: 326667[/import]

You can’t. Use a different variable name or store the images in a table. Once loaded, a newImage is immutable except for its display properties.

[lua]local myImage = {}
myImage[1] = display.newImage( “red.jpg”, system. ResourceDirectory, (math.random (0,320)), (math.random (0, 240)),true )
myImage[2] = display.newImage( “blue.jpg”, system. ResourceDirectory, (math.random (0,320)), (math.random (0, 240)),true )

myImage[1].isVisible = true
myImage[2].isVisible = false

local function someFunction()
for i=1,#myImage do
myImage[i].isVisible = not myImage[i].isVisible
end
end [import]uid: 44647 topic_id: 26667 reply_id: 108062[/import]

This is because myImage is not the only reference to those images, and assigning onto myImage makes myImage reference something else, but doesn’t change the original object (it still exists, you just can’t now use the myImage reference to change it).
Both images exist in the scene, and you are changing what myImage references, but the renderer doesn’t use local references to determine what it’s drawing. Corona is also holding references to both the red and blue images, and those internal references are what’s used to render out the scene on any given frame.

To clear off an internal reference you can use either display.remove(myImage) or myImage:removeSelf(), and then the image will be cleared from the scene, and your reference is ready to be reassigned. Otherwise, Toby2’s solution is a good one. [import]uid: 134101 topic_id: 26667 reply_id: 108075[/import]