1.Tiled地图
Tiled Map Editor
新建地图
将图片拖动到图块的地方
然后就可以绘制地图了
2.创建跑步场景
在场景中加入tiled地图
#include "TollgateScene.h" Scene* TollgateScene::createScene(){ Scene* scene = Scene::create(); auto layer = TollgateScene::create(); scene->addChild(layer); return scene; } bool TollgateScene::init(){ if (!Layer::init()) { return false; } TMXTiledMap* map = TMXTiledMap::create("level01.tmx"); this->addChild(map); return true; }
运行,地图已经加载到场景中了
这边需要注意的是地图文件的图片路径经常会出错,改成相对路径即可
3.创建主角
还是习惯性的创建基类,主角类
Entity继承Node
#include "Entity.h" void Entity::bindSprite(Sprite* sprite){ mSprite = sprite; this->addChild(mSprite); }Player继承Entity
#include "Player.h" bool Player::init(){ return true; } void Player::run(){ }
//添加主角 void TollgateScene::addPlayer(TMXTiledMap* map){ Size visibleSize = Director::getInstance()->getVisibleSize(); Player* player = Player::create(); player->bindSprite(Sprite::create("run1.png")); player->run(); player->setPosition(Point(100,visibleSize.height/2)); map->addChild(player); }运行后,主角加载到场景中
4.固定主角的出生点,对象层的使用
Tiled Map的对象层功能
图层-->添加对象层,命名为objects
选中对象层,插入矩形,更改属性,改名为playerpoint
//添加主角 void TollgateScene::addPlayer(TMXTiledMap* map){ Size visibleSize = Director::getInstance()->getVisibleSize(); Player* player = Player::create(); player->bindSprite(Sprite::create("run1.png")); player->run(); //获取对象层 TMXObjectGroup* objectGroup = map->getObjectGroup("objects"); //加载玩家坐标 ValueMap playerPoint = objectGroup->getObject("PlayerPoint"); float playerX = playerPoint.at("x").asFloat(); float playerY = playerPoint.at("y").asFloat(); player->setPosition(Point(playerX,playerY)); map->addChild(player); }TmxObjectGroup对象存放了对象层的所有对象,通过getObjectGroup获取指定的对象层,于是我们就获得了对象层的所有对象,这些对象用一个Map的容器存放
ValueMap和Map类似,不过ValueMap限定了key值为string了
5.让主角跑起来,
我们可以用到上节的动画辅助类
void Player::run(){ SpriteFrameCache* frameCache = SpriteFrameCache::getInstance(); frameCache->addSpriteFramesWithFile("runs.plist","runs.png"); Animation* action = AnimationUtil::createWithStringFrameName("run",0.1f,-1); mSprite->runAction(Animate::create(action)); }
6.添加角色控制器
采用组合的形式让主角向前跑,把向前跑作为一个功能单独写成一个类,主角只要增加一个成员变量(向前跑的类)就能实现向前跑的动作。
①先创建控制器的基类,继承Node对象
Controller:设置监听对象
#ifndef _CONTROLLER_H_ #define _CONTROLLER_H_ #include "cocos2d.h" #include "ControllerListener.h" USING_NS_CC; class Controller : public Node { public: //设置监听对象 void setControllerListener(ControllerListener* listener); protected: ControllerListener* m_listener; }; #endif #include "Controller.h" void Controller::setControllerListener(ControllerListener* listener){ this->m_listener = listener; }
②创建监听对象接口
要被控制的对象,只要继承ControllerListener接口,就可以被Controller控制
#ifndef _CONTROLLER_LISTENER_H_ #define _CONTROLLER_LISTENER_H_ #include "cocos2d.h" class ControllerListener { public: //两个都是虚函数,c++的虚函数就是在后面加=0, //设置目标坐标 virtual void setTagPosition(int x,int y) = 0; //得到目标坐标 virtual cocos2d::Point getTagPosition() = 0; private: }; #endif
③创建简单移动控制器,继承自Controller
主要调用update来进行position的更新操作
#ifndef _SIMPLE_MOVE_CONTROLLER_H_ #define _SIMPLE_MOVE_CONTROLLER_H_ #include "Controller.h" #include "cocos2d.h" class SimpleMoveController : public Controller { public: CREATE_FUNC(SimpleMoveController); virtual bool init(); virtual void update(float dt); //设置速度 void setISpeed(int iSpeed); private: int m_iSpeed; }; #endif
#include "SimpleMoveController.h" bool SimpleMoveController::init(){ this->m_iSpeed = 0; //启用定时器 this->scheduleUpdate(); return true; } //设置速度 void SimpleMoveController::setISpeed(int iSpeed){ this->m_iSpeed = iSpeed; } void SimpleMoveController::update(float dt){ if (m_listener!=NULL) { Point pos = m_listener->getTagPosition(); //仅仅增加移动对象的x坐标 pos.x+=m_iSpeed; m_listener->setTagPosition(pos.x,pos.y); } }
m_listener是Controller的protected变量
④实体的基类继承ControllerListener类
实现了ControllerListener的两个方法,绑定Controller对象
#ifndef _ENTITY_H_ #define _ENTITY_H_ #include "cocos2d.h" #include "ControllerListener.h" #include "Controller.h" USING_NS_CC; class Entity : public Node,public ControllerListener { public: void bindSprite(Sprite* sprite); //实现SimpleMoveController的方法 virtual void setTagPosition(int x,int y); virtual Point getTagPosition(); //设置控制器 void setController(Controller* controller); protected: Sprite* mSprite; Controller* mController; }; #endif
#include "Entity.h" void Entity::bindSprite(Sprite* sprite){ mSprite = sprite; this->addChild(mSprite); } void Entity::setController(Controller* controller){ //绑定Controller对象 this->mController = controller; mController->setControllerListener(this); } void Entity::setTagPosition(int x,int y){ setPosition(Point(x,y)); } Point Entity::getTagPosition(){ return getPosition(); }
⑤当然是创建简单移动控制器,并绑定主角
//添加主角 void TollgateScene::addPlayer(TMXTiledMap* map){ Size visibleSize = Director::getInstance()->getVisibleSize(); Player* player = Player::create(); player->bindSprite(Sprite::create("run1.png")); player->run(); //获取对象层 TMXObjectGroup* objectGroup = map->getObjectGroup("objects"); //加载玩家坐标 ValueMap playerPoint = objectGroup->getObject("PlayerPoint"); float playerX = playerPoint.at("x").asFloat(); float playerY = playerPoint.at("y").asFloat(); player->setPosition(Point(playerX,playerY)); //创建简单的移动控制器 SimpleMoveController* controller = SimpleMoveController::create(); //设置速度为1 controller->setISpeed(1); //添加到节点中,才能调用刷新函数 this->addChild(controller); //给主角加上控制器 player->setController(controller); map->addChild(player); }
运行,截图(本想qq截图的,但是看下360,就没开qq了)
原文链接:https://www.f2er.com/cocos2dx/346368.html