How do you perform a removeSelf command properly?

I have a draggable object (my player) who’s sprite I need to remove and replace with a different sprite for a game-over/death animation. To do this, when the collision function happens(the one that “kills” the player) I remove the player sprite (using the removeSelf command), and tell the player-dead sprite to play. The only problem is, when the removeSelf command activates, my object’s x and y coordinates become nil, and this creates an error with the conditional statements in my code that require a value for these coordinates (the one’s that keep the player from going off-screen, ie. if player.x < 25 then player.x = 25). Any solutions? I’m not a coder, so I’m thinking I could be using wrong approach as well.

Thank you,
Steven [import]uid: 79394 topic_id: 14749 reply_id: 314749[/import]

Any ideas? I really need to solve this one.

Thanks,
Steven [import]uid: 79394 topic_id: 14749 reply_id: 54637[/import]

Don’t remove the sprite at death, just set isVisible to false. Remove it later when you don’t need any values from it anymore.

–Woof! [import]uid: 24607 topic_id: 14749 reply_id: 54651[/import]

I haven’t used a sprite sheet yet, but from what I just read it’s a regular Display Object.

So, what I would recommend is to use a Display Group for your player object (http://developer.anscamobile.com/content/displaynewgroup). The technical change is that this item will hold the x, y screen coordinates instead of the Sprite sheet and can/will contain the sprites. Next add both of your sprite sheets (life & death) to the Group object.

The result is that : 1. When you change x or y on the Group, all of the contained items will move with it., 2. you can use isVisible to enable the sprite sheet that you want at that time.

It’ll look something like this (I haven’t tested this code):

[lua]local spriteLife = sprite.newSpriteSheet(“sprites_living.png”, 100, 100)
local spriteDeath = sprite.newSpriteSheet(“sprites_dead.png”, 100, 100)

local group = display.newGroup()

– first item added is indexed as ‘group[1]’
group:insert( spriteLife )
group:insert( spriteDeath )

– turn them on/off as you need
group[1].isVisible = true
group[2].isVisible = false[/lua]

I used the Display Group exclusively as the basis of my object-oriented framework because it’s extremely useful for this exact reason. You can hold all of your object’s visual items inside of it and make them act as one unit.

cheers,
dmc

http://developer.anscamobile.com/code/dmc-corona-library [import]uid: 74908 topic_id: 14749 reply_id: 54674[/import]

Worked like a charm. Thanks, iwoof, I’d throw you a virtual bone if I had one:)

Steven [import]uid: 79394 topic_id: 14749 reply_id: 54676[/import]

This will be very helpful for changing sprite animations on the fly and keeping things organized as well. Thanks so much for putting it out there!

Cheers,
Steven
[import]uid: 79394 topic_id: 14749 reply_id: 54677[/import]