【发布时间】:2014-06-29 18:36:58
【问题描述】:
我的屏幕底部有一个可以通过倾斜设备来倾斜的对象。它可以被随机下落的物体击中,当这种情况发生时,它会显示“游戏结束!”。但是,如果它撞到墙壁上(如下面的代码所示),它将显示“游戏结束!”消息也是如此。我怎样才能解决这个问题?我希望它能够在没有任何事情发生的情况下撞到墙上。 (我也会为联系人代理添加当前代码)。
墙壁代码:
//Left Wall
SKNode *node = [SKNode node];
node.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(0.0f, 0.0f, 1.0f, CGRectGetHeight(self.frame))];
[self addChild:node];
// Right wall
node = [SKNode node];
node.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(CGRectGetWidth(self.frame) - 1.0f, 0.0f, 1.0f, CGRectGetHeight(self.view.frame))];
[self addChild:node];
联系代表代码:
#pragma mark contact delegate
- (void)didBeginContact:(SKPhysicsContact *)contact {
//Create two physics body objects
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else {
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if ((firstBody.categoryBitMask & dodgerCategory) != 0) {
[secondBody.node removeFromParent];
self.takenHits++;
}if (self.takenHits >= 1) {
//Game Over
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Game Over."
message:@"You have been hit!" delegate:self
cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
self.paused = YES;
}
}
【问题讨论】:
标签: ios objective-c sprite-kit collision