2013-03-10 64 views
0

我在我的GameLayer中使用CCParallaxNode作爲滾動背景,並且出現一堆錯誤,我找不到原因。在cocos2d中創建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 

回答

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

非常感謝!非常簡單的解釋!快速的問題,我收到錯誤「使用未聲明的標識符'ParallaxSceneTAgParallaxNode'。任何想法爲什麼? – Surz 2013-03-11 15:43:25

+0

您需要聲明該整數。你可以像這樣使用'enum':'enum Tags {ParallaxSceneTagParallaxNode, };' – Calin 2013-03-13 08:34:41