In the (CCLayer*)GameSceneLayer, I'm adding a table to the space like this:
Code: Select all
-(id) init
{
if( (self=[super init])) {
/*
bla bla code
*/
CGPoint TableAPos = CGPointMake(400, 300);
TableSprite *aTableSprite = [self AddTableAtLocation:TableAPos withBottleNum:2 withGlassNum:1 withCardNum:0];
/*
bla bla code
*/
}
-(TableSprite*)AddTableAtLocation:(CGPoint)location withBottleNum:(NSInteger)numBottles withGlassNum:(NSInteger)glassNum withCardNum:(NSInteger)numCards {
TableSprite *newTableSprite = [[TableSprite alloc] initWithFrameName:@"Table"
atLocation:location
inSpace:space
staticBody:NO
withParentNode:self withBottles:numBottles withGlasses:glassNum withCards:numCards];
/*
bla bla code
*/
return newTableSprite;
}
Code: Select all
- (id)initWithFrameName:(NSString *)fileName
atLocation:(CGPoint)location
inSpace:(ChipmunkSpace*)theSpace
staticBody:(BOOL)isStatic
withParentNode:(CCNode*)parentNode
withBottles:(NSInteger)numBottles
withGlasses:(NSInteger)numGlasses
withCards:(NSInteger)numCards {
if (self = [super initWithFrameName:fileName atLocation:location inSpace:theSpace staticBody:isStatic withParentNode:parentNode]) {
/*
Bla bla code putting new objects like glasses, bottles and playing cards on the table
*/
}
return self;
}
Code: Select all
- (id)initWithFrameName:(NSString *)fileName atLocation:(CGPoint)location inSpace:(ChipmunkSpace*)theSpace staticBody:(BOOL)isStatic withParentNode:(CCNode*)parentNode {
if(self = [super init]) {
sprite = [CCSprite spriteWithSpriteFrameName:fileName];
sprite.anchorPoint = [[PhysEdShapeCache sharedShapeCache] anchorPointForShape:fileName];
////////////////////
// The shapesArray will contains every shapes from the body we are going to get next step
NSMutableArray *newArray = [[NSMutableArray alloc] init];
self.shapesArray = newArray;
[newArray release];
/////////////////////
// Here we get the shape designed with PhysicsEditor from Andreas Loew
ChipmunkCocosBody *newBody = [[PhysEdShapeCache sharedShapeCache] createBodyWithName:fileName withData:sprite inSpace:theSpace staticBody:isStatic];
// And we assign it to a pointer declared in class header
self.body = newBody;
body.pos = location;
// Then we add the body's shapes to our previously initialized array
[shapesArray addObjectsFromArray:body.shapes];
//////////////////
// Changing the shape's group here will be working while running the game
// The purpose is to make the tables no longer reacting to a certain collision
for (id aShape in shapesArray) {
ChipmunkShape *theShape = (ChipmunkShape*)aShape;
theShape.group = [NSNumber numberWithInt:2];
}
//////////////////
// With or without this line, it works, so far
[theSpace reindexShapesForBody:body];
spritesArray = [[NSMutableArray alloc] initWithObjects:sprite, nil];
body.syncedNodes = spritesArray;
[parentNode addChild:sprite];
}
return self;
}
Actually, I'd like to say "I want 3 tables, A, B and C, and I only want the table C to ignore collisions".
So, deleting the for loop from the PhysicSprite class init method, and back in my GameSceneLayer, I just add the for loop after I have created my tables:
Code: Select all
-(id) init
{
if( (self=[super init])) {
/*
bla bla code
*/
CGPoint TableAPos = CGPointMake(400, 300);
TableSprite *aTableSprite = [self AddTableAtLocation:TableAPos withBottleNum:2 withGlassNum:1 withCardNum:0];
///////////////////
for (id aShape in aTableSprite.shapesArray) {
ChipmunkShape *theShape = (ChipmunkShape*)aShape;
theShape.group = [NSNumber numberWithInt:2];
}
// With or without this line, it works, so far
[theSpace reindexShapesForBody:aTableSprite.body];
///////////////////////
/*
bla bla code
*/
}
This is my brainwasher of the moment, hope you like it :_)