【发布时间】:2023-03-30 08:49:01
【问题描述】:
您好,我一直在使用 SpriteKit 框架制作游戏,并在 2 个对象发生碰撞时设置了碰撞位掩码。其中一个对象,假设对象 A,可以有 2 种状态:黑色或正常,因此当两个对象碰撞并且 A 对象处于正常状态时,它会添加一个点,但是当两个对象碰撞并且 A 对象处于黑色时表示游戏结束。这段代码在 iOS 7 上运行良好,但是当我在 iOS 8 上运行它时,如果 A 对象状态为黑色,它的行为就像它处于正常状态并添加一个点。为什么会这样? iOS 7 和 8 的代码不同吗?请任何人帮助我,这是代码:
-(void)didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody;
SKPhysicsBody *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}else {
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (firstBody.categoryBitMask == objectACategory && secondBody.categoryBitMask == objectBCategory) {
NSLog(@"OBJECT A CAUGHT");
[firstBody.node removeFromParent];
[GameState sharedInstance].score++;
_scoreLabel.text = [NSString stringWithFormat:@"%d", [GameState sharedInstance].score];
_scoreLabel_2.text = [NSString stringWithFormat:@"%d", [GameState sharedInstance].score];
gameUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound " ofType:@"wav"]];
_gameSound = [[AVAudioPlayer alloc] initWithContentsOfURL:gameUrl error:nil];
_gameSound.delegate = self;
[_gameSound play];
if ([[firstBody.node.userData valueForKey:@"Black"] boolValue]) {
[self removeActionForKey:origamiFallKey];
NSLog(@"YOU LOSE");
[self gameOver];
[self runAction:[SKAction sequence:@[
[SKAction waitForDuration:0.5],
[SKAction performSelector:@selector(removeWhenLose) onTarget:self]]]];
}
}
为了设置状态,我在添加 objectA 的方法中使用了这段代码:
// Black
if(arc4random_uniform(6) == 0) {
_objectA.texture = [SKTexture textureWithImageNamed:@"blackImage.png"];
_objectA.physicsBody.collisionBitMask = objectACategory;
_objectA.userData = [[NSMutableDictionary alloc] init];
[_objectA.userData setValue:@YES forKey:@"Black"];
blackObjectA = YES;
}
【问题讨论】:
-
我想知道将
[_objectA.userData setValue:@YES forKey:@"Black"];切换到[_objectA.userData setValue:@(YES) forKey:@"Black"];是否可以解决您的问题并在iOS7 和iOS8 上运行 -
不,它仍然无法正常工作:/
-
我想是时候发挥创意了。在您检查 Black 并查看其中的内容之前,我会先注销 firstBody.node.userData。
标签: ios sprite-kit collision-detection categories bitmask