Last active
October 13, 2015 18:38
-
-
Save tks2shimizu/4239351 to your computer and use it in GitHub Desktop.
card game 25 source code 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "HelloWorldScene.h" | |
#include "SimpleAudioEngine.h" | |
using namespace cocos2d; | |
using namespace CocosDenshion; | |
using namespace std; | |
CCScene* HelloWorld::scene() | |
{ | |
CCScene* scene = CCScene::create(); | |
HelloWorld* layer = HelloWorld::create(); | |
scene->addChild(layer); | |
return scene; | |
} | |
bool HelloWorld::init() | |
{ | |
if (!CCLayer::init()) | |
{ | |
return false; | |
} | |
//変数初期化 | |
nextNumber = 1; | |
srand((unsigned)time(NULL)); | |
//タップイベントを取得する | |
this->setTouchEnabled(true); | |
this->setTouchMode(kCCTouchesOneByOne); | |
//ボタンを作成し配置する | |
makeButtons(); | |
return true; | |
} | |
void HelloWorld::makeButtons() | |
{ | |
//数値配列を初期化する | |
vector<int> numbers; | |
for (int i = 1; i <= 25; i++) | |
numbers.push_back(i); | |
//カードを5x5に配置する | |
for (int x = 0; x < 5; x++) | |
{ | |
for (int y = 0; y < 5; y++) | |
{ | |
//ランダムで1つの値を取得する | |
int index = rand() % numbers.size(); | |
//カードを生成する | |
CCString* fileName = CCString::createWithFormat("frontside%02d.png", numbers[index]); | |
CCSprite* button = CCSprite::create(fileName->getCString()); | |
button->setPosition(ccp((x + 0.5) * 128 + 160, (y + 0.5) * 128)); | |
button->setTag(numbers[index]); | |
this->addChild(button); | |
//数値配列から値を削除する | |
numbers.erase(numbers.begin() + index); | |
} | |
} | |
} | |
#pragma mark - タップイベント | |
bool HelloWorld::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent) | |
{ | |
return true; | |
} | |
void HelloWorld::ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent) | |
{ | |
//タップポイント取得 | |
CCPoint touchPoint = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView()); | |
CCNode* node = this->getChildByTag(nextNumber); | |
if (node->boundingBox().containsPoint(touchPoint)) { | |
//裏カードを作成する | |
CCSprite* button = CCSprite::create("backside.png"); | |
button->setPosition(node->getPosition()); | |
button->setTag(nextNumber); | |
this->addChild(button); | |
//表カードを削除する | |
node->removeFromParentAndCleanup(true); | |
nextNumber++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __HELLOWORLD_SCENE_H__ | |
#define __HELLOWORLD_SCENE_H__ | |
#include "cocos2d.h" | |
class HelloWorld : public cocos2d::CCLayer | |
{ | |
private: | |
int nextNumber; | |
void makeButtons(); | |
public: | |
virtual bool init(); | |
static cocos2d::CCScene* scene(); | |
CREATE_FUNC(HelloWorld); | |
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); | |
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); | |
}; | |
#endif // __HELLOWORLD_SCENE_H__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment