Hi,
First sorry for this long post, but I'm having very hard days dealing with a problem destructing Chipmunk objects & Sprites. The problem only appears (ocasionally) when I touch objects that are in screen, if I simulate the touch via altering by code a property (collision_type) in the object created everything works fine.
Also I get problems in the arbiters (having an excessive velocity NAN in the ApplyImpulse, the arbiter don't getting the the body a and b pairs), camera locate problems in CocosNode, hangs destroing Sprites in the cleanup method from CocosNode, glClear hangs .. But they are only only found if I touch the objects, so I'm thinking that my touches code have any desing problem.
Note: I use Cocos 2d 0.7.2 release and added cpMouse that I found in the Chipmunk forum. Game it's not Multitouch. I'm holding all my moving Sprites into an C array (misSprites) done in this way:
typedef struct st_Sprites {
cpShape *Shape;
unsigned int sprite;
int vida;
} st_Sprites;
st_Sprites misSprites[MAX_SPRITES];
In the GameLayer I set:
Game.h
@interface JUGAR : Layer
{
cpMouse *mouse;
...
Game.m Init
mouse = nil;
Here my methods for handling touches:
- (void)ccTouchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
if (mouse)
{
UITouch *myTouch = [touches anyObject];
CGPoint posicion = [myTouch locationInView: [myTouch view]];
posicion = [[Director sharedDirector] convertCoordinate: posicion];
cpMouseMove(mouse, CGPointMake(posicion.x, posicion.y));
if(mouse->grabbedBody == nil)
{
cpMouseGrab(mouse, 0);
}
}
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
cpShape *mishape;
Sprite *miSprite;
UITouch *mitouch = [touches anyObject];
CGPoint posicion = [mitouch locationInView: [mitouch view]];
posicion = [[Director sharedDirector] convertCoordinate: posicion];
for(int i=0;i<cont_Sprites;i++)
{
mishape = (cpShape*) misSprites[i].Shape;
miSprite = mishape->data;
if (miSprite)
{
CGSize size = miSprite.texture.contentSize;
CGRect bounds = CGRectMake(miSprite.position.x - size.width/2 - 50,
miSprite.position.y - size.height/2 - 50, size.width + 50, size.height + 50);
if (CGRectContainsPoint(bounds, posicion))
{
// for collide with another objects
mishape->collision_type = 1;
mouse = cpMouseNew(space);
cpMouseMove(mouse, CGPointMake(posicion.x, posicion.y));
// patch it's worth?
//http://www.slembcke.net/forums/viewtopic.php?f=1&t=380&p=1750&hilit=touch#p1750
cpMouseMove(mouse, CGPointMake(posicion.x, posicion.y));
mouse->moved = cpvzero;
// end patch
cpMouseGrab(mouse, 0);
break;
}
}
}
}
And another two methods:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesCancelled:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mouse)
{
cpMouseFree(mouse);
mouse = nil;
}
}
Do you see any problem? (I hope yes)
Anything that must be changed?
This is only what I need to finish my game. Any advice? Any help will be greatly appreciated cause I'm very lost with this errors.
Thanks in advance.
Pablo