2014-01-22 70 views
1

我編寫了一個精靈套件遊戲,我有8個數字,它們在屏幕的頂部產生並落到底部。每個號碼都有不同的設置,例如,如果調用數字1,則將數字加1,如果調用數字5,則刪除一條生命。現在,只要號碼與船舶接觸,就會發生這種情況,但不是調用它的意思就是調用第一種方法,所以它在冷卻得分++時。這是我的代碼,希望你能理解它。Sprite Kit保持首先打電話如果聲明

- (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; 
+1

什麼做你的類常量是什麼樣子? – molbdnilo

+0

我對Sprite Kit一無所知,但是我可以看到'categoryBitMask'是一個位掩碼,所以使用'<'操作符是不合適的...... – trojanfoe

+0

Ive更新了它! – user3110546

回答

2

您的所有類別的位掩碼是相同的,所以你的第一個條件總是爲真。

更改位掩碼來彼此不同:

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的冪。這是你的可讀性不很瞭解位掩碼。位掩碼是32位的,因此您最多可以使用32個類別(未來,您可能希望開始使用位運算符來輕鬆地在位掩碼中反映出更高的數字,例如另一個提示在這裏。因此,擴大你自己的代碼,與上述相同的價值觀是:

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 
+0

不,這不適用於位掩碼 - 例如,您將擁有'(DonutCategory&PizzaCategory)!= 0'。 – molbdnilo

+0

忘記將它們存儲爲2的實際權力。已編輯。 :) – Batalia

0

你所有的類別是相同的值,所以他們都會給出相同的結果。

做到這一點,而不是

enum Category 
{ 
    shipCategory = 0x1,      // 00000001 
    donutCategory = shipCategory << 1,  // 00000010 
    pizzaCategory = donutCategory << 1,  // 00000100 
    chocolateCategory = pizzaCategory << 1, // 00001000 
    // ... and so on ... 
}; 
+1

使用'NS_ENUM'比較好,因爲您可以定義正確的整數類型並定義這樣的值很麻煩(使用'1 << 7'更好,因爲錯誤更容易發現)。更不用說'enum'沒有任何幫助。 – trojanfoe

+0

這也可能更好的樣式你的枚舉如下: 'shipCategory = 0x1 << 0,donutCategory = 0x1 << 1,pizzaCategory = 0x1 << 2'等。這樣,你可以輕鬆地更改順序 – doctorBroctor