Cocos2d-x 《雷电大战》-精灵随手指移动,你点哪我走哪!

Cocos2d-x 《雷电大战》-精灵随手指移动,你点哪我走哪!,第1张

概述    本文要实现飞机游戏中,人的手指按着飞机,就能拖着飞机走动,这里实现了当你手指按在手机的图片上,手指一直按着屏幕,飞机就会跟着你走。同时,还加入了边界判断条件,让飞机在你的视野内移动,实现的效果完全和我们手机上的飞机游戏一样。 效果: Cocos2d-x版本:3.4 工程环境:VS30213 一、代码编写 1、头文件GameMain.h /***@作者 林炳文(邮箱:ling2008100

本文要实现飞机游戏中,人的手指按着飞机,就能拖着飞机走动,这里实现了当你手指按在手机的图片上,手指一直按着屏幕,飞机就会跟着你走。同时,还加入了边界判断条件,让飞机在你的视野内移动,实现的效果完全和我们手机上的飞机游戏一样。

效果:

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="c1.gif" alt="wKiom1T8QyfjnzlgABqxNLHvPJQ526.gif" src="http://s3.51cto.com/wyfs02/M01/5A/64/wKiom1T8QyfjnzlgABqxNLHvPJQ526.gif">

Cocos2d-x版本:3.4

工程环境:VS30213

一、代码编写

1、头文件GameMain.h

/***@作者林炳文(邮箱:[email protected])*@博客http://linbingwen.blog.51cto.com/*@时间2015.3.8*@功能游戏的主界面*/#ifndef__GameMain_H__#define__GameMain_H__#include"BackLayerDown.h"#include"BackLayerUp.h"#include"cocos2d.h"USING_NS_CC;classGameMain:publiccocos2d::Layer{public:staticcocos2d::Scene*createScene();virtualboolinit();virtualboolontouchBegan(cocos2d::touch*touch,cocos2d::Event*unused_event);virtualvoIDontouchmoved(cocos2d::touch*touch,cocos2d::Event*unused_event);virtualvoIDontouchEened(cocos2d::touch*touch,cocos2d::Event*unused_event);virtualvoIDontouchCancelled(cocos2d::touch*touch,cocos2d::Event*unused_even);CREATE_FUNC(GameMain);private:boolisHeroPlaneControl;//飞机是否被控制着floatmDeltaX;//英雄飞机随手指移动时的X偏移量floatmDeltaY;//英雄飞机随手指移动时的Y偏移量Sprite*mHeroPlane;//英雄飞机};#endif//__GameMain_H__
#include"GameMain.h"USING_NS_CC;Scene*GameMain::createScene(){autoscene=Scene::create();autolayer=GameMain::create();scene->addChild(layer);returnscene;}boolGameMain::init(){SizevisibleSize=Director::getInstance()->getVisibleSize();Pointorigin=Director::getInstance()->getVisibleOrigin();//这是地面图层this->addChild(BackLayerUp::create());//这是白云图层this->addChild(BackLayerDown::create());//加个飞机mHeroPlane=Sprite::create("air1.png");mHeroPlane->setposition(Vec2(visibleSize.wIDth/2,visibleSize.height/5));this->addChild(mHeroPlane,1,100);isHeroPlaneControl=false;//打开触摸,增加触摸监听事件this->settouchEnabled(true);autoListen=EventListenertouchOneByOne::create();Listen->ontouchBegan=CC_CALLBACK_2(GameMain::ontouchBegan,this);Listen->ontouchmoved=CC_CALLBACK_2(GameMain::ontouchmoved,this);Listen->ontouchended=CC_CALLBACK_2(GameMain::ontouchEened,this);Listen->ontouchCancelled=CC_CALLBACK_2(GameMain::ontouchCancelled,this);Listen->setSwallowtouches(false);Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(Listen,this);returntrue;}boolGameMain::ontouchBegan(cocos2d::touch*touch,cocos2d::Event*unused_event){PointmHeroPos=mHeroPlane->getposition();PointmBeganPos=touch->getLocationInVIEw();mBeganPos=Director::getInstance()->convertToGL(mBeganPos);//判断当前手指按下区域是否是英雄飞机的区域,并且计算飞机要移动时的偏移量if(mBeganPos.x>mHeroPos.x-mHeroPlane->getContentSize().wIDth/2&&mBeganPos.x<mHeroPos.x+mHeroPlane->getContentSize().wIDth/2&&mBeganPos.y>mHeroPos.y-mHeroPlane->getContentSize().height/2&&mBeganPos.y<mHeroPos.y+mHeroPlane->getContentSize().height/2){isHeroPlaneControl=true;//�算偏移量mDeltaX=mBeganPos.x-mHeroPos.x;mDeltaY=mBeganPos.y-mHeroPos.y;}returntrue;}voIDGameMain::ontouchmoved(cocos2d::touch*touch,cocos2d::Event*unused_event){if(isHeroPlaneControl){PointmMovedPos=touch->getLocationInVIEw();mMovedPos=Director::getInstance()->convertToGL(mMovedPos);SizevisibleSize=Director::getInstance()->getVisibleSize();Pointorigin=Director::getInstance()->getVisibleOrigin();floatx=mMovedPos.x-mDeltaX;//�得�去偏移量floaty=mMovedPos.y-mDeltaY;if(x<=mHeroPlane->getContentSize().wIDth/2+origin.x)//x到达屏幕左边界x=mHeroPlane->getContentSize().wIDth/2+origin.x;elseif(x>=visibleSize.wIDth-mHeroPlane->getContentSize().wIDth/2)//x到达屏幕右边界x=visibleSize.wIDth-mHeroPlane->getContentSize().wIDth/2;if(y<=mHeroPlane->getContentSize().height/2+origin.y)//y到达屏幕下边界y=mHeroPlane->getContentSize().height/2+origin.y;elseif(y>=visibleSize.height-mHeroPlane->getContentSize().height/2)//x到达屏幕上边界y=visibleSize.height-mHeroPlane->getContentSize().height/2;//飞机跟随手指移动mHeroPlane->setposition(Vec2(x,y));}}voIDGameMain::ontouchEened(cocos2d::touch*touch,cocos2d::Event*unused_event){isHeroPlaneControl=false;}voIDGameMain::ontouchCancelled(cocos2d::touch*touch,cocos2d::Event*unused_even){isHeroPlaneControl=false;}

这里再说一写主要函数:

头文件增加触摸事件:

virtualboolontouchBegan(cocos2d::touch*touch,cocos2d::Event*unused_even);

实现文件开启触摸事件监听:

//打开触摸,增加触摸监听事件this->settouchEnabled(true);autoListen=EventListenertouchOneByOne::create();Listen->ontouchBegan=CC_CALLBACK_2(GameMain::ontouchBegan,this);

然后就是触摸事件 的处理了:

boolGameMain::ontouchBegan(cocos2d::touch*touch,cocos2d::Event*unused_even){isHeroPlaneControl=false;}

方法很简单,代码量也很少,有需要的把上面的自己拿过去,把图片改改,把类名改改就可以了。

其实这里应该把英雄和移动事件单独写一个类,然后在GameMain里头来调用,因为英雄这个类我还构思好,所以先这样写,后头会把英雄飞机单独提取出来成为一个类,就不会在GameMain里头写这么多了;

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="c1.gif" alt="wKiom1T8QyfjnzlgABqxNLHvPJQ526.gif" src="http://s3.51cto.com/wyfs02/M01/5A/64/wKiom1T8QyfjnzlgABqxNLHvPJQ526.gif">

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="c2.gif" alt="wKioL1T8REXTT8-WAA275rBxn4o876.gif" src="http://s3.51cto.com/wyfs02/M02/5A/60/wKioL1T8REXTT8-WAA275rBxn4o876.gif">

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="c3.gif" alt="wKiom1T8Qy3jgfhpABuBZi5igXk095.gif" src="http://s3.51cto.com/wyfs02/M02/5A/64/wKiom1T8Qy3jgfhpABuBZi5igXk095.gif">



效果很好,飞机能跟随移动并且不会跑出屏幕范围

二、思路说明

1、首先在ontouchBegan判断触摸点是否在英雄飞机的图片矩形内,若在这个范围内,刚将布尔型的mHeroPlaneControl设置为true,并且计算触摸点的横纵坐标与英雄飞机的锚点坐标的差值。

2、因为要让英雄飞机移动,要修改锚点位置,必须知道锚点位置与触摸位置的偏移量,之后才可以通过这个偏移量设置主角的位置。

3、判断英雄飞机是否跑出屏幕范围了,如果是,就将它设置在边界处,详看上面的两个if判断。

4、在ontouchmoved中,若mHeroPlaneControl为true,说明可以移动英雄飞机。


若你觉得此文对你有用,那就帮我赞一下~~谢谢啦

总结

以上是内存溢出为你收集整理的Cocos2d-x 《雷电大战》-精灵随手指移动,你点哪我走哪!全部内容,希望文章能够帮你解决Cocos2d-x 《雷电大战》-精灵随手指移动,你点哪我走哪!所遇到的程序开发问题。

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

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

原文地址: https://www.outofmemory.cn/web/1046889.html

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

发表评论

登录后才能评论

评论列表(0条)

保存