0

我在我的 GameLayer 中使用 CCParallaxNode 作为滚动背景,我遇到了一堆错误,我不知道为什么。

“初始化器元素不是编译时常量”和“Unk

@implementation Background

// Load the sprites for each parallax layer, from background to foreground.
CCSprite* para1 = [CCSprite spriteWithFile:@"color.png"];
CCSprite* para2= [CCSprite spriteWithFile:@"ship-hd.png"];
CCSprite* para3 = [CCSprite spriteWithFile:@"twit.png"];
CCSprite* para4 = [CCSprite spriteWithFile:@"Start.png"];

// Set the correct offsets depending on the screen and image sizes.
para1.anchorPoint = CGPointMake(0, 1);
para2.anchorPoint = CGPointMake(0, 1);
para3.anchorPoint = CGPointMake(0, 0.6f);
para4.anchorPoint = CGPointMake(0, 0);

CGPoint topOffset = CGPointMake(0, screenSize.height);
CGPoint midOffset = CGPointMake(0, screenSize.height / 2);
CGPoint downOffset = CGPointZero;

// Create a parallax node and add the sprites to it.
CCParallaxNode* paraNode = [CCParallaxNode node];
[paraNode addChild:para1 z:1 parallaxRatio:CGPointMake(0.5f, 0) ositionOffset:topOffset];

[paraNode addChild:para2 z:2 parallaxRatio:CGPointMake(1, 0) positionOffset:topOffset];

[paraNode addChild:para3 z:4 parallaxRatio:CGPointMake(2, 0) positionOffset:midOffset];

[paraNode addChild:para4 z:3 parallaxRatio:CGPointMake(3, 0) positionOffset:downOffset];

[self addChild:paraNode z:0 tag:ParallaxSceneTagParallaxNode];

                   // Move the parallax node to show the parallaxing effect.
                   CCMoveBy* move1 = [CCMoveBy actionWithDuration:5 position:CGPointMake(−160, 0)];
                   CCMoveBy* move2 = [CCMoveBy actionWithDuration:15 position:CGPointMake(160, 0)];
                   CCSequence* sequence = [CCSequence actions:move1, move2, nil];

                   CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequence];
                   [paraNode runAction:repeat];

@end
4

1 回答 1

0

您需要将代码包含在 -(id)init 方法中,如下所示:

@implementation Background

- (id)init
{
    if (self = [super init]) {
        // Load the sprites for each parallax layer, from background to foreground.
        CCSprite* para1 = [CCSprite spriteWithFile:@"color.png"];
        // ... the rest of your code ... //
    }
    return self;
}

@end
于 2013-03-10T08:24:32.477 回答