Changing the Image of an Object

I have an object that I wanted to have change its image when it detects a collision with another object. Every time I try to implement this the program crashes.

I have a function called newShape ( kind, health );

where kind of the shape time and health is one of three images for the shape.

I dont see a command or structure to directly change the image of an object, so I tried to have it first set the object place hold which is an array ( i.e object[i] ) to nil, or just completely remove itself, and then reload a new shape from the function of the type needed and with the same properties into a completely new object or place in the array.

But it still crashes. Does anyone know an effective way to do this, or know any tricks/tips of why it might be crashing?

Thanks

Are you using physics engine? And if so, do you also need to change the physics body when the image changes?

If you are using physics engine, and if you need to change the physics body when the image changes upon collision, it's a bit trickier than just changing an image.

Anyhow, you may want to provide a little more information. (But who knows, someone benevolent may come along and simply provide several case scenarios with assumptions along with answers to each possible situations you may be trying to work out.)

Naomi

you cant just change image of an object, you need to remove it first and then declare another in its place

If you're using the physics engine then as Naomi suggested and need to change the physical body as well it's a little trickier - you'd remove and readd the body BUT you would need to do it only once collision is resolved. (This sounds like why you may be getting that crash?)

Peach

Ah you know, the function is called in the middle of the collision function. I will test calling it as the collision is finished and see if that fixes it.

To Naomi, I have the newShape function to declare the shape as a physical body as it is called, so it's still being redeclared as a physical object.

Ill let yall know if that fixes it. Thanks so much

After collision is finished would that entail e.phase == "ended" or as an outside listener for say a change in the objects property.

I tried to call it during the e.phase == "ended" and it still crashed. I can remove the object no problem, even during the collision, but it is calling a new object after that is causing the problem.

I save the removed objects x, y, index, and then try to use that to replace the image with:

object[index] = newShape ( kind, x, y ); but that makes it crash. I doubt it's a problem with the newShape function as it is called by the user many times in the program.

The index should be fine, I store the "i" value of the array as the object is created as a property of the object so it can be called again later to reference its place in the array.

I have even tried creating a newShape as a completely different array to check if that was the problem. I think it has something to do with called the newShape function during the collision, but the debugger tells me nothing as the simulator crashes.

Sadface

views:1454 update:2011/11/2 21:34:51
corona forums © 2003-2011