In old project i create collisions like in this question: why collision not working , cocos2d v3? . It ran ok, however when I created a new project something was wrong. I don't know what I did differently this time. Please help me.
-(void)addAIAction:(int)i{
if (!arrAI) {
arrAI = [[NSMutableArray alloc]init];
}
CCSprite *sprite = [CCSprite spriteWithImageNamed:@"res/gu_button.png"];
sprite.position = [GameConfig convertPoint:ccp(viewSize.width, 160)];
sprite.physicsBody.collisionType=@"aiCollision";
sprite.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:sprite.contentSize.width/2 andCenter:ccp(sprite.contentSize.width/2, sprite.contentSize.height/2)];
sprite.physicsBody.type = CCPhysicsBodyTypeDynamic;
[_physicsNode addChild:sprite];
[arrAI addObject:sprite];
}
// -----------------------------------------------------------------------
-(void)update:(CCTime)delta{
for (CCSprite *sp in arrPlayer) {
sp.position= ccp(sp.position.x+playerSpeed, sp.position.y);
}
for (CCSprite *sp in arrAI) {
sp.position= ccp(sp.position.x-aiSpeed, sp.position.y);
}
implement delegate :
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair playerCollision:(CCNode *)nodeA aiCollision:(CCNode *)nodeB{
NSLog(@"Coll.");
- (id)init
{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);
viewSize=CGSizeMake([GameConfig converPoint:480], [GameConfig converPoint:320]);
_physicsNode = [CCPhysicsNode node];
_physicsNode.collisionDelegate =self;
_physicsNode.debugDraw =YES;
_physicsNode.gravity=ccp(0, 0);
please let me know why " ccPhysicsCollisionBegin " never call thanks everyone
You've given no evidence to say your setting the delegate for the physics body, so therefore the ccPhysicsCollisionBegin method may not be called.
I don't know V3, but I'm guessing your going to need to add:
sprite.phisicsBody.deligate = self;
before you call [_physicsNode addChild:sprite];
in your addAIAction:
.