Iterating through shapes
-
- Posts: 6
- Joined: Tue Jun 16, 2009 11:49 am
- Contact:
Iterating through shapes
Hey everybody,
I've got a method that checks a given point for a specific cpBody by iterating through all bodies of the space and comparing the positions. Unfortunately I found out that I have to call cpSpaceRemoveShape and cpShapeFree to get rid of the Sprite and the assigned shape.
Could anyone tell me how to iterate through the shapes of a space (space->activeShapes I guess) so I could compare it's body property to the given body at the specific position to find out which space belongs to that body?
Thank you very much.
Have a nice day.
I've got a method that checks a given point for a specific cpBody by iterating through all bodies of the space and comparing the positions. Unfortunately I found out that I have to call cpSpaceRemoveShape and cpShapeFree to get rid of the Sprite and the assigned shape.
Could anyone tell me how to iterate through the shapes of a space (space->activeShapes I guess) so I could compare it's body property to the given body at the specific position to find out which space belongs to that body?
Thank you very much.
Have a nice day.
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Iterating through shapes
Code: Select all
cpSpaceHashEach(cpSpaceHash *hash, cpSpaceHashIterator func, void *data);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 6
- Joined: Tue Jun 16, 2009 11:49 am
- Contact:
Re: Iterating through shapes
Hey - thank you very much for your fast answer. I just tried it the way you suggested and it works - I get the shape that I want to remove ... unfortunately it crashes (BAD_ACCESS @ cpSpaceHashRemove) if I want to remove the shape...
I found this way to do it somewhere on the net:
Do you have any idea, why I just can't call cpSpaceRemoveShape anyway?
Thank you very much!
I found this way to do it somewhere on the net:
Code: Select all
cpShape *b = body->data;
cpSpaceRemoveBody(space, body);
cpBodyFree(body);
cpSpaceRemoveShape(space, b);
cpShapeFree(b);
Thank you very much!
-
- Posts: 6
- Joined: Tue Jun 16, 2009 11:49 am
- Contact:
Re: Iterating through shapes
Hmm ... I just tried it without the cpSpaceRemoveShape - only the cpShapeFree - that seems to work ... but I have no idea what it does
-
- Posts: 6
- Joined: Tue Jun 16, 2009 11:49 am
- Contact:
Re: Iterating through shapes
oh... oh oh ... ... forget it ... it just didn't crash directly ... ehm ... ouch ... any further suggestions how I could remove this?
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Iterating through shapes
You can't remove or free items from the same list while you are iterating it. This is a no-no with most any data structure in any language. You'll have to make a list of the objects you want to remove and remove them separately.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 6
- Joined: Tue Jun 16, 2009 11:49 am
- Contact:
Re: Iterating through shapes
I'm sorry - you're great ;-D ... it works (even though it's in a loop ...
Everything works fine - I just got two methods of creating shapes - and made that
body->data = shape
change only in the wrong method ... I'm an idiot ... but now it works great - I'm so happy thx
Everything works fine - I just got two methods of creating shapes - and made that
body->data = shape
change only in the wrong method ... I'm an idiot ... but now it works great - I'm so happy thx
-
- Posts: 6
- Joined: Tue Jun 16, 2009 11:49 am
- Contact:
Re: Iterating through shapes
Oh ... correction ... for anyone who tries that ;-D ... it's not in a loop ... after that ->data change I don't need the loop anymore
-
- Posts: 33
- Joined: Mon Mar 30, 2009 3:06 am
- Contact:
Re: Iterating through shapes
slembcke, you should give Chipmunk a slogan:
"Don't try and delete pointers that you're still iterating through, and maintain your own goddamn lists!"
Or, at least, whatever that would be in Latin
You have the patience of a saint my friend!
"Don't try and delete pointers that you're still iterating through, and maintain your own goddamn lists!"
Or, at least, whatever that would be in Latin
You have the patience of a saint my friend!
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
Who is online
Users browsing this forum: No registered users and 29 guests