首先来看看它的创建方式:
CCParallaxNode::create()
作用:创建一个视觉差效果类。
这个类的主要函数:
addChild(CCNode * child,unsigned int z,CCPoint & parallaxRatio,CCPoint & positionOffset)
作用:添加一个子类“视图层”。
参数1:子类,添加一个CCNode,让其作为视觉差效果中的一个视图层。
参数2:Z轴,视觉差中的遮挡关系。
参数3:x轴、y轴的移动速率。
参数4:子类坐标。
代码:
// 创建bear精灵
CCSprite* bear = CCSprite::create("bear1.png");//change the transform anchor point to 0,0 (optional)
bear->setAnchorPoint( ccp(0,0) );
//创建 background 精灵最为背景
CCSprite* background = CCSprite::create("bg.png");
// scale the image (optional)
background->setScale( 2.0f );
// change the transform anchor point (optional)
background->setAnchorPoint( ccp(0,0) );
// 创建一个parallax节点用于储存各种精灵,然后移动parallax节点以展示“视差”效果
CCParallaxNode* voidNode = CCParallaxNode::create();
// background的移动速度为 0.4x, 0.5y
voidNode->addChild(background, -1, ccp(0.4f,0.5f), CCPointZero);
// bear的移动速度为 3.0x, 2.5y
voidNode->addChild(bear, 1, ccp(3.0f,2.5f), ccp(200,800) );
//移动 CCParallaxNode 产生视差效果
CCActionInterval* goUp = CCMoveBy::create(4, ccp(0,-500) );
CCActionInterval* goDown = goUp->reverse();
CCActionInterval* go = CCMoveBy::create(8, ccp(-1000,0) );
CCActionInterval* goBack = go->reverse();
CCFiniteTimeAction* seq = CCSequence::create(goUp, go, goDown, goBack, NULL);
voidNode->runAction( (CCRepeatForever::create((CCActionInterval*) seq) ));
addChild( voidNode );