iphone - Sprite 上的无限滚动(视差)

标签 iphone ios cocos2d-iphone parallax

我是 cocos2d 世界的新手,我正在开发我的第一个教程并面临一个问题 我的问题是我有一个图像(1024 X 320)并且我的方向是横向我需要从右到左连续移动该图像为此目的我使用了 Ray 的太空射击教程(感谢他)但图像没有似乎一次又一次地出现。 我的代码是..

-(id) init
{
    if( (self=[super init])) {
        CGSize screenSize = [CCDirector sharedDirector].winSize;

        // 1) Create the CCParallaxNode
        backgroundNode = [CCParallaxNode node];
        [self addChild:backgroundNode z:-1];

        // 2) Create the sprites we'll add to the CCParallaxNode
        Back = [CCSprite spriteWithFile:@"bg_front_spacedust.png"];
        //Back.position=ccp(screenSize.width/2, screenSize.height/2);
        Back.rotation = -90;
        Back1 = [CCSprite spriteWithFile:@"bg_front_spacedust.png"];
        Back1.rotation = -90;

        // 3) Determine relative movement speeds for space dust and background
        CGPoint dustSpeed = ccp(0.1, 0.1);

        // 4) Add children to CCParallaxNode
        [backgroundNode addChild:Back z:0 parallaxRatio:dustSpeed positionOffset:ccp(screenSize.width/2, screenSize.height/2)];
        NSLog(@"back.content width is...%f",Back.contentSize.width);
        [backgroundNode addChild:Back1 z:1 parallaxRatio:dustSpeed positionOffset:ccp(screenSize.width/2, screenSize.height*2)];

        // 5) Enable updates
        [self scheduleUpdate]; 
}
    return self;
}


- (void)update:(ccTime)dt {

    // 1) Update background position
    CGPoint backgroundScrollVel = ccp(0,-1000);
    backgroundNode.position = ccpAdd(backgroundNode.position, ccpMult(backgroundScrollVel, dt));

    // 2) Check for background elements moving offscreen
    NSArray *spaceDusts = [NSArray arrayWithObjects:Back, Back1, nil];
    for (CCSprite *spaceDust in spaceDusts) {
        if ([backgroundNode convertToWorldSpace:spaceDust.position].x < -spaceDust.contentSize.width) {
            [backgroundNode incrementOffset:ccp(2*spaceDust.contentSize.width,0) forChild:spaceDust];
        }
    }
}

请帮我解决这个问题
提前致谢。

最佳答案

试试这个

if (backgroundNode.position.y <-screenSize.height*2)
            backgroundNode.position = ccp(0,0); 

由于 init 方法仅被调用一次,因此您正在执行的方法仅执行一次,因此您需要在更新方法中再次将 backgroundNode 的 Position 设置为 0。 这里的倍数可能会有所不同

关于iphone - Sprite 上的无限滚动(视差),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18484357/

相关文章:

ios - UIPanGestureRecognizer 的准确起始位置?

xcode - 在 Cocos2d 中使用单独的类文件控制特定 CCSprite 的实例

iphone - 状态栏与带有 xcode5 的 iOS 7 中的应用程序 View 重叠

ios - Callkit 中的群组通话

ios - 使用 Storyboard 以编程方式设置初始 View Controller

ios - 在我的游戏中实现 CCSprite 类

iphone - cocos2d-iOS - 手势识别器

iphone - 将字符串格式说明符(%@、%d 等)保存在 NSString 中以供将来占位符使用?

iphone - 敌人和斜坡

ios - Xcode Crash IOS APP:dyld:未加载库,原因:未找到图像