Skip to content

Instantly share code, notes, and snippets.

@jonnyijapan
Created May 14, 2015 01:53
Show Gist options
  • Save jonnyijapan/d79814f7b867ab5fe1f2 to your computer and use it in GitHub Desktop.
Save jonnyijapan/d79814f7b867ab5fe1f2 to your computer and use it in GitHub Desktop.
#include "HelloWorldScene.h"
USING_NS_CC;
ENCODABLE_OBJECT_DEFINE_CONSTRUCTOR(HelloWorld);//declare this for CCNSCoder
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
test_buttonpushcount = 0;
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
// auto closeItem = MenuItemImage::create(
// "CloseNormal.png",
// "CloseSelected.png",
// CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
//
// closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
// origin.y + closeItem->getContentSize().height/2));
auto button = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuButton, this));
button->setColor(cocos2d::Color3B::BLUE);
button->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// create menu, it's an autorelease object
auto menu = Menu::create(button, NULL);
// auto menu = Menu::create(closeItem, button, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
auto label = LabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen
label->setPosition(Point(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);
// // add "HelloWorld" splash screen"
// auto sprite = Sprite::create("HelloWorld.png");
//
// // position the sprite on the center of the screen
// sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
//
// // add the sprite as a child to this layer
// this->addChild(sprite, 0);
/*create and assign test values for CCNSCoding test*/
// test_bool = false;
// test_int = 99;
// for(int i = 0; i < 7; i++)
// test_vector.push_back(i);
//start saving to file
//this->save();
// bool result = false;
// CCNSCoder *cd1 = new CCNSCoder();
// if(cd1->saveObjectToFile("LocalData", this))
// result = true;
// delete cd1;
//reset test variable values
test_bool = true;
test_int = 0;
test_vector.clear();
CCLOG("Values before load, test_buttonpushcount: %d, test_int: %d", test_buttonpushcount, test_int);
//start loading from file
bool result = false;
CCNSCoder *cd2 = new CCNSCoder();
if(cd2->readObjectFromFile("LocalData", this)) {
result = true;
CCLOG("Load ok, test_buttonpushcount: %d, test_int: %d", test_buttonpushcount, test_int);
}
else {
CCLOGERROR("Load failed");
}
delete cd2;
{
auto labeltemp = LabelTTF::create("Count: xxx", "Arial", 24);
// position the label on the center of the screen
labeltemp->setPosition(Point(origin.x + visibleSize.width/2,
origin.y + label->getContentSize().height));
// add the label as a child to this layer
this->addChild(labeltemp, 1);
this->labelCount = labeltemp;
}
this->refreshbuttoncountlabel();
/*end*/
return true;
}
void HelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
void HelloWorld::menuButton(Object* pSender)
{
test_buttonpushcount++;
this->save();
this->refreshbuttoncountlabel();
}
void HelloWorld::save() {
bool result = false;
CCNSCoder *cd1 = new CCNSCoder();
if(cd1->saveObjectToFile("LocalData", this)) {
result = true;
CCLOG("Save ok");
}
else {
CCLOGERROR("Save failed");
}
delete cd1;
}
void HelloWorld::refreshbuttoncountlabel() {
auto ccstring = cocos2d::CCString::createWithFormat("Count: %d", test_buttonpushcount);
//std::string str = cocos2d::String(
this->labelCount->setString(ccstring->getCString());
}
#pragma mark CCNSCoding routine
void HelloWorld::encodeWithCoder(CCNSCoder *encoder)
{
encoder->encodeBoolForKey(test_bool, "testBool");
encoder->encodeIntForKey(test_int, "testInt");
encoder->encodeVectorForKey(&test_vector, "testVector");
CCLOG("Saving test_buttonpushcount: %d", test_buttonpushcount);
encoder->encodeIntForKey(test_buttonpushcount, "test_buttonpushcount");
}
void HelloWorld::initWithCoder(CCNSCoder *decoder)
{
test_bool = decoder->decodeBoolForKey("testBool");
test_int = decoder->decodeIntForKey("testInt");
test_vector = decoder->decodeVectorForKey<std::vector<int>>("testVector");
test_buttonpushcount = decoder->decodeIntForKey("test_buttonpushcount");
CCLOG("Loaded test_buttonpushcount: %d", test_buttonpushcount);
}
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "CCNSCoder.h"
class HelloWorld : public cocos2d::Layer,
public CCNSEncodableObject//Need inherit from CCNSEncodableObject
{
public:
/*test variables and functions for CCNSCoder*/
ENCODABLE_OBJECT_DECLARE_CONSTRUCTOR(HelloWorld);
virtual bool init();
int test_int;
bool test_bool;
int test_buttonpushcount;
std::vector<int> test_vector;
void initWithCoder(CCNSCoder *decoder);
void encodeWithCoder(CCNSCoder *coder);
/*end*/
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
// a selector callback
void menuCloseCallback(Object* pSender);
void menuButton(Object* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
void save();
void refreshbuttoncountlabel();
cocos2d::LabelTTF* labelCount;
};
#endif // __HELLOWORLD_SCENE_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment