ลองครั้งแรกที่ Chipmunk
การไม่ได้รับการลงทะเบียนการตรวจจับการชนกันคือปัญหา
รหัสของฉัน:
@implementation MainPlayScene
{
CCPhysicsNode *_physics;
CCNode *MyPhysicsBody;
CCNode *bottomBody;
}
+ (instancetype)scene
{
return [[self alloc] init];
}
- (instancetype)init
{
// Apple recommend assigning self with supers return value, and handling self not created
self = [super init];
if (!self) return(nil);
_physics = [CCPhysicsNode node];
_physics.debugDraw = YES;
[self addChild:_physics z:1];
/// BOTTOM
CGRect bottomRect = CGRectMake(0, 0, [CCDirector sharedDirector].viewSize.width, 10);
bottomBody = [CCNode node];
bottomBody.physicsBody = [CCPhysicsBody bodyWithPolylineFromRect:bottomRect cornerRadius:0];
bottomBody.physicsBody.collisionCategories = @[@"Bottom"];
bottomBody.physicsBody.type = CCPhysicsBodyTypeStatic;
[_physics addChild:bottomBody];
/// MyBody to bounce around
MyPhysicsBody = [CCSprite spriteWithImageNamed:@"MyBody-64x64-24.png"];
MyPhysicsBody.position = ccp((self.contentSize.width/2),(self.contentSize.height/2));
MyPhysicsBody = [CCNode node];
MyPhysicsBody.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, MyPhysicsBody.contentSize.height,MyPhysicsBody.contentSize.width} cornerRadius:0];
MyPhysicsBody.physicsBody.collisionCategories = @[@"MyBody"];
[_physics addChild:MyPhysicsBody z:150];
self.userInteractionEnabled = YES;
return self;
}
ตรวจจับเหตุการณ์การสัมผัสและใช้แรงกับร่างกายฟิสิกส์เพื่อให้กระเด้งขึ้นและลงบนร่างกายส่วนล่าง
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CCLOG(@"Touch Detected");
[MyPhysicsBody.physicsBody applyImpulse:ccp(0, 300.f)];
}
ตอนนี้ฉันพยายามตรวจจับการชนกันที่ "ด้านล่าง" แต่ไม่มีการบันทึกอะไรเลย แม้ว่าฉันจะเห็นว่าบรรทัดการแก้ไขข้อบกพร่องของวัตถุ 2 ชิ้นสัมผัสกันก็ตาม
/// try onCollisionEnter first ... nothing
-(void)onCollisionEnter:(CCNode *)entity collisionPair:(CCPhysicsCollisionPair *)pair
{
if ([entity.physicsBody.collisionCategories isEqual: @"Bottom"]) {
CCLOG(@"Hit bottomBody");
}
}
/// try ccPhysicsCollisionBegin pair ... nothing
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair MyBody:(CCNode *) MyBody Botton:(CCNode *)Bottom
{
CCLOG(@"Hit bottomBody");
return TRUE;
}
แน่นอนว่าฉันพลาดบางสิ่งที่สำคัญไปที่นี่…
ความช่วยเหลือใด ๆ ที่ชื่นชมมาก!
ขอบคุณ