1.用CocoStudio UI编辑按钮
设置画布大小800x500(奇怪的分辨率)
添加按钮,导出
D:\worktools\cocostests\HongGG\cocos2d\cocos\editor-support\cocostudio\proj.win32\libCocosStudio.vcxproj
D:\worktools\cocostests\HongGG\cocos2d\cocos\ui\proj.win32\libGUI.vcxproj
编辑TollgateScene.h,加入头文件
#include "editor-support/cocostudio/CCSGUIReader.h" #include "ui/CocosGUI.h" using namespace cocostudio; using namespace cocos2d::ui;
加入loadUI和jumpEvent方法
void loadUI();//加载控件 void jumpEvent(Ref*,TouchEventType type);//跳跃函数编辑cpp文件,记得在init方法中调用loadUI哦
void TollgateScene::loadUI(){ //加载编辑好的ui auto hUI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("LitterRunnerUI\LitterRunnerUI_1.ExportJson"); this->addChild(hUI); //根据名字找到控件 Button* jumpBtn = (Button*)Helper::seekWidgetByName(hUI,"jumpBtn"); //设置监听事件 jumpBtn->addTouchEventListener(this,toucheventselector(TollgateScene::jumpEvent)); } void TollgateScene::jumpEvent(Ref*,TouchEventType type){ switch (type) { //当点击结束的时候,调用主角的jump方法 case TouchEventType::TOUCH_EVENT_ENDED: m_player->jump(); break; default: break; } }
在头文件中加入 public: void jump();//跳跃函数 private: bool m_isJumping;//标记主角是否在跳跃cpp文件中实现方法
#include "Player.h" Player::Player() { m_isJumping = false;//初始化 } Player::~Player() { } bool Player::init(){ return true; } void Player::jump(){ //没有精灵时结束方法 if (getSprite() == NULL) { return; } //正在跳跃时结束方法 if (m_isJumping) { return; } m_isJumping = true; //执行2s,原地跳跃,跳跃高度200,执行1次 JumpBy* jumpBy = JumpBy::create(2.0f,Point(0,0),200,1); auto endFunc = [&](){ m_isJumping = false; }; auto callFunc = CallFunc::create(endFunc); Action* actions = Sequence::create(jumpBy,callFunc,NULL); this->runAction(actions); }至此,运行项目,点击按钮,主角已经可以跳跃了哦
2.加入怪物
在实体筛选器下新建怪物筛选器,新建怪物类
#ifndef _MONSTER_H_ #define _MONSTER_H_ #include "Entity.h" #include "Player.h" class Monster:public Entity { public: Monster(); ~Monster(); CREATE_FUNC(Monster); virtual bool init(); public: void show();//显示怪物 void hide();//隐藏怪物 void reset();//重置怪物 bool isAlive();//是否是活动状态 bool isCollideWithPlayer(Player* player);//碰撞比较 private: bool m_isAlive; }; #endif
#include "Monster.h" Monster::Monster(){ m_isAlive = false; } Monster::~Monster(){} bool Monster::init(){ return true; } void Monster::show(){ //如果怪物存在 if (getSprite() != NULL) { //设置为可见状态 this->setVisible(true); m_isAlive = true; } } void Monster::hide(){ if (getSprite() != NULL) { this->setVisible(false); reset(); m_isAlive = false; } } void Monster::reset(){ if (getSprite() != NULL) { this->setPosition(Point(800+CCRANDOM_0_1()*2000,200-CCRANDOM_0_1()*100)); } } bool Monster::isAlive(){ return m_isAlive; } bool Monster::isCollideWithPlayer(Player* player){ //获取碰撞检测对象的boundingBox Rect rect = player->getBoundingBox(); Point point = this->getPosition(); //判断是否有交集 return rect.containsPoint(point); }新建一个管理筛选器,在下面建一个怪物管理筛选器
#ifndef _MONSTER_MANGER_H_ #define _MONSTER_MANGER_H_ #include "cocos2d.h" #include "Monster.h" USING_NS_CC; #define MAX_MONSTER_NUM 10//最多10个怪物 class MonsterManger:public Node { public: MonsterManger(); ~MonsterManger(); CREATE_FUNC(MonsterManger); virtual bool init(); virtual void update(float dt);//更新函数 void bindPlayer(Player* player); private: void createMonsters();//创建怪物对象 Vector<Monster*> monsters;//存放怪物对象 Player* m_player;//主角 }; #endif
#include "MonsterManger.h" MonsterManger::MonsterManger() { } MonsterManger::~MonsterManger() { } bool MonsterManger::init(){ createMonsters();//创建怪物对象 this->scheduleUpdate();//开启update函数的调用 return true; } void MonsterManger::createMonsters(){ Monster* monster = NULL; for (int i=0;i<MAX_MONSTER_NUM;i++) { //创建怪物对象 monster = Monster::create(); monster->bindSprite(Sprite::create("monster.png")); monster->reset(); //将怪物添加进控制层????? this->addChild(monster); //保存怪物 monsters.pushBack(monster); } } //刷新界面 void MonsterManger::update(float dt){ for (auto monster:monsters)//遍历每一个怪物 { if (monster->isAlive())//如果怪物是活动的 { int speed = 4;//定义一个速度,向左移动 monster->setPositionX(monster->getPositionX()-speed); if (monster->getPositionX()<0)//怪物已经移出界面了 { monster->hide();//隐藏掉 }else if(monster->isCollideWithPlayer(m_player)) { m_player->hit();//掉血 monster->hide();//隐藏 } }else { monster->show();//不是活动的话就显示 } } } void MonsterManger::bindPlayer(Player* player){ this->m_player = player; }原文链接:https://www.f2er.com/cocos2dx/346560.html