经常用到的视觉差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锚点的位子

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值