转自http://blog.sina.com.cn/s/blog_4adf31ea0102eolk.html
Parallax视差,学好英文对编程很重要啊。。。
首先来看看它的创建方式:
<1> CCParallaxNode::create()
作用:创建一个视觉差效果类。
这个类的主要函数:
<1> addChild(CCNode * child,unsigned int z,CCPoint & parallaxRatio,CCPoint & positionOffset)
作用:添加一个子类“视图层”。
参数1:子类,添加一个CCNode,让其作为视觉差效果中的一个视图层。
参数2:Z轴,视觉差中的遮挡关系。
参数3:x轴、y轴的移动速率。
参数4:子类坐标。
//创建四个精灵
CCSprite* para1 = [CCSprite spriteWithFile:@"parallax1.png"];
CCSprite* para2 = [CCSprite spriteWithFile:@"parallax2.png"];
CCSprite* para3 = [CCSprite spriteWithFile:@"parallax3.png"];
CCSprite* para4 = [CCSprite spriteWithFile:@"parallax4.png"];
//设置偏移量
para1.anchorPoint = CGPointMake(0, 1);
para2.anchorPoint = CGPointMake(0, 1);
para3.anchorPoint = CGPointMake(0, 0.6f);
para4.anchorPoint = CGPointMake(0, 0);
//CCParallaxNode 的位置
CGPoint topOffset = CGPo
intMake(0, screenSize.height);
CGPoint midOffset = CGPointMake(0, screenSize.height / 2);
CGPoint downOffset = CGPointZero;
//建立CCParallaxNode
//parallaxRatio为移动剩数因子
CCParallaxNode* paraNode = [CCParallaxNode node];
//以半速移动
[paraNode addChild:para1 z:1 parallaxRatio:CGPointMake(0.5f, 0) positionOffset:topOffset];
//正常速度
[paraNode addChild:para2 z:2 parallaxRatio:CGPointMake
(1, 0) positionOffset:topOffset];
//两倍的速度
[paraNode addChild:para3 z:4 parallaxRatio:CGPointMake(2, 0) positionOffset:midOffset];
//三倍的速度
[paraNode addChild:para4 z:3 parallaxRatio:CGPointMake(3, 0) positionOffset:downOffset];
[self addChild:paraNode z:0 tag:ParallaxSceneTagParallax
Node];
//移动 CCParallaxNode
CCMoveBy* move1 = [CCMoveBy actionWithDuration:5 position:CGPointMake(-160, 0)];
CCMoveBy* move2 = [CCMoveBy actionWithDuration:15 position:CGPointMake(160, 0)];
CCSequence* sequence = [CCSequence actions:move1, move2, nil];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequence];
[paraNode runAction:repeat];
转自http://blog.sina.com.cn/s/blog_4adf31ea0102eolk.html