游戏场景不显示

时间:2014-09-05 09:35:43

标签: ios objective-c xcode sprite-kit

我的游戏运行菜单场景,但是当我按下播放按钮并转换到主场景时,我只能看到灰色屏幕。

继承人我的代码:Myscene.h

#import <SpriteKit/SpriteKit.h>

@interface MyScene : SKScene
@property (strong, nonatomic) SKLabelNode *scoreLabel;
@property (nonatomic) NSInteger score;
@property (nonatomic) BOOL gameOver;
@property SKEmitterNode *shatter;

typedef NS_ENUM(NSInteger, SpriteType) {
    SpriteTypeBackground,
    SpriteTypeSquare
};

Myscene.m

    #import "MyScene.h"

@interface MyScene()

@property SKSpriteNode *background;
@property SKSpriteNode *square;

@end

@implementation MyScene
#define kNumberOfColors     2 //Setting total number of possible colors



-(id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
        [self runAction:[SKAction repeatActionForever: [SKAction sequence:@[[SKAction                                                                             performSelector:@selector(addSquareAndBackground) onTarget:self] ]]]];

        //Score Label
        float margin = 10;
        self.scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
        self.scoreLabel.text = @"Score: 0";
        self.scoreLabel.fontSize = [self convertFontSize:14];
        self.scoreLabel.zPosition = 4;
        self.scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
        self.scoreLabel.position = CGPointMake(margin, margin);
        [self addChild:self.scoreLabel];



    }
    return self;
}

- (float)convertFontSize:(float)fontSize
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        return fontSize * 2;
    } else {
        return fontSize;
    }
}


-(void)addSquareAndBackground {
    _background = [self createSpriteWithType:SpriteTypeBackground];
    _square = [self createSpriteWithType:SpriteTypeSquare];
}

-(void)removeSquareAndBackground {
    [_background removeFromParent];
    [_square removeFromParent];
}

-(SKSpriteNode *) createSpriteWithType:(SpriteType)type {
    // Select a color randomly
    NSString *colorName = [self randomColorName];
    SKSpriteNode *sprite;
    if (type == SpriteTypeBackground) {
        NSString *name = [NSString stringWithFormat:@"%@Outline",colorName];
        sprite = [SKSpriteNode spriteNodeWithImageNamed:name];
        sprite.name = name;
        sprite.size = CGSizeMake(CGRectGetHeight(self.frame), CGRectGetWidth(self.frame));
    }
    else {
        sprite = [SKSpriteNode spriteNodeWithImageNamed:colorName];
        sprite.name = [NSString stringWithFormat:@"%@Sprite",colorName];
        sprite.size = CGSizeMake(50, 50);
    }
    sprite.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
    [self addChild:sprite];

    return sprite;
}

- (NSString *) randomColorName {
    NSString *colorName;
    switch (arc4random_uniform(kNumberOfColors)) {
        case 0:
            colorName = @"blue";
            break;
        case 1:
            colorName = @"pink";
            break;
            // Add more colors here
        default:
            break;
    }
    return colorName;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    NSString *pinkShatterPath = [[NSBundle mainBundle] pathForResource:@"pinkShatter" ofType:@"sks"];
    SKEmitterNode *pinkShatter = [NSKeyedUnarchiver unarchiveObjectWithFile:pinkShatterPath];
    pinkShatter.particlePosition = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));


    if (node == _square) {
        // Extract the color name from the node name
        NSArray *squareNameParts = [node.name componentsSeparatedByCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
        // Extract the color name from the node name
        NSArray *backgroundNameParts = [_background.name componentsSeparatedByCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
        // Compare if the colors match
        if ([backgroundNameParts[0] isEqualToString: squareNameParts[0]]) {
            //Add SKAction to add 1 to score label
            [SKAction performSelector:@selector(removeSquareAndBackground) onTarget:self];
            [self addChild:pinkShatter];
            self.score += 10;

            //NSLog(@"Score"); for console error detection
        } else {
            //NSLog(@"Lost"); //Add SKAction to display game-over menu
            SKLabelNode *gameOverLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
            gameOverLabel.text = @"You Lose!!!!!";
            gameOverLabel.fontSize = 20;
            gameOverLabel.zPosition = 4;
            gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                                 CGRectGetMidY(self.frame));

            [gameOverLabel setScale:0.1];

            [self addChild:gameOverLabel];
            [gameOverLabel runAction:[SKAction scaleTo:1.0 duration:0.5]];

            self.gameOver = YES;
            return;
        }
    }


}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    [self.scoreLabel setText:[NSString stringWithFormat:@"Score: %ld", (long)self.score]];
}

@end

在我添加菜单场景之前,它在添加后现在工作正常,我似乎无法使其工作。

1 个答案:

答案 0 :(得分:1)

你知道你在这做什么吗?

[self runAction:
 [SKAction repeatActionForever:
  [SKAction sequence:@[
   [SKAction performSelector:@selector(addSquareAndBackground) onTarget:self]
  ]]]];

首先,序列完全是多余的,因为你实际上并没有对多个动作进行排序,你只有一个:

[self runAction:
 [SKAction repeatActionForever:
   [SKAction performSelector:@selector(addSquareAndBackground) onTarget:self]
  ]];

重复执行performSelector将导致选择器无延迟地重复运行。 performSelector动作没有持续时间,它立即运行。永远重复它将创建背景精灵,每帧一个或者,取决于SK如何解释它,甚至可能在无限循环中运行选择器,直到应用程序崩溃。