01.
这一回主要搭建大致的框架,写程序的过程中错误不断,磕磕碰碰才写到现在,可见自己的能力是多么的差,所以要多写程序。
多思考代码的逻辑,
这一章主要谢了一个测试ActionManager,
其中有CallFunc和CallFuncN有什么区别?
CC_CALLBACK_0
CC_CALLBACK_1
和其它几个有什么区别?
回答这两个问题,写到这里进行下一章学习。
#ifndef ACTIONMANAGERLAYER_H #define ACTIONMANAGERLAYER_H #include "cocos2d.h" USING_NS_CC; static const char s_pathGrossini[] = "Images/grossini.png"; enum { kTagNode, kTagGrossini, kTagSequence }; class ActionManagerLayer : public Layer { public: ActionManagerLayer(); virtual ~ActionManagerLayer(); CREATE_FUNC(ActionManagerLayer); virtual bool init(); static Scene *createScene(); private: void removeThis(); }; #endif // ACTIONMANAGERLAYER_H #include "ActionManagerLayer.h" ActionManagerLayer::ActionManagerLayer() { } ActionManagerLayer::~ActionManagerLayer() { } bool ActionManagerLayer::init() { if (!Layer::init()) return false; auto _screenSize = Director::getInstance()->getWinSize(); auto sprite = Sprite::create(s_pathGrossini); sprite->setPosition(Vec2(_screenSize.width/2, _screenSize.height/2)); addChild(sprite, 1, kTagGrossini); // Sum of all action's duration is 1.5 second. // sprite->runAction(RotateBy::create(1.5f, 90)); sprite->runAction(Sequence::create(DelayTime::create(1.4f), FadeOut::create(1.1f), nullptr)); // After 1.5 second, self will be removed. sprite->runAction(Sequence::create(DelayTime::create(1.4f), CallFunc::create(CC_CALLBACK_0(ActionManagerLayer::removeThis, this)), nullptr)); return true; } cocos2d::Scene *ActionManagerLayer::createScene() { Scene *scene = Scene::create(); Layer *layer = ActionManagerLayer::create(); scene->addChild(layer); return scene; } void ActionManagerLayer::removeThis() { auto sprite = getChildByTag(kTagGrossini); sprite->removeChild(sprite, this); } #include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include "ActionManagerLayer.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); auto layerColor = LayerColor::create(Color4B(200, 0, 0, 100)); // add layer as a child to scene scene->addChild(layer); scene->addChild(layerColor); // 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; } auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto _screenSize = Director::getInstance()->getWinSize(); / // 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(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); / // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label /* auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); // position the label on the center of the screen label->setPosition(Vec2(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(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); */ /* std::ostringstream osr; Vector<MenuItem *>menuItems; MenuItemFont *(menuItem[64]); for (int i = 0 ;i < 16; i++) { for (int j = 0; j < 4; j++) { osr << "menuItem_" << (i*4 + j); menuItem[i*4+j] = MenuItemFont::create(osr.str()); menuItem[i*4+j]->setPosition(Vec2(_screenSize.width*(j+1)/4-140, _screenSize.height/16*(16-i)-20)); menuItems.pushBack(menuItem[i*4+j]); osr.str(""); } } auto mainMenu = Menu::createWithArray(menuItems); mainMenu->setPosition(Vec2::ZERO); this->addChild(mainMenu); auto director = Director::getInstance(); menuItem[0]->setString("ActionManager"); menuItem[0]->setCallback([&](Ref *sender) { director->replaceScene(ActionManagerLayer::createScene()); }); */ auto menuItem1 = MenuItemFont::create("ActionManager"); menuItem1->setPosition(Vec2(_screenSize.width/4 - 140, _screenSize.height/16*15)); menuItem1->setCallback([&](Ref *sender) { Director::getInstance()->replaceScene(ActionManagerLayer::createScene()); }); Vector<MenuItem *> menuItems; menuItems.pushBack(menuItem1); auto mainMenu = Menu::createWithArray(menuItems); mainMenu->setPosition(Vec2::ZERO); this->addChild(mainMenu); return true; return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { //Close the cocos2d-x game scene and quit the application Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif /*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/ //EventCustom customEndEvent("game_scene_close_event"); //_eventDispatcher->dispatchEvent(&customEndEvent); } #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__ #ifndef _APP_DELEGATE_H_ #define _APP_DELEGATE_H_ #include "cocos2d.h" /** @brief The cocos2d Application. Private inheritance here hides part of interface from Director. */ class AppDelegate : private cocos2d::Application { public: AppDelegate(); virtual ~AppDelegate(); virtual void initGLContextAttrs(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. @return false Initialize failed, app terminate. */ virtual bool applicationDidFinishLaunching(); /** @brief Called when the application moves to the background @param the pointer of the application */ virtual void applicationDidEnterBackground(); /** @brief Called when the application reenters the foreground @param the pointer of the application */ virtual void applicationWillEnterForeground(); }; #endif // _APP_DELEGATE_H_ #include "AppDelegate.h" #include "HelloWorldScene.h" #include "SimpleAudioEngine.h" USING_NS_CC; using namespace CocosDenshion; // static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); static cocos2d::Size designResolutionSize = cocos2d::Size(1280, 720); static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate() { } // if you want a different context, modify the value of glContextAttrs // it will affect all platforms void AppDelegate::initGLContextAttrs() { // set OpenGL context attributes: red,green,blue,alpha,depth,stencil GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; GLView::setGLContextAttrs(glContextAttrs); } // if you want to use the package manager to install more packages, // don't modify or remove this function static int register_all_packages() { return 0; //flag for packages manager } bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) glview = GLViewImpl::createWithRect("cocosdemo", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); #else glview = GLViewImpl::create("cocosdemo"); #endif director->setOpenGLView(glview); } // turn on display FPS director->setDisplayStats(false); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0f / 30); // Set the design resolution glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::EXACT_FIT); /* auto frameSize = glview->getFrameSize(); // if the frame's height is larger than the height of medium size. if (frameSize.height > mediumResolutionSize.height) { director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); } // if the frame's height is larger than the height of small size. else if (frameSize.height > smallResolutionSize.height) { director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); } // if the frame's height is smaller than the height of medium size. else { director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); } */ register_all_packages(); std::vector<std::string> searchPaths; searchPaths.push_back(""); auto fileUtils = FileUtils::getInstance(); fileUtils->setSearchPaths(searchPaths); // create a scene. it's an autorelease object auto scene = HelloWorld::createScene(); // run director->runWithScene(scene); return true; } // This function will be called when the app is inactive. Note, when receiving a phone call it is invoked. void AppDelegate::applicationDidEnterBackground() { Director::getInstance()->stopAnimation(); // if you use SimpleAudioEngine, it must be paused // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { Director::getInstance()->startAnimation(); // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); }