在cocos2d中创建CCParallaxNode的问题?

时间:2013-03-10 03:10:04

标签: cocos2d-iphone parallax ccsprite initializer

我在我的GameLayer中使用CCParallaxNode作为滚动背景,我遇到了一堆错误,我无法弄清楚原因。

“Initializer元素不是编译时常量”,也是“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

1 个答案:

答案 0 :(得分: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