//MyLayer.h #include "cocos2d.h" USING_NS_CC; class MyLayer : public CCLayer { public: static MyLayer* create(); bool init(); }; //MyLayer.cpp #include "MyLayer.h" MyLayer* MyLayer::create() { MyLayer *pRet = new MyLayer(); if (pRet && pRet->init()) { pRet->autorelease(); } else { delete pRet; pRet = NULL; } return pRet; } bool MyLayer::init() { if (!CCLayer::init()) { return false; } CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite* spr = CCSprite::create("1.png"); spr->setPosition(ccp(winSize.width / 2, winSize.height / 2)); addChild(spr); return true; } //MyScene.h #include "cocos2d.h" USING_NS_CC; class MyScene : public CCScene { public: static MyScene* create(); bool init(); }; //myScene.cpp #include "MyScene.h" #include "MyLayer.h" MyScene* MyScene::create() { MyScene *pRet = new MyScene(); if (pRet && pRet->init()) { pRet->autorelease(); } else { delete pRet; pRet = NULL; } return pRet; } bool MyScene::init() { MyLayer* layer = MyLayer::create(); this->addChild(layer); return true; }
appDelegate:
CCScene *pScene = MyScene::create();
director->runWithScene(pScene);