Created
February 19, 2013 21:25
-
-
Save prototechno/4990091 to your computer and use it in GitHub Desktop.
Cocos2d-xでシングルトンによる画面間データ連携 ref: http://qiita.com/items/5eca8aab0dbd3f33273c
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
// | |
// GameManager.cpp | |
// SingletonTEST | |
// | |
// Created by prototechno on 2013/02/20. | |
// | |
// | |
#include "SimpleAudioEngine.h" | |
#include "GameManager.h" | |
using namespace cocos2d; | |
using namespace CocosDenshion; | |
//All static variables need to be defined in the .cpp file | |
//I've added this following line to fix the problem | |
GameManager* GameManager::m_mySingleton = NULL; | |
GameManager::GameManager() | |
{ | |
} | |
GameManager* GameManager::sharedGameManager() | |
{ | |
//If the singleton has no instance yet, create one | |
if(NULL == m_mySingleton) | |
{ | |
//Create an instance to the singleton | |
m_mySingleton = new GameManager(); | |
} | |
//Return the singleton object | |
return m_mySingleton; | |
} |
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
// | |
// GameManager.h | |
// SingletonTEST | |
// | |
// Created by prototechno on 2013/02/20. | |
// | |
// | |
#ifndef __SingletonTEST__GameManager__ | |
#define __SingletonTEST__GameManager__ | |
typedef enum{ | |
GameModeMenu , | |
GameModeNagasa , | |
GameModeKasa , | |
}GameMode; | |
#include "cocos2d.h" | |
class GameManager | |
{ | |
private: | |
GameManager(); | |
static GameManager* m_mySingleton; | |
public: | |
static GameManager* sharedGameManager(); | |
GameMode gameMode; | |
int count; | |
float gameTime; | |
}; | |
#endif /* defined(__SingletonTEST__GameManager__) */ |
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 "GameManager.h" | |
void Play5::countTimer(float time) | |
{ | |
GameManager::sharedGameManager()->gameTime += time; | |
//時間を表示する | |
CCString* timeString = CCString::createWithFormat("%8.0f秒", GameManager::sharedGameManager()->gameTime); | |
CCLabelTTF* timerLabel = (CCLabelTTF*)this->getChildByTag(kTagTimerLabel); | |
timerLabel->setString(timeString->getCString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment