我编写了一个sprite kit游戏,我有8个数字,它们在屏幕顶部产生并落到底部。每个数字都有不同的设置,例如,如果调用数字1,它会为分数加1,如果调用5则会删除生命。现在每当数字与船只接触时就会发生这种情况,但不是调用它所指的那个方法而是调用第一个方法,所以它在得分++时冷却。这是我的代码,希望你能理解它。
- (void)didBeginContact:(SKPhysicsContact *)contact
{
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 & shipCategory) != 0 &&
(secondBody.categoryBitMask & DonutCategory) != 0)
{
//score
score ++;
scorelabel.text = [NSString stringWithFormat:@"%d",score];
//highscore
if (score > HighScore) {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:score] forKey:@"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];
highscorelabel.text = [NSString stringWithFormat:@"%.f",HighScore];
}
}
else if ((firstBody.categoryBitMask & shipCategory) != 0 &&
(secondBody.categoryBitMask & PizzaCategory) != 0)
{
//score
score ++;
scorelabel.text = [NSString stringWithFormat:@"%d",score];
//highscore
if (score > HighScore) {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:score] forKey:@"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];
highscorelabel.text = [NSString stringWithFormat:@"%.f",HighScore];
}
}
else if ((firstBody.categoryBitMask & shipCategory) != 0 &&
(secondBody.categoryBitMask & ChocolateCategory) != 0)
{
//score
score ++;
scorelabel.text = [NSString stringWithFormat:@"%d",score];
//highscore
if (score > HighScore) {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:score] forKey:@"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];
highscorelabel.text = [NSString stringWithFormat:@"%.f",HighScore];
}
}
else if ((firstBody.categoryBitMask & shipCategory) != 0 &&
(secondBody.categoryBitMask & SoftCategory) != 0)
{
//score
score ++;
Life ++;
scorelabel.text = [NSString stringWithFormat:@"%d",score];
//highscore
if (score > HighScore) {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:score] forKey:@"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];
HighScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];
highscorelabel.text = [NSString stringWithFormat:@"%.f",HighScore];
}
}
else if ((firstBody.categoryBitMask & shipCategory) != 0 &&
(secondBody.categoryBitMask & AppleCategory) != 0)
{
Life--;
lifelabel.text = [NSString stringWithFormat:@"%d",Life];
if(Life <= 0) {
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
SKScene * gameOverScene = [[GameOverScene alloc] initWithSize:self.size];
[self.view presentScene:gameOverScene transition: reveal];
}
}
else if ((firstBody.categoryBitMask & shipCategory) != 0 &&
(secondBody.categoryBitMask & GrapeCategory) != 0)
{
//lifes
Life--;
lifelabel.text = [NSString stringWithFormat:@"%d",Life];
if(Life <= 0) {
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
SKScene * gameOverScene = [[GameOverScene alloc] initWithSize:self.size];
[self.view presentScene:gameOverScene transition: reveal];
}
}
else if ((firstBody.categoryBitMask & shipCategory) != 0 &&
(secondBody.categoryBitMask & OrangeCategory) != 0)
{
//lifes
Life--;
lifelabel.text = [NSString stringWithFormat:@"%d",Life];
if(Life <= 0) {
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
SKScene * gameOverScene = [[GameOverScene alloc] initWithSize:self.size];
[self.view presentScene:gameOverScene transition: reveal];
}
}
else if ((firstBody.categoryBitMask & shipCategory) != 0 &&
(secondBody.categoryBitMask & BananaCategory) != 0)
{
//lifes
Life--;
lifelabel.text = [NSString stringWithFormat:@"%d",Life];
if(Life <= 0) {
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
SKScene * gameOverScene = [[GameOverScene alloc] initWithSize:self.size];
[self.view presentScene:gameOverScene transition: reveal];
}
}
}
这是船舶和1号物理
-(void)addShip
{
//initalizing spaceship node
ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
[ship setScale:0.5];
ship.zRotation = - M_PI / 2;
//Adding SpriteKit physicsBody for collision detection
ship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ship.size];
ship.physicsBody.categoryBitMask = shipCategory;
ship.physicsBody.dynamic = YES;
ship.physicsBody.contactTestBitMask = DonutCategory | PizzaCategory | ChocolateCategory | SoftCategory | AppleCategory | GrapeCategory | OrangeCategory | BananaCategory;
ship.physicsBody.collisionBitMask = 0;
ship.physicsBody.usesPreciseCollisionDetection = YES;
ship.name = @"ship";
ship.position = CGPointMake(260,30);
actionMoveRight = [SKAction moveByX:-30 y:0 duration:.2];
actionMoveLeft = [SKAction moveByX:30 y:0 duration:.2];
[self addChild:ship];
}
- (void)shoot1 //donut
{
// Sprite Kit knows that we are working with images so we don't need to pass the image’s extension
Donut = [SKSpriteNode spriteNodeWithImageNamed:@"1"];
[Donut setScale:0.15];
// Position the Donut outside the top
int r = arc4random() % 300;
Donut.position = CGPointMake(20 + r, self.size.height + Donut.size.height/2);
Donut.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Donut.size];
Donut.physicsBody.categoryBitMask = DonutCategory;
Donut.physicsBody.dynamic = YES;
Donut.physicsBody.contactTestBitMask = shipCategory;
Donut.physicsBody.collisionBitMask = 0;
Donut.physicsBody.usesPreciseCollisionDetection = YES;
// Add the Dount to the scene
[self addChild:Donut];
// Here is the Magic
// Run a sequence
[Donut runAction:[SKAction sequence:@[
// Move the Dount and Specify the animation time
[SKAction moveByX:0 y:-(self.size.height + Donut.size.height) duration:5],
// When the Dount is outside the bottom
// The Dount will disappear
[SKAction removeFromParent]]]];
}
我不太了解这些类别!
static const uint32_t shipCategory = 0x1 << 1;
static const uint32_t DonutCategory = 0x1 << 2;
static const uint32_t PizzaCategory = 0x1 << 2;
static const uint32_t ChocolateCategory = 0x1 << 2;
static const uint32_t SoftCategory = 0x1 << 2;
static const uint32_t AppleCategory = 0x1 << 2;
static const uint32_t GrapeCategory = 0x1 << 2;
static const uint32_t OrangeCategory = 0x1 << 2;
static const uint32_t BananaCategory = 0x1 << 2;
答案 0 :(得分:2)
除船舶以外的所有类别的位掩码均相同,因此您的第一个if条件始终为真。
将您的位掩码更改为彼此不同:
static const uint32_t shipCategory = 1;
static const uint32_t DonutCategory = 2;
static const uint32_t PizzaCategory = 4;
static const uint32_t ChocolateCategory = 8;
static const uint32_t SoftCategory = 16;
static const uint32_t AppleCategory = 32;
static const uint32_t GrapeCategory = 64;
static const uint32_t OrangeCategory = 128;
static const uint32_t BananaCategory = 256;
确保整数是2的幂。这是为了您对bitmasks了解不多的可读性。
位掩码是32位,因此您最多可以使用32个类别(2 31 ) - 将来,您可能希望开始使用按位运算符来轻松反映高数字位掩码,正如这里的另一个提示。因此,扩展您自己的代码,与上面相同的值将是:
static const uint32_t shipCategory = 0x1 << 0; // 1
static const uint32_t DonutCategory = 0x1 << 1;
static const uint32_t PizzaCategory = 0x1 << 2;
static const uint32_t ChocolateCategory = 0x1 << 3;
static const uint32_t SoftCategory = 0x1 << 4;
static const uint32_t AppleCategory = 0x1 << 5;
static const uint32_t GrapeCategory = 0x1 << 6;
static const uint32_t OrangeCategory = 0x1 << 7;
static const uint32_t BananaCategory = 0x1 << 8; // 256
答案 1 :(得分:0)
您的所有类别都是相同的值,因此它们都会给出相同的结果。
改为
enum Category
{
shipCategory = 0x1, // 00000001
donutCategory = shipCategory << 1, // 00000010
pizzaCategory = donutCategory << 1, // 00000100
chocolateCategory = pizzaCategory << 1, // 00001000
// ... and so on ...
};