Changing Display Images

Hey Guys,

Stupid question but I cant seem to figure it out properly. I have a sensor that converts wood objects to stone. I can do this simply by changing the physics of the items from dynamic to static but I’m having issues changing the images to relate to the change

E.G. Wooden Block image changing to Stone Block image

Is there a way to do this other then create two separate images and then have them on the same x,y values and just change the alpha readings ? Would pref like to be able to jist change the image,

E.G.

[code]for i = GameObjects.numChildren,1,-1 do
local child = GameObjects[i]
if child.myName == “woodBlock” then
child.??? = ???( “images/stoneBlock.png”, 35, 35)
end
end[code] [import]uid: 40417 topic_id: 9390 reply_id: 309390[/import]

Theres probably a simple way to do this like you are asking for, but I’m using this

Object = {}
Object.image = display.newImage(blah)

Object.x = 1
Object.y = 1

Then if I want to change the image I just go Object.image = display.newImage(something) and all the properties are saved.
[import]uid: 34945 topic_id: 9390 reply_id: 34319[/import]

Instead of changing the alpha, you can alter the isVisible property.
I think the movieclip.lua module works like this - it creates a display group and loads all the images into it, only making visible the one you want (so that might be a quick way of achieving what you want).

Currently the quickest way of changing images is to use sprites and spritesheets, although that’s a little more complicated to setup, so if your needs are relatively simple and you don’t have framerate issues, I’d suggest looking into movieclips. [import]uid: 46639 topic_id: 9390 reply_id: 34335[/import]

Sorry should have been more clear “GameObjects” is a display Group containing “wood”, “stone” and “powerup” items, with that would’nt I need to then add another image for all wood ones and change it that way. Each Game Object is setup as follows.

-- Setup Gravity 1 --   
GameItem1 = display.newImageRect( "images/gravityChange.png", 30, 30 )  
GameItem1.x = 240; GameItem1.y = 15  
GameItem1.myName = "gravityChange"  
  
physics.addBody( GameItem1, "kinematic", physicsData:get("GravityChange"))   
GameItem1:addEventListener( "touch", onTouch )  
  
GameObjects:insert (GameItem1)  
GameItem1.HasBeenMoved = false  

Can I just add another values like

GameItem1.stoneImage = display.newImageRect( "images/stoneBlock.png", 30, 30 ) GameItem1.woodImage = display.newImageRect( "images/woodBlock.png", 30, 30 )

and then call which one is dsiplayed at any one time like

 GameItem1 = GameItem1.stoneImage 

I don’t think you can do that, but I really like innominata’s solution. You could combine that with what you want to do and get most of the way there. For example:

GameItem1 = {}  
GameItem1.image = display.newImage("images/gravityChange.png")  
GameItem1.x = 30  
GameItem1.y = 30  
GameItem1.stoneImage = "stoneBlock.png"  
GameItem1.woodImage = "woodBlock.png"  

Then when you want to change to the stone image, you’d to something like:

GameItem1.image = display.newImage("images/" .. GameItem1.stoneImage )  

That’s untested code, but it looks right. :slight_smile:

Jay
[import]uid: 9440 topic_id: 9390 reply_id: 34341[/import]

This is an old tread but one that I believe is interesting to a lot of people new to Corona, so I decided to revive it with my own, related, questions.

Here is the code where in my game I create a number of display objects and attach the same image to them all:

-- Create enemies  
for i=1,enemyMax do  
 enemies[i] = display.newImage("assets/graphics/saucer.png")  
 enemies[i].x = display.contentWidth + 50  
 enemies[i].y = - 100  
 physics.addBody(enemies[i], "kinematic", {bounce = 0})  
 enemies[i].isFixedRotation = true  
 enemies[i].name = "enemy"  
 enemies[i].life = 0  
 enemies[i].bullet = 0  
 enemies[i].hit = 0  
 enemies[i].speed = 0  
 enemies[i].path = 0  
 enemies[i].shoot = 0  
 enemies[i].var = 0  
 enemies[i].count = 0  
 enemies[i].count2 = 0  
 enemies[i].multi = 0  
 enemies[i].fall = 0  
 enemiesLayer:insert(enemies[i])  
end  

What I would like to do is to change the image of a specific instance of ‘enemies’, say enemies[10].

However, I do not understand in what way the table-value ‘image’ is related with the display objects actual image in the last example above.

If I simply write this line:

enemies[i].image = display.newImage("assets/graphics/saucer.png")  

instead of this one (like in the example):

enemies[i] = display.newImage("assets/graphics/saucer.png")
And later do this:

enemies[10].image = display.newImage("assets/graphics/enemybullet.png")  

I get an error saying: “attempt to index field ‘?’ {a nil value)”, related to the new line.

Can anyone tell me what I am missing here?
[import]uid: 99737 topic_id: 9390 reply_id: 93070[/import]