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

Any ideas? I really need to solve this one.

Thanks,
Steven

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!

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):

1
2
3
4
5
6
7
8
9
10
11
12
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

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

Steven

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

views:1669 update:2011/9/26 8:07:09
corona forums © 2003-2011