使用Tiled编辑地图

  • 每个图层仅仅能放一种瓦片
  • 瓦片的大小最好是32*32的倍数
  • 对象层里面设置路径的坐标
  • 主程序中获取对象层中的坐标,做对应的操作



设置口袋精灵类:


Monster.h

#include "cocos2d.h"

USING_NS_CC;

class Monster : public Sprite
{
public:
virtual bool init(Vector<Node*> points);
static Monster* create(Vector<Node*> points); //用于获取对象层的坐标
Vector<Node*> p;
Vector<Node*>::iterator start; //精灵
Sprite *s; //依照路径移动
void moveByPath(); //种类随机数
int ranNum; };

Monster.cpp

#include "Monster.h"

Monster* Monster::create(Vector<Node*> points)
{
auto monster = new Monster();
monster->init(points);
monster->autorelease();
return monster;
} bool Monster::init(Vector<Node*> points)
{
Sprite::init(); //设置随机数,控制出场精灵种类
srand(time(NULL));
ranNum = rand()%5; p = points;
start = p.begin(); switch (ranNum)
{
case 0 :
{
s = Sprite::create("1.png");
break;
}
case 1:
{
s = Sprite::create("2.png");
break;
}
case 2:
{
s = Sprite::create("3.png");
break;
}
case 3:
{
s = Sprite::create("4.png");
break;
}
case 4:
{
s = Sprite::create("5.png");
break;
}
}
s->setPosition((*start)->getPosition());
addChild(s); return true;
} //沿着路径移动
void Monster::moveByPath(){
++start;
if(start == p.end()){
s->removeFromParent();
}
else{
Point a = Point((*start)->getPosition());
s->runAction(Sequence::create(MoveTo::create(2,a),CallFuncN::create(CC_CALLBACK_0(Monster::moveByPath,this)),NULL));
}
}

主场景类


HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
#include "Monster.h" USING_NS_CC; class HelloWorld : public cocos2d::LayerColor
{
public: static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld); void menuCloseCallback(cocos2d::Ref* pSender); //存放对象层里的坐标
Vector<Node*> points;
Vector<Node*>::iterator startPoint; //加入物体
void addMonster(); //用于控制时间间隔
int oldTime;
int curTime;
void resetTime(); void update(float dt); //精灵
Sprite *s; }; #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{ auto scene = Scene::createWithPhysics(); auto layer = HelloWorld::create(); scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
scene->getPhysicsWorld()->setGravity(Point(0,-1000)); scene->addChild(layer); return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !LayerColor::initWithColor(Color4B(255,255,255,255)) )
{
return false;
} Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin(); //加入物理边界
auto body = PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT,3);
auto node = Node::create();
node->setPhysicsBody(body);
node->setPosition(visibleSize.width/2,visibleSize.height/2);
addChild(node); //加入地图文件
auto map = TMXTiledMap::create("pokamon.tmx");
map->setPosition(200,0);
addChild(map); //获得对象层中的坐标,存在向量里
TMXObjectGroup* objectGroup = map->getObjectGroup("monster");
ValueVector object = objectGroup->getObjects(); for (ValueVector::iterator it = object.begin(); it != object.end(); it++) {
Value obj = *it;
ValueMap m = obj.asValueMap();
auto node = Node::create();
node->setPosition(m.at("x").asFloat()+200,m.at("y").asFloat());
points.pushBack(node);
} //重置时间
resetTime(); //开启计时器
scheduleUpdate(); return true;
} void HelloWorld::update(float dt)
{
++oldTime;
if (oldTime == curTime)
{
resetTime();
addMonster();
}
} void HelloWorld::resetTime()
{
oldTime = 0;
curTime = 40;
} void HelloWorld::addMonster()
{
auto hero = Monster::create(points);
hero->moveByPath();
addChild(hero);
} void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}

效果:



最新文章

  1. [bzoj2732][HNOI2012]射箭
  2. ClickOnce部署(3):使用证书
  3. NYOJ 451
  4. 面试题-Stack的最小值o(1)
  5. Web APP开发技巧总结
  6. 任意文件夹下打开cmd功能的设置(win10)
  7. winform 窗体传值
  8. Linux安装、卸载软件
  9. 自动打补丁Bat指令
  10. C#局域网桌面共享软件制作(三)
  11. BootStrap2学习日记20---定制缩略图
  12. Android - Binder驱动
  13. 推荐一个PHP扩展 来真正实现PHP多线程的开发
  14. JQuery图片轮换 nivoSlider图片轮换
  15. 后台构建 html 字符串传到前台字符串转码(html)处理
  16. 用C++语言开发Android程序 配置开发环境
  17. OpenWrt的UCI系统
  18. Linux GIT服务器配置
  19. Hive基础(3)---Fetch Task(转)
  20. 解决Python2.7的UnicodeEncodeError:&#39;ascii&#39; codec can&#39;t encode characters in position 0-78: ordinal not in range(128)异常错误

热门文章

  1. android-EditText 更改外边框无效
  2. qtcreator配置
  3. 带你走进EJB--EJB和Spring对比(转)
  4. BZOJ1492: [NOI2007]货币兑换Cash(CDQ分治,斜率优化动态规划)
  5. CISP/CISA 每日一题 九(2017-11-30 09:25)
  6. HUSTOJ 1072 小数背包问题
  7. ontouch、dispatchtouchevent、interceptouchevent-相关事件
  8. 存储过程和SQL语句比较
  9. 【Educational Codeforces Round 33 C】 Rumor
  10. PatentTips - Method for guest operating system integrity validation