【cocos2dx 3.2】瓦片地图制作

From: http://blog.csdn.net/ns2250225/article/details/40509185

使用Tiled编辑地图

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



设置口袋精灵类:


Monster.h

  1. #include "cocos2d.h"   
  2.   
  3. USING_NS_CC;  
  4.   
  5. class Monster : public Sprite  
  6. {  
  7. public:  
  8.     virtual bool init(Vector<Node*> points);  
  9.     static Monster* create(Vector<Node*> points);  
  10.   
  11.     //用于获取对象层的坐标   
  12.     Vector<Node*> p;  
  13.     Vector<Node*>::iterator start;  
  14.   
  15.     //精灵   
  16.     Sprite *s;  
  17.   
  18.     //按照路径移动   
  19.     void moveByPath();  
  20.   
  21.     //种类随机数   
  22.     int ranNum;  
  23.   
  24. };  
#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

  1. #include "Monster.h"   
  2.   
  3. Monster* Monster::create(Vector<Node*> points)  
  4. {  
  5.     auto monster = new Monster();  
  6.     monster->init(points);  
  7.     monster->autorelease();  
  8.     return monster;  
  9. }  
  10.   
  11. bool Monster::init(Vector<Node*> points)  
  12. {  
  13.     Sprite::init();  
  14.   
  15.     //设置随机数,控制出场精灵种类   
  16.     srand(time(NULL));  
  17.     ranNum = rand()%5;  
  18.   
  19.     p = points;  
  20.     start = p.begin();  
  21.   
  22.     switch (ranNum)  
  23.     {  
  24.     case 0 :  
  25.             {  
  26.                 s = Sprite::create("1.png");  
  27.                 break;  
  28.             }  
  29.     case 1:  
  30.             {  
  31.                 s = Sprite::create("2.png");  
  32.                 break;  
  33.             }  
  34.     case 2:  
  35.             {  
  36.                 s = Sprite::create("3.png");  
  37.                 break;  
  38.             }  
  39.     case 3:  
  40.             {  
  41.                 s = Sprite::create("4.png");  
  42.                 break;  
  43.             }  
  44.     case 4:  
  45.             {  
  46.                 s = Sprite::create("5.png");  
  47.                 break;  
  48.             }  
  49.     }  
  50.     s->setPosition((*start)->getPosition());  
  51.     addChild(s);  
  52.   
  53.     return true;  
  54. }  
  55.   
  56. //沿着路径移动   
  57. void Monster::moveByPath(){  
  58.     ++start;  
  59.     if(start == p.end()){  
  60.         s->removeFromParent();  
  61.     }  
  62.     else{  
  63.         Point a = Point((*start)->getPosition());  
  64.         s->runAction(Sequence::create(MoveTo::create(2,a),CallFuncN::create(CC_CALLBACK_0(Monster::moveByPath,this)),NULL));  
  65.     }  
  66. }  
#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

  1. #ifndef __HELLOWORLD_SCENE_H__   
  2. #define __HELLOWORLD_SCENE_H__   
  3.   
  4. #include "cocos2d.h"   
  5. #include "Monster.h"   
  6.   
  7. USING_NS_CC;  
  8.   
  9. class HelloWorld : public cocos2d::LayerColor  
  10. {  
  11. public:  
  12.   
  13.     static cocos2d::Scene* createScene();  
  14.     virtual bool init();    
  15.     CREATE_FUNC(HelloWorld);  
  16.     
  17.     void menuCloseCallback(cocos2d::Ref* pSender);  
  18.     
  19.   
  20.     //存放对象层里的坐标   
  21.     Vector<Node*> points;  
  22.     Vector<Node*>::iterator startPoint;  
  23.   
  24.   
  25.     //添加物体   
  26.     void addMonster();  
  27.   
  28.     //用于控制时间间隔   
  29.     int oldTime;  
  30.     int curTime;  
  31.     void resetTime();  
  32.   
  33.     void update(float dt);  
  34.   
  35.     //精灵   
  36.     Sprite *s;  
  37.   
  38.       
  39. };  
  40.   
  41. #endif // __HELLOWORLD_SCENE_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

  1. #include "HelloWorldScene.h"   
  2.   
  3. USING_NS_CC;  
  4.   
  5. Scene* HelloWorld::createScene()  
  6. {  
  7.      
  8.     auto scene = Scene::createWithPhysics();  
  9.   
  10.     auto layer = HelloWorld::create();  
  11.   
  12.     scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);  
  13.     scene->getPhysicsWorld()->setGravity(Point(0,-1000));  
  14.     
  15.     scene->addChild(layer);  
  16.   
  17.     return scene;  
  18. }  
  19.   
  20. // on "init" you need to initialize your instance   
  21. bool HelloWorld::init()  
  22. {  
  23.     //   
  24.     // 1. super init first   
  25.     if ( !LayerColor::initWithColor(Color4B(255,255,255,255)) )  
  26.     {  
  27.         return false;  
  28.     }  
  29.       
  30.     Size visibleSize = Director::getInstance()->getVisibleSize();  
  31.     Point origin = Director::getInstance()->getVisibleOrigin();  
  32.       
  33.   
  34.     //添加物理边界   
  35.     auto body = PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT,3);   
  36.     auto node = Node::create();  
  37.     node->setPhysicsBody(body);  
  38.     node->setPosition(visibleSize.width/2,visibleSize.height/2);  
  39.     addChild(node);  
  40.   
  41.     //添加地图文件   
  42.     auto map = TMXTiledMap::create("pokamon.tmx");  
  43.     map->setPosition(200,0);  
  44.     addChild(map);  
  45.       
  46.   
  47.     //获得对象层中的坐标,存在向量里   
  48.     TMXObjectGroup* objectGroup = map->getObjectGroup("monster");  
  49.     ValueVector object = objectGroup->getObjects();  
  50.       
  51.     for (ValueVector::iterator it = object.begin(); it != object.end(); it++) {  
  52.         Value obj = *it;  
  53.         ValueMap m = obj.asValueMap();  
  54.         auto node = Node::create();  
  55.         node->setPosition(m.at("x").asFloat()+200,m.at("y").asFloat());  
  56.         points.pushBack(node);  
  57.     }  
  58.       
  59.     //重置时间   
  60.     resetTime();  
  61.       
  62.     //开启计时器   
  63.     scheduleUpdate();  
  64.   
  65.     return true;  
  66. }  
  67.   
  68. void HelloWorld::update(float dt)  
  69. {  
  70.     ++oldTime;  
  71.     if (oldTime == curTime)  
  72.     {  
  73.         resetTime();  
  74.         addMonster();  
  75.     }  
  76. }  
  77.   
  78. void HelloWorld::resetTime()  
  79. {  
  80.     oldTime = 0;  
  81.     curTime = 40;  
  82. }  
  83.   
  84. void HelloWorld::addMonster()  
  85. {  
  86.     auto hero = Monster::create(points);  
  87.     hero->moveByPath();  
  88.     addChild(hero);  
  89. }  
  90.   
  91.   
  92. void HelloWorld::menuCloseCallback(Ref* pSender)  
  93. {  
  94. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)   
  95.     MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");  
  96.     return;  
  97. #endif   
  98.   
  99.     Director::getInstance()->end();  
  100.   
  101. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)   
  102.     exit(0);  
  103. #endif   
  104. }  
#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
}

效果:



扩展阅读:

【Cocos2d-x 3.3】实战项目《口袋空战》核心源码(1)

【Cocos2d-x 3.3】实战项目《口袋空战》核心源码(2)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值