2013-03-10 4 views
0

Я использую CCParallaxNode для прокрутки фона в моем GameLayer, и я получаю кучу ошибок, и я не могу понять, почему.Проблемы с созданием CCParallaxNode в cocos2d?

"Initializer элемент не является константой во время компиляции", а также «Унк

@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) инициализации, например:

@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

+0

Вам нужно объявить это целое число. Вы можете использовать 'enum' для этого, например:' enum Tags { ParallaxSceneTagParallaxNode, }; ' – Calin

 Смежные вопросы

  • Нет связанных вопросов^_^