2013-04-07 77 views
0

我在我的cocos2d应用程序中使用视差滚动效果。我想要我的背景持续滚动;然而,我可能会错误地实现它,因为我的背景滚动到左侧,但在此之后,它再也不会回来。在Cocos2d中使用视差滚动背景的问题?

下面是代码:

- (void) setUpBackground 
{ 

    CGSize winSize = [CCDirector sharedDirector].winSize; 

    // Create the CCParallaxNode 
    _backgroundNode = [CCParallaxNode node]; 
    [self addChild:_backgroundNode z:-2]; 

    // Create ths prites 
    para1 = [CCSprite spriteWithFile:@"bg.png"]; 
    para2= [CCSprite spriteWithFile:@"shipp.png"]; 

    // Determine relative movement speeds for trees and background 
    CGPoint treeSpeed = ccp(0.1, 0.1); 
    CGPoint bgSpeed = ccp(0.5, 0.5); 

    // Add children to the CCParallexNode 
    [_backgroundNode addChild:para1 z:0 
       parallaxRatio:treeSpeed 
       positionOffset:ccp(160,winSize.height/2)]; 

    [_backgroundNode addChild:para2 z:1 
       parallaxRatio:bgSpeed 
       positionOffset:ccp(para1.contentSize.width* para1.scale, winSize.height/2)]; 

} 

- (void)updateBackground:(ccTime)dt 
{ 
    CGPoint backgroundScrollVel = ccp(-1000, 0); 
    _backgroundNode.position = ccpAdd(_backgroundNode.position, ccpMult(backgroundScrollVel, dt)); 

} 
+0

也许这有助于:http://www.learn-cocos2d.com/2012/12/ways-scrolling-cocos2d-explained/ – LearnCocos2D 2013-04-07 18:54:25

回答

0

您还没有显示全代码,但我猜你可能会安排在其他地方updateBackground()

updateBackground()中,您设置了背景的位置,其位置X正在减少超时。这就是为什么它永远不会再回来。

当背景到达场景的边缘时,您必须计算整个场景的宽度,背景的宽度以及不计划updateBackground()