把新创建的空项目中的HelloworldScene类删除掉,自己写一个.
一步步学习,不能投机取巧,先从最简单的hello world开始!
#ifndef __HelloCpp__LoadingLayer__ #define __HelloCpp__LoadingLayer__ #include "cocos2d.h" /** @brief 游戏启动层类 * * 游戏启动会创建此类对象,通过工厂方法创建一个场景,并将自己(层)加入到场景中 */ class LoadingLayer : public cocos2d::CCLayer { public: // 初始化函数 virtual bool init(); // 工厂方法,生成场景对象 static cocos2d::CCScene * scene(); // 关闭菜单的回调 void menuCloseCallback(CCObject *pSender); // 静态创建类对象 static LoadingLayer * create(); }; #endif /* defined(__HelloCpp__LoadingLayer__) */
#include "LoadingLayer.h" USING_NS_CC; CCScene * LoadingLayer::scene() { // 使用工厂方法生成scene CCScene *pScene = CCScene::create(); // 使用工厂方法生成启动层对象 LoadingLayer *pLayer = LoadingLayer::create(); // 把启动层对象加入到场景中 pScene->addChild(pLayer); return pScene; } bool LoadingLayer::init() { if (!CCLayer::init()) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); // 添加图片菜单项 CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png", "CloseSelected.png", this, menu_selector(LoadingLayer::menuCloseCallback)); // 设置菜单项在屏幕的位置 pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width / 2, origin.y + pCloseItem->getContentSize().height / 2)); // 创建菜单并添加到层中 CCMenu *pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); // 创建标题 CCLabelTTF *pLabel = CCLabelTTF::create("Hello World", "Arial", 24); pLabel->setPosition(ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)); this->addChild(pLabel, 1); // 添加精灵 CCSprite *pSprite = CCSprite::create("HelloWorld.png"); pSprite->setPosition(ccp(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); this->addChild(pSprite, 0); return true; } LoadingLayer * LoadingLayer::create() { LoadingLayer *pRet = new LoadingLayer(); if (pRet && pRet->init()) { pRet->autorelease(); return pRet; } delete pRet; pRet = NULL; return NULL; } void LoadingLayer::menuCloseCallback(cocos2d::CCObject *pSender) { CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif return; }
时间: 2024-12-31 01:54:24