cocos2d-x 3.2 移动游戏背景和精灵

cocos2d-x 3.2 移动游戏背景和精灵,第1张

概述1,先新增监听事件 auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(StartGame::onTouchBegan, this); listener->onTouchMoved = CC

1,先新增监听事件

auto Listener = EventListenertouchOneByOne::create();	Listener->setSwallowtouches(true);	Listener->ontouchBegan = CC_CALLBACK_2(StartGame::ontouchBegan,this);	Listener->ontouchmoved = CC_CALLBACK_2(StartGame::ontouchmoved,this);	Listener->ontouchended = CC_CALLBACK_2(StartGame::ontouchended,this);	_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);

2,监听的方法回调函数实现
bool StartGame::ontouchBegan(touch* touch,Event* event){    Point touchLocation = this->converttouchToNodeSpace(touch);    this->selectSpriteFortouch(touchLocation);    return true;}voID StartGame::ontouchmoved(touch* touch,Event* event){	Point touchLocation = this->converttouchToNodeSpace(touch);    Point oldtouchLocation = touch->getPrevIoUsLocation();    oldtouchLocation = this->convertToNodeSpace(oldtouchLocation);    Point translation = touchLocation - oldtouchLocation;//获取移动的距离	cclog("touchLocation--------------------%d-------%d",touchLocation.x,touchLocation.y);	cclog("oldtouchLocation--------------------%d-------%d",oldtouchLocation.x,oldtouchLocation.y);	cclog("translation--------------------%d-------%d",translation.x,translation.y);	    this->boundLayerPos(translation);//背景移动函数
    //this->panForTranslation(translation);//精灵移动函数}voID StartGame::ontouchended(touch* touch,Event* event){}


3,方法实现

这个事背景事件,在init里面加载一张背景图片

voID StartGame::boundLayerPos(Point newtouch){    Size winSize = Director::getInstance()->getWinSize();	Point newPos = this->getposition() + newtouch;	auto map = getChildByTag(102); //这里获取背景图片    newPos.x = MIN(newPos.x,0);    newPos.x = MAX(newPos.x,-map->getContentSize().wIDth+winSize.wIDth);    newPos.y = MIN(newPos.y,0);	newPos.y = MAX(newPos.y,-map->getContentSize().height + winSize.height);    this->setposition(newPos);}

精灵移动方法实现

在init里面加载一个精灵

voID StartGame::panForTranslation(Point translation){	auto sprite = getChildByTag(101);//获取精灵    Point newPos = sprite->getposition() + translation;    sprite->setposition(newPos);    }

这就ok了


精灵或者是背景图片移动都是在触屏监听事件的ontouchmoved(touch* touch,Event * event)里面获取原触屏点和移动以后的触屏点,得到他们的差值,给精灵或者背景图片重新定位setposition(Point * point),就完成了背景和精灵的移动(背景图片要判断边界值得)


注意,Node可通过函数getBoundingBox返回精灵的边界矩形。这比你自己手动计算精灵的边界矩形要好多了。因为,第一,它更简单;第二,它考虑了精灵的位置坐标变换。(比如锚点变了,要执行相应的矩阵变换,具体可以参考源代码,这些就不再细说了。)

点击选中的时候可以先让精灵做一些动作,这里是闪烁

voID StartGame::selectSpriteFortouch(Point touchLocation){	auto sprite = getChildByTag(101);       if ( sprite->getBoundingBox().containsPoint(touchLocation) )    {            auto blink = CCBlink::create(2.0f,5);			sprite->runAction(blink);    }      }
总结

以上是内存溢出为你收集整理的cocos2d-x 3.2 移动游戏背景和精灵全部内容,希望文章能够帮你解决cocos2d-x 3.2 移动游戏背景和精灵所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1009653.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存