cocos2dx 3.2的瓦片地图制作
2015年03月17日 17:45
0 点赞
0 评论
更新于 2025-11-21 17:34
今天我们继续学习cocos2dx 3.2的应用实例,使用cocos2dx 3.2进行瓦片地图制作。
使用Tiled编辑地图
在使用Tiled编辑地图时,需要注意以下几点:
- 每一个图层只能放置一种瓦片。
- 瓦片的大小最好是32 * 32的倍数,这样可以保证地图的显示效果和性能。
- 在对象层里面设置路径的坐标,后续在主程序中获取对象层中的坐标并进行相应的操作。
代码实现
口袋精灵类(Monster)
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)
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Monster.h"
USING_NS_CC;
class HelloWorld : public 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
}
效果
通过上述代码,我们可以实现使用cocos2dx 3.2制作瓦片地图,并让口袋精灵沿着预设的路径移动。具体的显示效果可以根据实际情况进行调整和优化。