视差层没有出现在GameScene中

时间:2012-12-18 19:46:07

标签: cocos2d-iphone parallax

通常我采取行动layer.m并在其init方法中添加对parallax方法的调用,例如:

    [self addScrollingBackgroundWithParallax];

该方法是:

-(void)addScrollingBackgroundWithParallax {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CGSize levelSize = [[GameManager sharedGameManager]
                    getDimensionsOfCurrentScene];

// 1. Create image (sprite node)
CCSprite *BGLayer1 = [CCSprite spriteWithFile:@"chap9_scrolling4.png"];

// 2. Create Parallax Node, Position it 
parallaxNode = [CCParallaxNode node];
[parallaxNode setPosition:ccp(levelSize.width/2.0f,screenSize.height/2.0f)];
float xOffset = 0;
// 3. Add image node to parallax node
[parallaxNode addChild:BGLayer1 z:1 parallaxRatio:ccp(1.0f,1.0f) positionOffset:ccp(0.0f,0.0f)];
xOffset = (levelSize.width/2) * 0.3f;
// 4. Add parallax node to layer
[self addChild:parallaxNode z:1];

}

这很好用。所以我试着做一些不同的事情。在游戏场景类中,我启动以下内容:

-(id)init{
self = [super init];
if (self != nil) {
    // The scene will start out by running layer1
    _layer = [PlaneLevel node];
    [self addChild:_layer z:1];

    //Add eye candy
    layer2 = [Scene2EyeCandy node];
    [self addScrollingBackgroundWithParallax];

}
return self;

}

游戏玩家和eyecandy图层。我将游戏层添加到场景中,但是然后不是将eyecandy层添加到场景中,而是调用addScrollingBackgroundWithParallax方法执行此操作:

-(void)addScrollingBackgroundWithParallax {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CGSize levelSize = [[GameManager sharedGameManager] getDimensionsOfCurrentScene];
// 1. Creates the parallax node
CCParallaxNode *parallaxNode = [CCParallaxNode node];
// 2. Positions it
[parallaxNode setPosition:ccp(levelSize.width/2.0f,screenSize.height/2.0f)];

float xOffset = 0;
// 3. Adds the eyecandy layer to the parallax node 
[parallaxNode addChild:layer2 z:40 parallaxRatio:ccp(1.0f,1.0f) positionOffset:ccp(0.0f,0.0f)];

xOffset = (levelSize.width/2) * 0.3f;
// 4. Finally add the parallax node to the scene.
[self addChild:parallaxNode z:1];

}

但是当我运行游戏时,我根本没有获得eyecandy层。

1 个答案:

答案 0 :(得分:0)

当然,你没有。你的代码:

//Add eye candy
layer2 = [Scene2EyeCandy node];
[self addScrollingBackgroundWithParallax];

那么

[self addChild: layer2];