CCParallaxNode类和视差效果

转自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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值