Multigrab question and issue.

Official forum for the Chipmunk2D Physics Library.
tetraweb
Posts: 31
Joined: Wed Feb 13, 2013 8:46 pm
Contact:

Re: Multigrab question and issue.

Post by tetraweb »

My touchesEnded looks like this:

Code: Select all

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
{
	for(UITouch *touch in touches) {
           ChipmunkGrab *grab = [_multiGrab endLocation:[self convertTouchToNodeSpace:touch]];
           ChipmunkShape *shape = [grab grabbedShape];
           if (shape) {
              BasicCrate *aCrate = shape.data;
              if (aCrate.beingDragged) [aCrate endDragging];
           }
       }
}
The exception is

Code: Select all

 *** -[BasicCrate retain]: message sent to deallocated instance 0x558310 
and the stack trace looks like this:

Code: Select all

#0	0x35a4e870 in ___forwarding___ ()
#1	0x359a9650 in _CF_forwarding_prep_0 ()
#2	0x3366cf94 in objc_retain ()
#3	0x33679be4 in objc_retainAutoreleasedReturnValue ()
#4	0x0010c094 in -[PlayScene ccTouchesEnded:withEvent:] at /Users/gregt/SCG/Projects/SpellTower - Base2.1rc0a_ncp/Base2.1rc0a_ncp/PlayScene.m:300
#5	0x359a63fc in -[NSObject performSelector:withObject:withObject:] ()
#6	0x000b5a34 in -[CCTouchDispatcher touches:withEvent:withTouchType:] at /Users/gregt/SCG/Projects/SpellTower - Base2.1rc0a_ncp/Base2.1rc0a_ncp/libs/cocos2d/Platforms/iOS/CCTouchDispatcher.m:313
#7	0x000b60be in -[CCTouchDispatcher touchesEnded:withEvent:] at /Users/gregt/SCG/Projects/SpellTower - Base2.1rc0a_ncp/Base2.1rc0a_ncp/libs/cocos2d/Platforms/iOS/CCTouchDispatcher.m:366
#8	0x000b37a0 in -[CCGLView touchesEnded:withEvent:] at /Users/gregt/SCG/Projects/SpellTower - Base2.1rc0a_ncp/Base2.1rc0a_ncp/libs/cocos2d/Platforms/iOS/CCGLView.m:349
Greg
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Multigrab question and issue.

Post by slembcke »

Ah. Yep. I figured something like that was happening.

So the ChipmunkMultiGrab is retaining the shape that is grabbed, but the shape does not retain it's .data property to avoid retain cycles. It's just an assigned property since the idea is that whatever you put in the .data property probably has a retained reference to the Chipmunk object. When the BasicCrate object is removed from the game, it's being deallocated since you are removing all of the retained references to it. Then when you release your finger, you are attempting to access the deallocated object. This is a pretty common problem when working with Objective-C's retain counting. You sort of have to design around it.

I think the simplest possible solution is to keep an array of the game objects you are dragging to retain them. Add the game object (to be clear, the BasicCrate and not the shape) to the array in touchesBegan, and remove them in touchesEnded. You could also avoid removing objects where .beingDragged is true if that doesn't break your gameplay.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tetraweb
Posts: 31
Joined: Wed Feb 13, 2013 8:46 pm
Contact:

Re: Multigrab question and issue.

Post by tetraweb »

Thanks so much for your clear help. The dragging array is how I'll go.

Regarding the other problem I had, with the cpHardAssert; if I disable that should I just return nil if the _grabs array is empty?

It's a kids game I'm making, and I don't want a crash if they have their hands all over it during a scene transition, which I know is likely from my own kids.

Thanks again for the help you have already provided many times.

Greg
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Multigrab question and issue.

Post by slembcke »

Yeah, I suppose it should just return nil otherwise.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tetraweb
Posts: 31
Joined: Wed Feb 13, 2013 8:46 pm
Contact:

Re: Multigrab question and issue.

Post by tetraweb »

Bringing up my own old thread, I am now having issues with multi touch when my play tester kids grab a shape with two fingers,when they let go and the object was removed as a result, and removed from my array of grabbed objects, I crash on the second touch going through.

Code: Select all

	for(UITouch *touch in touches) {
            ChipmunkGrab *grab = [_multiGrab endLocation:[self convertTouchToNodeSpace:touch]]; 
crash-> ChipmunkShape *shape = [grab grabbedShape];
            if (shape) {
                if ([shape.data isMemberOfClass:[BasicCrate class]]) {
                    BasicCrate *aCrate = shape.data;
                    if (aCrate.beingDragged) {
                        [aCrate endDragging];
                        [draggingThings removeObjectIdenticalTo:aCrate];
                    }
                } 
Trying to figure out my flow, without adding a second copy of the object to the array when touched with a second finger, seems jacky and unreliable.

Grge
Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests