【cocos2dx 3.2】瓦片地图制作
来源:互联网 发布:淘宝网沙滩半身裙 编辑:程序博客网 时间:2024/04/29 05:39
使用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 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}
效果:
0 0
- 【cocos2dx 3.2】瓦片地图制作
- 【cocos2dx 3.2】瓦片地图制作
- cocos2dx之瓦片地图
- cocos2dx之瓦片地图
- 瓦片地图-Cocos2dx
- 百度瓦片地图制作
- cocos2dx进阶学习之瓦片地图编辑器
- cocos2dx 3.0之瓦片地图编辑器1
- cocos2dx源代码分析之TMXTiled 瓦片地图
- Cocos2dx-Tiled Map(瓦片地图)
- Cocos2dx——瓦片地图(Tiled Map)
- cocos2dx之加载瓦片地图tmx
- cocos2dx 瓦片地图中间有黑线
- 利用 Global mapper制作地图瓦片
- 制作百度地图离线JavaScript API加载本地瓦片地图
- cocos2dx 自学笔记 <四> 瓦片地图 Tiled Map 学习
- 地图瓦片
- 瓦片地图
- 第九周项目五 程序填充题(1)
- 现代信息检索作业5
- 职场分享:职场中你需要培养的习惯
- 在线重定义
- (5) Cocos2d-x 3.x开发——导入Cocostudio资源
- 【cocos2dx 3.2】瓦片地图制作
- poj 4089:电话号码
- nginx 请求失败代码解析
- http://blog.csdn.net/zh8727/article/details/39394561
- ERROR 2003 (HY000): Can't connect to MySQL server on '***.**.***.***' (110)
- 可以下拉刷新、上拉获取更多的SwipeRefreshAndLoadLayout
- Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
- split函数处理mysql时间问题及截取url
- Azure入门学习(三)