cocos2d-x-3.2塔防游戏开发1:背景,地图,obj,产生移动NPC的实
塔防游戏的开发: 1,准备资源: 2,一般的游戏开发中我们用到的都是大图,首先简单的介绍下图片的裁切, 3,纹理打包工具的使用,Texturepacker工具,它会自动的把图片摆放图片,把多张图片整合到一张图片上,这样的话可以减少图片的开销,这样加载图片是只需要加载一张图片就好了,它导出的时cocos2d的plist格式。 4,使用TexturePacker打包多张小图 5, 1,将多张小图拖动到TextPacker中 2.定义导出格式 导出plist文件名 导出合成图的png文件名 3. 选择publish发布 生成一个xxx.plist xxx.png 4.拷贝这2个文件到Resource目录 5.使用SpriteFrameCatch一次性加载到缓存 //使用plist实现动画的创建,使用SpriteFrameCache::加载所有帧在内存 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("xxx.plist"); SpriteFrame*sf=SpriteFrameCache::getInstance()->getSpriteFrameByName("key");key就是在第一步拖动过去小图片的文件名。 6.屏幕的适配: ?在applicationDidFinishLaunching方法中添加以下几行代码: ?glview->setDesignResolutionSize(1136.0f,640.0f,ResolutionPolicy::FIXED_HEIGHT); ?std::vector<std::string>searchPath; ?searchPath.push_back("height_864"); ?CCFileUtils::getInstance()->setSearchPaths(searchPath); ?director->setContentScaleFactor(864.0f/ 640.0f);//注意:这里需要加上.0f ?特别要说明的是,本游戏的地图资源大小为1536 * 864。我们要制作一个高度方向上全部显示的游戏,所以选择分辨率模式为:FIXED_HEIGHT。 7. 接下来我们会做如下的操作: 1.背景添加 2.地图添加 3.加载解析地图中的对象层数据 4.创建怪物 5.让怪物自动从第一个点移动到最后一个点 ------------------- //加载背景 auto sp=Sprite::create("playbg.png"); this->addChild(sp); sp->setPosition(Vec2(Director::getInstance()->getWinSize().width/2, Director::getInstance()->getWinSize().height/2)); //加载地图 auto map=TMXTiledMap::create("tilemap0.tmx"); this->addChild(map); this->setTag(888); //加载所有点:按照这个路径走下去 initAllPoint(map); TDPoint。h TDPoint。Cpp //前提:可以做一个对象层的类 #include"cocos2d.h" USING_NS_CC; //定义每一个点 class TDPoint:public Ref { public: int px; int py; static TDPoint *createPoint(int x,int y); }; #include"TDPoint.h" TDPoint * TDPoint::createPoint(int x,int y){ TDPoint * td=new TDPoint(); td->autorelease(); td->px=x; td->py=y; return td; } //创建对象层“obj”,并添加对象。这里对象就是指图中的小矩形,用这些矩形对象就可以计算敌人的行进路线,它们记录了敌人们移动的顺序和位置坐标。注意摆放位置和顺序。这里我们可以为矩形对象添加属性名来标示它的顺序,从0开始,依次增加,Cocos2d-x会为我们创建ValueMap类型的结构来保存相关的数据。 //做好类的定义我们在GameScene。h中定义 static Vector<TDPoint *> allPoint;//保存所有点 void initAllPoint(TMXTiledMap *m);//加载地图的对象层每个点 //在GameScene。Cpp中实现 void GameScene::initAllPoint(TMXTiledMap *m){ ValueVector value; //向量 value=m->getObjectGroup("obj")->getObjects();//得到对象层数据//obj使我们定义对象时的名称,,使用for获取这几个点 for (int i=0; i<value.size(); i++) { ValueMap val=value.at(i).asValueMap(); //获取第i个点得到每一个元素,每个元素都是一个valuemap TDPoint * newp=TDPoint::createPoint(val.at("x").asInt(), val.at("y").asInt()); allPoint.pushBack(newp); }
} //这个过程其实就是一个解析的过程,解析我们在对象层的数据,这样obj是一个valuevector的集合,我们把集合解析成valuemap的对象,我们通过key得到x,y,得到以后我们添加到allpoint中 //
//对象层的数据 //<objectgroup name="obj"width="48" height="27"> //? <object name="0"x="333" y="861"/> //? <object name="1"x="339" y="497"/> //<object name="2"x="727" y="494"/> //? </objectgroup // 8.接下来我们就该产生怪物了 Enemy。h中 #include"cocos2d.h" #include"TDPoint.h" #include"GameScene.h"//GameScene中的allpoint定义为静态的,这样,在这里就可以引用 USING_NS_CC; class Enemy:public Node { public: // Vector<TDPoint *>nowPoint;//所有的移动点 int nowPointIndex;//当前点的编号 int type;//怪物类型 int hp;//怪物生命 int speed;//移动速度 int ex,ey; CREATE_FUNC(Enemy);
bool init(); void MoveTo(); static Enemy *createEnemy(int type); }; Enemy。Cpp中 ///想要使用文理,那么我们到GameSCene。cpp中先去加载文理,写在GameScene中: SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Play.plist");
#include"Enemy.h" Enemy * Enemy::createEnemy(int type){ Enemy * newe=Enemy::create(); newe->type=type; switch (type) { case 1://第一种怪物 {Vector<SpriteFrame * >allf; for (int i=1; i<5; i++) { SpriteFrame * sf= SpriteFrameCache::getInstance()->getSpriteFrameByName( StringUtils::format("enemyLeft1_%d.png",i) ); allf.pushBack(sf); } Animation * animation=Animation::createWithSpriteFrames(allf); animation->setDelayPerUnit(0.3); Animate * animate=Animate::create(animation); Sprite *sp=Sprite::create(); sp->setOpacity(0); sp->setTag(100); sp->runAction(RepeatForever::create(animate)); newe->addChild(sp); } newe->speed=60; break; case 2://第二种怪物 {Vector<SpriteFrame * >allf; for (int i=1; i<5; i++) { SpriteFrame * sf= SpriteFrameCache::getInstance()->getSpriteFrameByName( StringUtils::format("enemyLeft2_%d.png",i) ); allf.pushBack(sf); } Animation * animation=Animation::createWithSpriteFrames(allf); animation->setDelayPerUnit(0.3); Animate * animate=Animate::create(animation); Sprite *sp=Sprite::create(); sp->setOpacity(0); sp->setTag(100); sp->runAction(RepeatForever::create(animate)); newe->addChild(sp); newe->speed=80; } default: break; } //将怪物移动到目标点第一个点 int x=GameScene::allPoint.at(0)->px; //在地图的第0个元素的(点)的位置的x int y=GameScene::allPoint.at(0)->py; //在地图的第0个元素的(点)的位置的y newe->ex=x; newe->ey=y; newe->nowPointIndex=0;//第0个点得编号 newe->setPosition(Vec2(x,y)); newe->setOpacity(0); auto act1=FadeIn::create(0.8); auto act2=CallFunc::create(CC_CALLBACK_0(Enemy::MoveTo,newe));//到这个点我就让它自动的移动 newe->getChildByTag(100)->runAction(Sequence::create(act1,act2,NULL)); //自动移动 //newe->MoveTo(); return newe; } bool Enemy::init() { if (!Node::init()){ return false; } //创建怪物动画
return true; } void Enemy::MoveTo(){ //获取下一个点 this->nowPointIndex++; if (nowPointIndex>=GameScene::allPoint.size()) { //玩家减血 CCLOG("我跑了 .....玩家掉血"); //怪物消失 this->removeFromParent();//消失 return; } int x=GameScene::allPoint.at(nowPointIndex)->px;//下一个点得px int y=GameScene::allPoint.at(nowPointIndex)->py;//下一个点得py double far=Vec2(x,y).getDistance(Vec2(ex,ey)); //sqrt((x-ex)*(x-ex)+(y-ey)*(y-ey)); // CCLOG("moveto far=%lf",far);
ex=x;//这是重置点得坐标 ey=y; double time=far/speed;//这样的话就实现了匀速的前进 auto act=MoveTo::create(time,Vec2(x,y)); auto act2=CallFunc::create(CC_CALLBACK_0(Enemy::MoveTo,this)); this->runAction(Sequence::create(act,NULL)); } // 定义好了怪物,我们就需要createEnemy,让它定时的在第一个点得位置 产生怪物,定义定时器, 在GameScene。cpp中 this->schedule(schedule_selector(GameScene::newEnemy),3); this->enemyCount=20;
void GameScene::newEnemy(float t) {
if(enemyCount>0) { int type=random()%10>8?2:1; Enemy * e1=Enemy::createEnemy(type); this->addChild(e1); enemyCount--; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |