Swapping image in object without error.

Hi trying to swap and image when user presses a button. Simple task but I cant seem to do without an error. Heres a snippet of my class. The issue lies with when I remove the original image, I think it removes the whole object. Please help. :wink:

[code]
function Balloon.new()

local balloon = display.newImageRect( “BalloonBasic.png”, 71, 89 )

balloon.id = “balloon”

balloon.x = 200
balloon.y = 200

physics.addBody( balloon, “dynamic”, { density=0.2, friction=1, bounce=0.0, radius=40 } )
balloon.rotation = 0
balloon.isFixedRotation = true

balloon.health = 100

function balloon:applyImpulse(wind, hotAir)
balloon:applyLinearImpulse( wind, hotAir, 20, balloon.y )

– this cause the error, how can I swap image???
balloon:removeSelf()
balloon = display.newImageRect( “balloon-flame.png”, 71, 89 )

end

function balloon:destroy()

balloon:removeSelf()
balloon = nil
end

return balloon

end

return Balloon
[/code] [import]uid: 118379 topic_id: 22359 reply_id: 322359[/import]

I have declared table at top :slight_smile: [import]uid: 118379 topic_id: 22359 reply_id: 89128[/import]

Hi

I wonder if “balloon-flame.png” is just the flame or a copy of the original balloon with the flame added

In the latter case you might want to make temporarily invisible the normal balloon setting it’s alpha to 0, and then destroy the balloon with the flame and restore the visibility of the original.

RaĂșl BeltrĂĄn [import]uid: 44101 topic_id: 22359 reply_id: 89148[/import]

thx for reply, i agree with your logic and its something that sounds good. I was thinking if I added the flame in only I would have to always update the position of the balloon with the flame rather than just swapping the image. I think my issue may be I need to return the display group rather then the display object, then update the groups position instead, this will solve it. Will have a go and let u know how it goes. [import]uid: 118379 topic_id: 22359 reply_id: 89283[/import]

You could make this easy for yourself by using the movieclip library : http://developer.anscamobile.com/reference/index/movieclipnewanim

Or better yet, spritesheets : http://developer.anscamobile.com/reference/index/sprite [import]uid: 84637 topic_id: 22359 reply_id: 89314[/import]

Thanks I did manage to solve it the long way, wish id inspected movieclip a little more, thanks for insight.

Does using spritesheets have much performance impact? [import]uid: 118379 topic_id: 22359 reply_id: 89454[/import]