标签:
选门游戏:
.h
1 #ifndef __BOSS_TWO_SCENE_H__ 2 #define __BOSS_TWO_SCENE_H__ 3 4 #include "cocos2d.h" 5 #include "iostream" 6 7 // 对于错的图片名 8 #define YesSprite "BossResources/BossTwo/yes.png" 9 #define NoSprite "BossResources/BossTwo/no.png" 10 11 using namespace std; 12 USING_NS_CC; 13 14 15 class BossTwo : public CCLayer{ 16 17 public: 18 19 static CCScene* createScene(); 20 virtual bool init(); 21 CREATE_FUNC(BossTwo); 22 23 void menuBackCallBack(CCObject* pSender); 24 25 // 初始化成员变量 26 void initMemebers(); 27 28 // 添加门框和门 29 void addDoorAndFrame(int num); 30 31 // 获取随机数,(start -- end)之间的 32 int getRandom(int start, int end); 33 34 virtual void onEnter(); 35 virtual void onExit(); 36 virtual bool ccTouchBegan(CCTouch *touch, CCEvent *event); 37 38 // 移除门 39 void removeCallBack(CCNode *pSender); 40 41 // 添加对错图片 42 void addYesAndNoCallBack(CCNode *pSender); 43 44 // 添加对错图片 45 void addYesAndNo(CCPoint position, string str); 46 47 48 private: 49 50 CCSprite *bg1; // 背景 51 CCSize size; // 屏幕大小 52 53 vector<string> vecDoor; // 门数组 54 vector<string> vecDoorFrame; // 门框数组 55 vector<string> vecSmoke; // 烟雾 56 int nSuccess; // 存储第几个门是对的 57 58 59 }; 60 61 #endif //__BOSS_TWO_SCENE_H__
.cpp
1 #include "BossTwo.h" 2 3 4 //CCNode进入场景时调用 5 void BossTwo::onEnter() 6 { 7 CCDirector* pDirector = CCDirector::sharedDirector(); 8 pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 9 CCLayer::onEnter(); 10 } 11 12 //CCNode退出场景时调用 13 void BossTwo::onExit() 14 { 15 CCDirector* pDirector = CCDirector::sharedDirector(); 16 pDirector->getTouchDispatcher()->removeDelegate(this); 17 CCLayer::onExit(); 18 } 19 20 21 CCScene* BossTwo::createScene(){ 22 23 CCScene *scene = CCScene::create(); 24 CCLayer *layer = BossTwo::create(); 25 scene->addChild( layer ); 26 return scene; 27 28 } 29 30 bool BossTwo::init(){ 31 32 if (!CCLayer::init()){ 33 return false; 34 } 35 36 srand(time(NULL)); 37 38 size = CCDirector::sharedDirector()->getVisibleSize(); 39 40 bg1 = CCSprite::create( "BossResources/BossTwo/bg1.png" ); 41 bg1->setPosition( CCPoint( size.width / 2, size.height / 2 ) ); 42 this->addChild(bg1); 43 44 addDoorAndFrame(3); 45 46 CCSprite *spriteNo = CCSprite::create("BossResources/BossTwo/error.png"); 47 CCSprite *spriteYes = CCSprite::create("BossResources/BossTwo/right.png"); 48 49 spriteNo->setPosition(CCPoint(size.width * 0.4, size.height * 0.9)); 50 spriteYes->setPosition(CCPoint(size.width * 0.6, size.height * 0.9)); 51 52 this->addChild(spriteYes, 10); 53 this->addChild(spriteNo, 11); 54 55 return true; 56 57 } 58 59 void BossTwo::initMemebers(){ 60 61 // 初始化门框 62 vecDoorFrame.push_back("BossResources/BossTwo/errordoorframe1.png"); 63 vecDoorFrame.push_back("BossResources/BossTwo/errordoorframe2.png"); 64 vecDoorFrame.push_back("BossResources/BossTwo/rightdoorfame.png"); 65 66 // 初始化门 67 vecDoor.push_back("BossResources/BossTwo/errordoor.png"); 68 vecDoor.push_back("BossResources/BossTwo/door.png"); 69 vecDoor.push_back("BossResources/BossTwo/door.png"); 70 71 // 初始化烟雾 72 vecSmoke.push_back("BossResources/BossTwo/dayanwu.png"); 73 vecSmoke.push_back("BossResources/BossTwo/xiaoyanwu.png"); 74 vecSmoke.push_back(""); 75 76 } 77 78 void BossTwo::addDoorAndFrame(int num){ 79 80 81 initMemebers(); // 每次调用的时候都初始化一下数据 82 83 int nums = num; // 中间变量 84 85 for (int i = 0; i < num; i++) // 添加num个门 86 { 87 88 int randoms = CCRANDOM_0_1() * nums; // 获取随机数 89 90 // 创建一个门框 91 CCSprite *sprite = CCSprite::create(vecDoorFrame.at(randoms).c_str()); 92 CCSprite *sprite2 = CCSprite::create(vecDoor.at(randoms).c_str()); 93 94 CCSprite *sprite3; 95 if (vecSmoke.at(randoms) == ""){ 96 sprite3 = CCSprite::create(); 97 } 98 else{ 99 sprite3 = CCSprite::create(vecSmoke.at(randoms).c_str()); 100 } 101 102 sprite->setTag(100 + i); // 设置门框的tag为100 + i 103 104 sprite2->setTag(200 + i); // 设置门的tag是200 + i 105 106 sprite3->setTag(300 + i); // 设置烟雾的tag是300 + i 107 108 sprite->setPosition(CCPoint(size.width / (num + 1) * (i + 1), size.height / 2.5)); 109 110 sprite2->setPosition(sprite->getPosition()); // 门的位置是在门框上的 111 112 sprite3->setAnchorPoint(CCPoint(0.5, 0.0)); // 烟雾的锚点是下中部分 113 114 sprite3->setPosition(CCPoint( // 烟雾的位置:门的上面 115 sprite2->getPositionX(), 116 sprite2->getPositionY() + sprite2->getContentSize().height / 2)); 117 118 // 将门框添加到场景中 119 this->addChild(sprite); 120 this->addChild(sprite2); 121 this->addChild(sprite3); 122 123 CCLOG("---------- ===========", vecDoorFrame.at(randoms).c_str()); 124 if (vecDoorFrame.at(randoms) == "BossResources/BossTwo/rightdoorfame.png"){ 125 nSuccess = sprite->getTag(); // 存储对的门的tag 126 CCLOG("---- tag = %d ----", sprite->getTag()); 127 } 128 129 // 删除门框数组中的值 130 vecDoorFrame.erase(vecDoorFrame.begin() + randoms); 131 vecDoor.erase(vecDoor.begin() + randoms); 132 vecSmoke.erase(vecSmoke.begin() + randoms); 133 134 nums--; // 随机数的取值减1 135 136 } 137 138 } 139 140 // 获取随机数,这里没有用到 141 int BossTwo::getRandom(int start, int end){ 142 float i = CCRANDOM_0_1() * (end - start + 1) + start; 143 return (int)i; 144 } 145 146 // 返回按钮 147 void BossTwo::menuBackCallBack(CCObject* pSender){ 148 149 } 150 151 152 153 bool BossTwo::ccTouchBegan(CCTouch *touch, CCEvent *event) 154 { 155 // 判断点击的是哪一个门框 156 for (int i = 100; i <= 102; i++) 157 { 158 CCSprite *sprite = dynamic_cast<CCSprite *>(this->getChildByTag(i)); 159 160 if (sprite->boundingBox().containsPoint(touch->getLocation())) 161 { 162 CCLOG("---- onTouchBegan ---- tag = %d", sprite->getTag()); 163 164 // 获取点击的门框的门 165 CCSprite *spriteDoor = dynamic_cast<CCSprite *>(this->getChildByTag(i + 100)); 166 CCMoveBy *moveByDoor = CCMoveBy::create(0.5f, CCPoint(spriteDoor->getContentSize().width, 0)); 167 spriteDoor->runAction( 168 CCSequence::create( 169 moveByDoor, 170 CCDelayTime::create(0.3f), 171 CCCallFuncN::create(spriteDoor, callfuncN_selector(BossTwo::removeCallBack)), 172 NULL)); 173 174 // 判断点击的是否是正确的门 175 if (nSuccess == sprite->getTag()){ 176 CCLOG("---- you are right ----"); 177 addYesAndNo(sprite->getPosition(), YesSprite); 178 } 179 else 180 { 181 CCLOG("---- you are error ----"); 182 addYesAndNo(sprite->getPosition(), NoSprite); 183 } 184 } 185 186 } 187 188 CCLog("BossTwo ccTouchBegan "); 189 return false; 190 } 191 192 void BossTwo::addYesAndNo(CCPoint position, string str){ 193 194 CCSprite *sprite = CCSprite::create(str.c_str()); 195 sprite->setPosition(position); 196 this->addChild(sprite , 100); 197 198 } 199 200 void BossTwo::removeCallBack(CCNode *pSender){ 201 CCSprite *spriteDoor = dynamic_cast<CCSprite *>(pSender); 202 CCLOG("---- psender tag = %d ----", spriteDoor->getTag()); 203 spriteDoor->removeFromParent(); 204 205 } 206 207 void BossTwo::addYesAndNoCallBack(CCNode *pSender){ 208 CCSprite *sprite = dynamic_cast<CCSprite *>(pSender); 209 CCLOG("---- psender tag = %d ----", sprite->getTag()); 210 // 判断点击的是否是正确的门 211 if (nSuccess == sprite->getTag()){ 212 CCLOG("---- you are right ----"); 213 addYesAndNo(sprite->getPosition(), YesSprite); 214 } 215 else 216 { 217 CCLOG("---- you are error ----"); 218 addYesAndNo(sprite->getPosition(), NoSprite); 219 } 220 }
效果图:
Cocos Studio ---------- 使用 C++ 开发游戏( 选门 )
标签:
原文地址:http://www.cnblogs.com/dudu580231/p/4829567.html