经常用到的视觉差parallax

auto winSize = Director::getInstance()->getWinSize();
	Sprite *bg = Sprite::create("HelloWorld.png");
	bg->setAnchorPoint(Vec2(0,0));
	
	
	Sprite *ball = Sprite::create("sprite.png");
	ball->setAnchorPoint(Vec2(0.5,0.5));


	Sprite *samll = Sprite::create("sprite.png");
	samll->setAnchorPoint(Vec2(0,0));
	


	ParallaxNode *parallaxNode = ParallaxNode::create();
	this->addChild(parallaxNode,0,"parall");
	parallaxNode->addChild(bg,0,Vec2(0.2,0.2),Vec2(0,0));
	parallaxNode->addChild(ball,1,Vec2(1,1),Vec2(winSize.width/2,winSize.height/2));
	parallaxNode->addChild(samll,1,Vec2(1,1),Vec2(0,0));
auto e = EventListenerTouchOneByOne::create();
	e->onTouchBegan = [bg, ball, samll, parallaxNode](Touch*t, Event*e)
	{
		return true;
	};
	e->onTouchMoved = [bg, ball, samll, parallaxNode](Touch*t, Event*e)
	{
		Vec2 delta = t->getDelta();
		parallaxNode->setPosition(parallaxNode->getPosition()+delta);
	};
	e->onTouchEnded = [bg, ball, samll, parallaxNode](Touch*t, Event*e)
	{
		log("x= %.2f  y=%.2f",bg->getPositionX(),bg->getPositionY());
		log("x= %.2f  y=%.2f", ball->getPositionX(), ball->getPositionY());
		log("x= %.2f  y=%.2f", samll->getPositionX(), samll->getPositionY());
		log("x= %.2f  y=%.2f", parallaxNode->getPositionX(), parallaxNode->getPositionY());
	};
	auto es = this->getEventDispatcher();
	es->addEventListenerWithSceneGraphPriority(e, this);
    return true;


parallax 的   void addChild(Node * child, int z, const Vec2& parallaxRatio, const Vec2& positionOffset);函数

child 为添加的Node 节点 渲染循序 parallaxRatio为相对于parallaxNode 节点比率 如 parallaxNode 移动100 比率为0.5则移动50 positionOffset为开始设定相对于paallaxNode锚点的位子

此函数 常常用在 游戏 地图移动比如: 玛丽奥,魂斗罗等等游戏 可见到 



你可能感兴趣的:(coco2dx)