我編寫了一個精靈套件遊戲,我有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;
什麼做你的類常量是什麼樣子? – molbdnilo
我對Sprite Kit一無所知,但是我可以看到'categoryBitMask'是一個位掩碼,所以使用'<'操作符是不合適的...... – trojanfoe
Ive更新了它! – user3110546