我在我的 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));
}