Use tiled to edit a map
- Each layer can only contain one tile.
- The tile size should be a multiple of 32*32.
- Set path coordinates in the object Layer
- Obtain the coordinates in the object Layer in the main program and perform corresponding operations.
Set the pocket genie class:
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));}}Main scenario
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 instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init firstif ( !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}Effect:
[Cocos2dx 3.2] tile map creation