Cocos2D:在我稍微移动地图之前,CCParallaxNode不会显示

时间:2011-06-21 21:28:06

标签: iphone background cocos2d-iphone scroll parallax

我正在使用Cocos2D 0.99.5。我有一个CCParallaxNode添加了背景精灵。由于某些原因,它们都没有显示,直到我开始我的地图开始移动一点。随着球员运动慢慢滚动。

我已将此添加到init:

    winSize = [CCDirector sharedDirector].winSize;

    bgMountainsMax = floor(winSize.width/240)+1;

    if((int)winSize.width%240 > 0){
        bgMountainsBumper = ((int)winSize.width%240)/bgMountainsMax;
    }else{
        bgMountainsBumper = 0;
    }

    backgroundNode = [CCParallaxNode node];
    [self addChild:backgroundNode z:-1];

    CGPoint mountainSpeed = ccp(0.5, 0.5);

    bgMountains = [[NSMutableArray alloc] initWithCapacity: bgMountainsMax];

    for(int i=0; i<bgMountainsMax; ++i){
        mountain = [CCSprite spriteWithFile:@"mountainBG1.png"];
        mountain.opacity = 80;
        [bgMountains insertObject:mountain atIndex:i];

        [backgroundNode addChild:[bgMountains objectAtIndex:i] z:0 parallaxRatio:mountainSpeed positionOffset:ccp((240*i)+(bgMountainsBumper*i),98)];

    }

这增加了更新:

    for (CCSprite *mountainNum in bgMountains) {
    if ([backgroundNode convertToWorldSpace:mountainNum.position].x < -(240/2)) {
        [backgroundNode incrementOffset:ccp(winSize.width+(240),0) forChild:mountainNum];
    }

    if ([backgroundNode convertToWorldSpace:mountainNum.position].x > winSize.width+(240/2)) {          
        [backgroundNode incrementOffset:ccp(-(winSize.width+240),0) forChild:mountainNum];
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你应该在初始化内容结束时调用更新函数

[backgroundNode incrementOffset:ccp(winSize.width+(240),0) forChild:mountainNum];
[backgroundNode incrementOffset:ccp(-(winSize.width+240),0) forChild:mountainNum];

没有条件,最初要进行设置。