【cocos2dx 3.2】瓦片地图制作
使用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
}
效果:
【cocos2dx 3.2】瓦片地图制作的更多相关文章
- Cocos2d-X研究之v3.x瓦片地图具体解释
在游戏开发过程中,我们会遇到超过屏幕大小的地图,比如即时战略游戏,使得玩家能够在地图中滚动游戏画面.这类游戏一般会有丰富的背景元素,假设直接使用背景图切换的方式,须要为每一个不同的场景准备一张背景图, ...
- 关于Cocos2d-x的瓦片地图
1.cocos2d-x的瓦片地图是用Tiled地图编辑器做的,这个软件开源,免费,一般都是用它制作瓦片地图. 2.瓦片地图是由块层和对象组成的,块层的作用是显示和一些重叠的时候覆盖角色的作用,而对象是 ...
- Cocos2d-x使用瓦片地图
图所示的复杂地图可以使用瓦片地图技术,瓦片地图是用一些小图片(瓦片)拼接而成,这样可以大大地减少内存消耗.如图所示的瓦片地图,只需要如图所示的三个瓦片就可以了. 瓦片地图 地图中的瓦片 瓦片地图的分类 ...
- cocos2dx进阶学习之瓦片地图编辑器
之前学习了瓦片地图类,现在我们来学习下瓦片地图制作工具 这个是开源的工具,可以从网上下载,下面我们演示下怎么做地图 步骤1 将需要用到的图片放到一个目录下,比如我机器上就是d:\tiled,这些图片是 ...
- cocos2d-x中的Tiled地图
cocos2d-x中的瓦片地图是通过tiledMap软件制作的,存档格式是.tmx格式.此软件的使用步骤简单总结如下: (1)制作瓦片地图 1 打开软件,软件界面如下图. 2. 新建地图(文件-> ...
- [SpriteKit] 制作瓦片地图小游戏
概述 SpriteKit制作瓦片地图游戏,深入了解2D游戏制作过程 详细 代码下载:http://www.demodashi.com/demo/10703.html 说实话这个2D游戏实战的入门看的我 ...
- Cocos2d-JS中瓦片地图API
为了访问瓦片地图,Cocos2d-JS中访问瓦片地图API,主要的类有:TMXTiledMap.TMXLayer和TMXObjectGroup等.1.TMXTiledMapTMXTiledMap是瓦片 ...
- android瓦片地图技术研究
最近根据公司项目需求,需要制作场馆的室内图并且实现根据rfid信号的自动定位功能,研究了好久找到了一个目前为止还算好用的瓦片地图工具——TileView. github连接:https://githu ...
- Google Map API V3调用arcgis发布的瓦片地图服务
由于最近项目需要用到CAD制作的地图,但之前一直使用的是用谷歌离线瓦片地图的方式,怎么样把CAD图像地图一样有缩放,移动的功能放到网页显示成了难题, 原先的谷歌地图的代码难道就不能用了?重新写一套代码 ...
随机推荐
- STM32 输入捕获的脉冲宽度及频率计算
输入捕获模式可以用来测量脉冲宽度或者测量频率.STM32 的定时器,除了 TIM6 和 TIM7,其他定时器都有输入捕获功能.以下是对脉冲宽度及频率的计算. 1.脉冲宽度 如下图所示,采集该高电平脉冲 ...
- 洛谷 P1598 垂直柱状图
P1598 垂直柱状图 题目描述 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过72个字符),然后用柱状图输出每个字符在输入文件中出现的次数.严格地按照输出样例来安排你的输出格式. ...
- phoenixframe自己主动化平台在Linux环境下运行用例的说明
phoenixframe自己主动化平台支持在Linux环境下使用phantomjs,Firefox.chrome运行測试用例.但有下面几个问题须要注意: 1.若无法启动phantomjs,Firefo ...
- 开源课程管理系统(CMS):Moodle
开源课程管理系统(CMS):Moodle 一.总结 1.php开发的cms,可借鉴参考用 二.Moodle(百度) Moodle(Modular Object-Oriented Dynamic Lea ...
- TTL和RS232之间的详细对比
[背景] 之前就听过TTL,一直没搞懂其和RS232的区别. 最近,打算去买个USB转RS232的芯片,结果找到此产品: 六合一多功能USB转UART串口模块CP2102 usb TTL485 232 ...
- Python 极简教程(七)列表 list
由于列表过于重要,请认真看完并保证所有代码都敲过一遍. 什么是列表 列表是 Python 中最常用的数据结构,也是一种数据类型,其样式如下: li = [1, 2, 3, 'a', 'b'] 列表是一 ...
- 洛谷—— P1967 货车运输 || COGS——C 1439. [NOIP2013]货车运输
https://www.luogu.org/problem/show?pid=1967#sub || http://www.cogs.pro/cogs/problem/problem.php?pi ...
- 机器学习算法中怎样选取超參数:学习速率、正则项系数、minibatch size
本文是<Neural networks and deep learning>概览 中第三章的一部分,讲机器学习算法中,怎样选取初始的超參数的值.(本文会不断补充) 学习速率(learnin ...
- 三星Galaxy Tab S2上市,压制苹果之心凸显
平板市场正在迎来史上最为关键的一次PK,众所周知,平板市场的苹果和三星一直是行业的领头羊,而在激烈的竞争中.三星平板似乎后劲更足.众多性能优异的产品频频推出.平板之王的称谓呼之欲出. 去年三星 ...
- Traveler Nobita (zoj 3456 最小生成树)
Traveler Nobita Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita used a time machin ...