Cocos Studio study ---------- 使用CocosStudio1.6制作 界面,并结合代码制作游戏
使用CocosStudio1.6制作 界面,并结合代码制作游戏
Cocos Studio界面
代码:.h
1 #ifndef __BOSS_ONE_SCENE_H__ 2 #define __BOSS_ONE_SCENE_H__ 3 4 #include "cocos2d.h" 5 #include "cocos-ext.h" 6 7 #define BEGIN_TAG 100 8 #define END_TAG 111 9 10 using namespace cocos2d::extension; 11 USING_NS_CC; 12 using namespace cocos2d::gui; 13 14 class BossOne : public UILayer{ 15 16 public: 17 18 static CCScene* createScene(); 19 virtual bool init(); 20 CREATE_FUNC(BossOne); 21 // 初始化视图,cocos Studio 制作的界面 22 void initView(); 23 24 // 返回按钮的回调函数 25 void menuBackCallback(CCObject* pSender, TouchEventType type); 26 // 图片的回调函数 27 void imageCallback(CCObject* pSender, TouchEventType type); 28 29 void RemoveCallback(); 30 void addLidCallBack(); 31 void reductionCallBack(); 32 33 void visibleCallBack(CCNode *pSender); 34 35 // 添加火焰 36 void addFire(int tag); 37 38 // 获取从 start 到 end 的随机数 39 int getRand(int start, int end); 40 41 // 添加锅盖 42 void addLid(CCPoint position); 43 44 //// 触摸开始 45 //virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); 46 //virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); 47 //virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); 48 49 // 每0.5秒添加一个火焰 50 void addFireUpdate(float dt); 51 52 // 每2秒后删除火焰 53 void removeFireUpdate(float dt); 54 55 void addAnimation(UIImageView *temp); 56 57 void update(float dt); 58 59 private: 60 61 // 界面 62 Widget* m_wUI; 63 // 文本框 64 char strBigFireNums[256]; 65 66 UIButton * m_nButtonBack; // 返回按钮 67 UILabel * m_nLabelBigFire; // 大火焰的显示数量的 label 68 UILabel * m_nLabelSmallFire; // 小火焰的显示数量的 label 69 70 CCSprite *lidSprite; 71 72 std::vector<int> vecFire; 73 74 int m_nfireNums; 75 76 }; 77 78 79 #endif // __BOSS_ONE_SCENE_H__
.cpp
1 #include "BossOne.h" 2 3 CCScene* BossOne::createScene(){ 4 5 CCScene *scene = CCScene::create(); 6 CCLayer *layer = BossOne::create(); 7 scene->addChild(layer); 8 return scene; 9 10 } 11 12 bool BossOne::init(){ 13 14 if (!UILayer::init()){ 15 return false; 16 } 17 18 m_nfireNums = 0; 19 20 srand(time(NULL)); 21 22 // 初始化界面 23 initView(); 24 25 // 锅盖 26 lidSprite = CCSprite::create("BossResources/BossOne/lid.png"); 27 lidSprite->setPosition(CCPoint(-lidSprite->getContentSize().width, 0)); 28 this->addChild(lidSprite); 29 30 schedule(schedule_selector(BossOne::addFireUpdate), 1.0f); 31 scheduleUpdate(); 32 return true; 33 34 } 35 36 // 获取 start 到 end 的随机数 37 int BossOne::getRand(int start, int end) 38 { 39 float i = CCRANDOM_0_1()*(end - start + 1) + start; //产生一个从start到end间的随机数 40 return (int)i; 41 } 42 43 // 初始化视图,cocos Studio 制作的界面 44 void BossOne::initView(){ 45 46 // 界面 47 m_wUI = GUIReader::shareReader()->widgetFromJsonFile("BossResources/BossOne/BossOne.ExportJson"); 48 addWidget(m_wUI); 49 50 // 获取开始按钮 51 m_nButtonBack = dynamic_cast<UIButton*>(m_wUI->getChildByName("Button_Back")); 52 // 添加点击事件 53 m_nButtonBack->addTouchEventListener(this, toucheventselector(BossOne::menuBackCallback)); 54 //m_nButtonBack-> 55 56 // 获取 TextField 文本框 57 m_nLabelBigFire = dynamic_cast<UILabel*>(m_wUI->getChildByName("Label_SmallFire")); 58 59 // 获取 名字被抢注了 的图片 60 m_nLabelSmallFire = dynamic_cast<UILabel*>(m_wUI->getChildByName("Label_BigFire")); 61 62 for (int i = BEGIN_TAG; i <= END_TAG; i++) 63 { 64 UIImageView *temp = dynamic_cast<UIImageView*>(m_wUI->getChildByTag(i)); 65 temp->addTouchEventListener(this, toucheventselector(BossOne::imageCallback)); 66 67 temp->setVisible(false); // 不显示 68 temp->setTouchEnabled(false); // 不接受触摸事件 69 70 addAnimation(temp); 71 72 //int nNum = 3; 73 //CCAnimation* animation = CCAnimation::create(); 74 //for (int i = 1; i <= nNum; i++) 75 //{ 76 // char szName[100] = { 0 }; 77 // sprintf(szName, "BossResources/BossOne/huoyan/%d.png", i); 78 // animation->addSpriteFrameWithFileName(szName); //加载动画的帧 79 //} 80 //animation->setDelayPerUnit(2.8f / 14.0f); 81 //animation->setRestoreOriginalFrame(true); 82 //CCAnimate *animate = CCAnimate::create(animation); 83 84 //CCSprite *sprite = CCSprite::create(); 85 //sprite->setAnchorPoint(CCPoint(0.5, 0)); 86 //sprite->setPosition(temp->getPosition()); 87 //sprite->setTag(i + 100); 88 //sprite->setVisible(false); 89 //addChild(sprite); 90 91 //sprite->runAction(CCRepeatForever::create(animate)); 92 93 } 94 95 } 96 97 void BossOne::update(float dt){ 98 99 } 100 101 void BossOne::addAnimation(UIImageView *temp){ 102 103 int nNum = 3; 104 CCAnimation* animation = CCAnimation::create(); 105 for (int i = 1; i <= nNum; i++) 106 { 107 char szName[100] = { 0 }; 108 sprintf(szName, "BossResources/BossOne/huoyan/%d.png", i); 109 animation->addSpriteFrameWithFileName(szName); //加载动画的帧 110 } 111 animation->setDelayPerUnit(0.1f); 112 animation->setRestoreOriginalFrame(true); 113 CCAnimate *animate = CCAnimate::create(animation); 114 115 CCSprite *sprite = CCSprite::create(); 116 sprite->setAnchorPoint(CCPoint(0.5, 0)); 117 sprite->setPosition(temp->getPosition()); 118 sprite->setTag(temp->getTag() + 100); 119 sprite->setVisible(false); 120 addChild(sprite); 121 122 sprite->runAction(CCRepeatForever::create(animate)); 123 } 124 125 void BossOne::addFire(int tag){ 126 127 UIImageView *temp = dynamic_cast<UIImageView*>(m_wUI->getChildByTag(tag)); 128 //// 显示图片 129 //temp->setVisible(true); 130 CCSprite *sprite = dynamic_cast<CCSprite*>(this->getChildByTag(tag + 100)); 131 // 显示图片 132 sprite->setVisible(true); 133 134 // 可以接受触摸事件 135 temp->setTouchEnabled(true); 136 137 schedule(schedule_selector(BossOne::removeFireUpdate), 2.0f); 138 139 140 } 141 142 // 返回按钮的回调函数 143 void BossOne::menuBackCallback(CCObject* pSender, TouchEventType type){ 144 145 if (type == TouchEventType::TOUCH_EVENT_ENDED) 146 { 147 CCLOG("---- menuBackCallback ----"); 148 } 149 150 } 151 152 // 图片的回调函数 153 void BossOne::imageCallback(CCObject* pSender, TouchEventType type) 154 { 155 if (type == TouchEventType::TOUCH_EVENT_ENDED) 156 { 157 UIImageView *temp = dynamic_cast<UIImageView*>(pSender); 158 addLid(temp->getPosition()); 159 std::vector<int>::iterator it; 160 for (it = vecFire.begin(); it != vecFire.end(); it++) 161 { 162 if ((*it) == temp->getTag()){ 163 it = vecFire.erase(it); //删除元素,返回值指向已删除元素的下一个位置 164 break; 165 } 166 } 167 temp->runAction( 168 CCSequence::create( 169 CCDelayTime::create(0.3), 170 CCCallFuncN::create(this, callfuncN_selector(BossOne::visibleCallBack)), 171 NULL)); 172 } 173 } 174 175 void BossOne::addLid(CCPoint position){ 176 177 // 获取屏幕的大小 178 CCSize size = CCDirector::sharedDirector()->getVisibleSize(); 179 // 屏幕的高度 180 float screen_height = size.height; 181 // 点击的位置的 x 轴值 182 float posX = position.x; 183 // 点击的位置的 y 轴值 184 float posY = position.y + 50; 185 186 lidSprite->setPosition(CCPoint(posX, lidSprite->getContentSize().height + screen_height)); 187 lidSprite->setVisible(true); 188 189 m_nfireNums++; 190 sprintf(strBigFireNums, "%02d", m_nfireNums); 191 m_nLabelBigFire->setText(strBigFireNums); 192 193 lidSprite->runAction( 194 CCSequence::create( 195 CCMoveTo::create(0.3f, CCPoint(posX, posY)), 196 CCDelayTime::create(0.1f), 197 CCCallFunc::create(this, callfunc_selector(BossOne::RemoveCallback)), 198 NULL 199 ) 200 ); 201 202 } 203 204 void BossOne::RemoveCallback(){ 205 lidSprite->setVisible(false); 206 } 207 208 209 void BossOne::addFireUpdate(float dt){ 210 // 添加火焰, 先随机获取一个tag,根据tag获取图片 211 int randomNum = getRand(BEGIN_TAG, END_TAG); 212 for (int i = 0; i < vecFire.size(); i++){ 213 if (randomNum == vecFire.at(i)){ 214 if (vecFire.size() != 12){ 215 addFireUpdate(0); 216 } 217 else{ 218 return; 219 } 220 return; 221 } 222 } 223 vecFire.push_back(randomNum); 224 addFire(randomNum); 225 } 226 227 void BossOne::removeFireUpdate(float dt){ 228 229 if (vecFire.size() > 0){ 230 int tag = vecFire.at(0); 231 UIImageView *temp = dynamic_cast<UIImageView*>(m_wUI->getChildByTag(tag)); 232 //temp->setVisible(false); // 不显示 233 CCSprite *sprite = dynamic_cast<CCSprite*>(this->getChildByTag(tag + 100)); 234 // 显示图片 235 sprite->setVisible(false); 236 temp->setTouchEnabled(false); // 不接受触摸事件 237 vecFire.erase(vecFire.begin()); 238 } 239 240 } 241 242 void BossOne::visibleCallBack(CCNode *pSender){ 243 UIImageView *temp = dynamic_cast<UIImageView*>(pSender); 244 // 点击图片后,图片隐藏 并且 不接受触摸事件了 245 //temp->setVisible(false); 246 CCSprite *sprite = dynamic_cast<CCSprite*>(this->getChildByTag(temp->getTag() + 100)); 247 // 显示图片 248 sprite->setVisible(false); 249 temp->setTouchEnabled(false); 250 }
效果图: