我们先看看cocos2dx里面的test工程,开始先在applicationDidFinishLaunching()里面创建scene和 layer,layer是TestController()。
这里主要是建立各种动作的测试。ActionsDemo函数里面比较重要的函数有restartCallback,nextCalback,backCallback3个函数。
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
CCSize designSize = CCSizeMake(480, 320);
if (screenSize.height > 320)
{
CCSize resourceSize = CCSizeMake(960, 640);
CCFileUtils::sharedFileUtils()->setResourceDirectory("hd");
pDirector->setContentScaleFactor(resourceSize.height/designSize.height);
}
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
CCScene * pScene = CCScene::create();
CCLayer * pLayer = new TestController();
pLayer->autorelease();
pScene->addChild(pLayer);
pDirector->runWithScene(pScene);
return true;
}
然后在layer层上,TestController构造函数里面设置各种菜单。
TestController::TestController()
: m_tBeginPos(CCPointZero)
{
// add close menu
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(s_pPathClose, s_pPathClose, this, menu_selector(TestController::closeCallback) );
CCMenu* pMenu =CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
pCloseItem->setPosition(ccp( VisibleRect::right().x - 30, VisibleRect::top().y - 30));
// add menu items for tests
m_pItemMenu = CCMenu::create();
for (int i = 0; i < TESTS_COUNT; ++i)
{
// #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
// CCLabelBMFont* label = CCLabelBMFont::create(g_aTestNames[i].c_str(), "fonts/arial16.fnt");
// #else
CCLabelTTF* label = CCLabelTTF::create(g_aTestNames[i].c_str(), "Arial", 24);
// #endif
CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestController::menuCallback));
m_pItemMenu->addChild(pMenuItem, i + 10000);
pMenuItem->setPosition( ccp( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) ));
}
m_pItemMenu->setContentSize(CCSizeMake(VisibleRect::getVisibleRect().size.width, (TESTS_COUNT + 1) * (LINE_SPACE)));
m_pItemMenu->setPosition(s_tCurPos);
addChild(m_pItemMenu);
setTouchEnabled(true);
addChild(pMenu, 1);
}
layer层TestContorller响应菜单的函数构造了一个新的Scene。
void TestController::menuCallback(CCObject * pSender)
{
// get the userdata, it's the index of the menu item clicked
CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
int nIdx = pMenuItem->getZOrder() - 10000;
// create the test scene and run it
TestScene* pScene = CreateTestScene(nIdx);
if (pScene)
{
pScene->runThisTest();
pScene->release();
}
}
新的场景继承自TestScene,在onEnter的时候画好菜单。
void TestScene::onEnter()
{
CCScene::onEnter();
//add the menu item for back to main menu
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
// CCLabelBMFont* label = CCLabelBMFont::create("MainMenu", "fonts/arial16.fnt");
//#else
CCLabelTTF* label = CCLabelTTF::create("MainMenu", "Arial", 20);
//#endif
CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestScene::MainMenuCallback));
CCMenu* pMenu =CCMenu::create(pMenuItem, NULL);
pMenu->setPosition( CCPointZero );
pMenuItem->setPosition( ccp( VisibleRect::right().x - 50, VisibleRect::bottom().y + 25) );
addChild(pMenu, 1);
}
runThisTest是一个重要的函数,比如ActionsTestScene类里面。
void ActionsTestScene::runThisTest()
{
s_nActionIdx = -1;
addChild(NextAction());
CCDirector::sharedDirector()->replaceScene(this);
}
CCLayer* NextAction()
{
++s_nActionIdx;
s_nActionIdx = s_nActionIdx % ACTION_LAYER_COUNT;
CCLayer* pLayer = CreateLayer(s_nActionIdx);
pLayer->autorelease();
return pLayer;
}
NextAction()会创建一个新层,比如ActionManual,这类继承ActionsDemo,onEnter()函数生成3个精灵,3个菜单,
void ActionsDemo::onEnter()
{
CCLayer::onEnter();
// Or you can create an sprite using a filename. only PNG is supported now. Probably TIFF too
m_grossini = CCSprite::create(s_pPathGrossini);
m_grossini->retain();
m_tamara = CCSprite::create(s_pPathSister1);
m_tamara->retain();
m_kathia = CCSprite::create(s_pPathSister2);
m_kathia->retain();
addChild(m_grossini, 1);
addChild(m_tamara, 2);
addChild(m_kathia, 3);
m_grossini->setPosition(ccp(VisibleRect::center().x, VisibleRect::bottom().y+VisibleRect::getVisibleRect().size.height/3));
m_tamara->setPosition(ccp(VisibleRect::center().x, VisibleRect::bottom().y+VisibleRect::getVisibleRect().size.height*2/3));
m_kathia->setPosition(ccp(VisibleRect::center().x, VisibleRect::bottom().y+VisibleRect::getVisibleRect().size.height/2));
// add title and subtitle
std::string str = title();
const char * pTitle = str.c_str();
CCLabelTTF* label = CCLabelTTF::create(pTitle, "Arial", 18);
addChild(label, 1);
label->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y - 30) );
std::string strSubtitle = subtitle();
if( ! strSubtitle.empty() )
{
CCLabelTTF* l = CCLabelTTF::create(strSubtitle.c_str(), "Thonburi", 22);
addChild(l, 1);
l->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y - 60) );
}
// add menu
CCMenuItemImage *item1 = CCMenuItemImage::create(s_pPathB1, s_pPathB2, this, menu_selector(ActionsDemo::backCallback) );
CCMenuItemImage *item2 = CCMenuItemImage::create(s_pPathR1, s_pPathR2, this, menu_selector(ActionsDemo::restartCallback) );
CCMenuItemImage *item3 = CCMenuItemImage::create(s_pPathF1, s_pPathF2, this, menu_selector(ActionsDemo::nextCallback) );
CCMenu *menu = CCMenu::create(item1, item2, item3, NULL);
menu->setPosition(CCPointZero);
item1->setPosition(ccp(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));
item2->setPosition(ccp(VisibleRect::center().x, VisibleRect::bottom().y+item2->getContentSize().height/2));
item3->setPosition(ccp(VisibleRect::center().x + item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));
addChild(menu, 1);
}
void ActionsDemo::onExit()
{
m_grossini->release();
m_tamara->release();
m_kathia->release();
CCLayer::onExit();
}
onExit()函数里面仅是释放在onEnter里面的3个精灵。
在子类ActionManual里面重要的函数有onEnter
void ActionManual::onEnter()
{
ActionsDemo::onEnter();
CCSize s = CCDirector::sharedDirector()->getWinSize();
m_tamara->setScaleX( 2.5f);
m_tamara->setScaleY( -1.0f);
m_tamara->setPosition( ccp(100,70) );
m_tamara->setOpacity( 128);
m_grossini->setRotation( 120);
m_grossini->setPosition( ccp(s.width/2, s.height/2));
m_grossini->setColor( ccc3( 255,0,0));
m_kathia->setPosition( ccp(s.width-100, s.height/2));
m_kathia->setColor( ccBLUE);
}
这里主要是建立各种动作的测试。ActionsDemo函数里面比较重要的函数有restartCallback,nextCalback,backCallback3个函数。
void ActionsDemo::restartCallback(CCObject* pSender)
{
CCScene* s = new ActionsTestScene();
s->addChild( RestartAction() );
CCDirector::sharedDirector()->replaceScene(s);
s->release();
}
void ActionsDemo::nextCallback(CCObject* pSender)
{
CCScene* s = new ActionsTestScene();
s->addChild( NextAction() );
CCDirector::sharedDirector()->replaceScene(s);
s->release();
}
void ActionsDemo::backCallback(CCObject* pSender)
{
CCScene* s = new ActionsTestScene();
s->addChild( BackAction() );
CCDirector::sharedDirector()->replaceScene(s);
s->release();
}