本系列學習教程使用的是cocos2d-x-2.1.4(最新版為cocos2d-x-2.1.5)
博主發現前兩個系列的學習教程被嚴重抄襲,在這裡呼籲大家請尊重開發人員的勞動成果,
轉載的時候請務必註明出處:http://blog.csdn.net/yangyu20121224/article/details/12067629
一、類的建立
類中添加代碼,.h標頭檔。
#ifndef __GAME_SCENE_H__#define __GAME_SCENE_H__#include "cocos2d.h"class GameLayer :public cocos2d::CCLayer{public://初始化方法virtual bool init();//建立情境static cocos2d::CCScene* scene();CREATE_FUNC(GameLayer);//初始化組件bool setupViews();};#endif
.cpp檔案,這段代碼有很詳細的注釋。
#include "GameScene.h"#include "StaticData.h"USING_NS_CC;//建立遊戲情境CCScene* GameLayer::scene(){CCScene* scene = CCScene::create();GameLayer* layer = GameLayer::create();scene->addChild(layer);return scene;}//初始化方法bool GameLayer::init(){bool isRet = false;do {CC_BREAK_IF(!this->setupViews());isRet = true;} while (0);return true;}// 初始化控制項和布景bool GameLayer::setupViews(){bool isRet = false;do {CCLOG("games initialization...");//獲得視窗尺寸大小CCSize winSize = CCDirector::sharedDirector()->getWinSize();//建立背景圖片精靈CCSprite* background = CCSprite::create(STATIC_DATA_STRING("game_background"));//設定精靈位置background->setPosition(CCPointMake(winSize.width * 0.5, winSize.height * 0.5));//添加精靈至圖層this->addChild(background);//加入plist檔案至緩衝CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(STATIC_DATA_STRING("GameLayer_plist"));//從緩衝讀取並建立精靈CCSprite* game_box01 = CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("game_ui_box_01"));//設定錨點game_box01->setAnchorPoint(ccp(0.5,1));//設定精靈位置game_box01->setPosition(CCPointMake(winSize.width * 0.5, winSize.height));//添加精靈至圖層this->addChild(game_box01);//從緩衝讀取並建立精靈CCSprite* game_box02 = CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("game_ui_box_02"));//設定錨點game_box02->setAnchorPoint(ccp(0.5,0));//設定精靈位置game_box02->setPosition(CCPointMake(winSize.width * 0.5, 0));//添加精靈至圖層this->addChild(game_box02,3);//建立背景圖片精靈CCSprite* ui_2p = CCSprite::create(STATIC_DATA_STRING("game_ui_2p"));//設定精靈位置ui_2p->setPosition(CCPointMake(winSize.width * 0.5, 41));//添加精靈至圖層this->addChild(ui_2p,2);isRet=true;} while (0);return isRet;}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>default_gold</key><string>200</string><key>title</key><string>title.png</string><key>background</key><string>background.png</string><key>StartScene_Texture</key><string>StartScene.plist</string><key>start_normal</key><string>ui_button_box02_02.png</string><key>start_selected</key><string>ui_button_box02_01.png</string><key>scene_normal</key><string>ui_button_box01_02.png</string><key>scene_selected</key><string>ui_button_box01_01.png</string><key>Button_Texture</key><string>Button.plist</string><key>start</key><string>ui_2p_010.png</string><key>scene</key><string>button_other_014.png</string><key>loading_title</key><string>loading_title.png</string><key>loading_1_1</key><string>loading_1_1.png</string><key>loading_1_2</key><string>loading_1_2.png</string><key>loading_2_1</key><string>loading_2_1.png</string><key>loading_2_2</key><string>loading_2_2.png</string><key>game_background</key><string>game_background.png</string><key>cannon_plist</key><string>cannon.plist</string><key>cannon</key><string>cannon.png</string><key>cannon10_plist</key><string>cannon10.plist</string><key>cannon10</key><string>cannon10.png</string><key>increase_button</key><string>increase_button.png</string><key>reduce_button</key><string>reduce_button.png</string><key>GameLayer_plist</key><string>GameLayer.plist</string><key>game_ui_2p</key><string>game_ui_2p.png</string><key>game_ui_box_01</key><string>ui_box_01.png</string><key>game_ui_box_02</key><string>ui_box_02.png</string></dict></plist>
//點擊“開始遊戲”按鈕的回調void StartLayer::start_callback(CCObject* pSender){ CCLOG( "start game");CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.0f, GameLayer::scene()));}