日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg

發(fā)布時間:2024/9/27 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


1 游戲邏輯架構(gòu)

詳細(xì)介紹

A 一個導(dǎo)演同一時間只能運(yùn)行一個場景,場景當(dāng)中,可以同時加載多個層,一個層可以可載多個精靈。層中亦可以加層。

B? 場景切換

sceneàaddChild(layer);

layeràaddChild(sprite);

?

2 項目創(chuàng)建命令:

A 進(jìn)入tools下的project-creat

E:\Installed\cocos2d-x-2.2.3\tools\project-creator>

B

python create_project.py -project MyCocos2dx -package com.toto.mycocos01 -language cpp

C 命令解釋:

-project MyCocos2dx工程名

-package com.toto.mycocos01 包名

-language cpp 開發(fā)語言可選項目有javascript lua

D 創(chuàng)建后的項目目錄:

3 ?簡介

1 查看cocos2dx游戲的版本信息。

創(chuàng)建了一個cocos2dx項目之后,打開項目之后,會有如下項目結(jié)構(gòu)

展開libcocos2d,找到cocos2d.cpp,雙擊打開此cpp文件,內(nèi)容如下:

?

#include "cocos2d.h"

?

NS_CC_BEGIN

?

const char* cocos2dVersion()

{

??? return "2.2.3";

}

?

NS_CC_END

?

截圖如下:

分析:

A? 由上可以看出項目的版本號是:2.2.3

B? 依賴的頭文件 “cocos2d.h”

?

2 查看程序入口

程序入口是:main.cpp

#include "main.h"

#include "AppDelegate.h"

#include "CCEGLView.h"

?

USING_NS_CC;

?

int APIENTRY _tWinMain(HINSTANCE hInstance,

?????????????????????? HINSTANCE hPrevInstance,

?????????????????????? LPTSTR??? lpCmdLine,

?????????????????????? int?????? nCmdShow)

{

??? UNREFERENCED_PARAMETER(hPrevInstance);

??? UNREFERENCED_PARAMETER(lpCmdLine);

?

??? // create the application instance

??? AppDelegate app;??? ?????????????????????????//Delegate:表示 委派為代表 n:代表

??? CCEGLView* eglView = CCEGLView::sharedOpenGLView();???

??? eglView->setViewName("MyCocos2dx");???? ????????????//程序的標(biāo)題

??? eglView->setFrameSize(480, 320);??????? ????????????//程序的尺寸

??? return CCApplication::sharedApplication()->run();?? //關(guān)于shared的一般是單例模式

}

進(jìn)入run函數(shù), run的代碼結(jié)構(gòu)如下(選中run(),再按F12進(jìn)行查看):

int CCApplication::run()

{

??? PVRFrameEnableControlWindow(false);

?

??? // Main message loop:

??? MSG msg;

??? LARGE_INTEGER nFreq;

??? LARGE_INTEGER nLast;

??? LARGE_INTEGER nNow;

?

??? QueryPerformanceFrequency(&nFreq);

??? QueryPerformanceCounter(&nLast);

?

??? // Initialize instance and cocos2d.

??? if (!applicationDidFinishLaunching())

??? {

??????? return 0;

??? }

?

??? CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView();

??? pMainWnd->centerWindow();

??? ShowWindow(pMainWnd->getHWnd(), SW_SHOW);

?

??? while (1)

??? {

??????? if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))

??????? {

??????????? // Get current time tick.

??????????? QueryPerformanceCounter(&nNow);

?

??????????? // If it's the time to draw next frame, draw it, else sleep a while.

??????????? if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)

??????????? {

??????????????? nLast.QuadPart = nNow.QuadPart;

??????????????? CCDirector::sharedDirector()->mainLoop();

??????????? }

??????????? else

??????????? {

??????????????? Sleep(0);

??????????? }

??????????? continue;

??????? }

?

??????? if (WM_QUIT == msg.message)

??????? {

??????????? // Quit message loop.

??????????? break;

??????? }

?

??????? // Deal with windows message.

??????? if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))

??????? {

??????????? TranslateMessage(&msg);

??????????? DispatchMessage(&msg);

??????? }

??? }

?

??? return (int) msg.wParam;

}

程序的入口:applicationDidFinishLaunching()

?

AppDelegate.cpp

bool AppDelegate::applicationDidFinishLaunching() {

??? // initialize director

??? CCDirector* pDirector = CCDirector::sharedDirector();

??? CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

?

??? pDirector->setOpenGLView(pEGLView);

???

??? // 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);??? //設(shè)置幀率

?

??? // create a scene. it's an autorelease object

??? CCScene *pScene = HelloWorld::scene();

?

??? // run

??? pDirector->runWithScene(pScene);

?

??? return true;

}

截圖:

?

HelloWorldScene.h?? HelloWorld類的本質(zhì)是一個層(CCLayer):

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__

?

#include "cocos2d.h"

?

class HelloWorld : public cocos2d::CCLayer

{

public:

??? // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone

??? virtual bool init();?

?

??? // there's no 'id' in cpp, so we recommend returning the class instance pointer

??? static cocos2d::CCScene* scene();

???

??? // a selector callback

??? void menuCloseCallback(CCObject* pSender);

???

??? // implement the "static node()" method manually

??? CREATE_FUNC(HelloWorld);

};

?

#endif // __HELLOWORLD_SCENE_H__

?

HelloWorldScene.cpp

#include "HelloWorldScene.h"

?

USING_NS_CC;

?

CCScene* HelloWorld::scene()

{

??? // 'scene' is an autorelease object

??? CCScene *scene = CCScene::create();

???

??? // 'layer' is an autorelease object

??? HelloWorld *layer = HelloWorld::create();

?

??? // add layer as a child to scene

??? scene->addChild(layer);

?

??? //return the scene

??? return scene;

}

?

// on "init" you need to initialize your instance

bool HelloWorld::init()

{

??? //

??? // 1. super init first

??? if ( !CCLayer::init() )

??? {

??????? return false;

??? }

???

??? CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

??? CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

?

??? /

??? // 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

??? CCMenuItemImage *pCloseItem = CCMenuItemImage::create(

??????????????????????????????????????? "CloseNormal.png",

??????????????????????????????????????? "CloseSelected.png",

??????????????????????????????????????? this,

??????????????????????????????????????? menu_selector(HelloWorld::menuCloseCallback));

???

??? pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,

??????????????????????????????? origin.y + pCloseItem->getContentSize().height/2));

?

??? // create menu, it's an autorelease object

??? CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);

??? pMenu->setPosition(CCPointZero);

??? this->addChild(pMenu, 1);

?

??? /

??? // 3. add your codes below...

?

??? // add a label shows "Hello World"

??? // create and initialize a label

???

??? CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

???

??? // position the label on the center of the screen

??? pLabel->setPosition(ccp(origin.x + visibleSize.width/2,

??????????????????????????? origin.y + visibleSize.height - pLabel->getContentSize().height));

?

??? // add the label as a child to this layer

??? this->addChild(pLabel, 1);

?

??? // add "HelloWorld" splash screen"

??? CCSprite* pSprite = CCSprite::create("HelloWorld.png");

?

??? // position the sprite on the center of the screen

??? pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

?

??? // add the sprite as a child to this layer

??? this->addChild(pSprite, 0);

???

??? return true;

}

?

?

void HelloWorld::menuCloseCallback(CCObject* pSender)

{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)

??? CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");

#else

??? CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

??? exit(0);

#endif

#endif

}

?

總結(jié):

1、對于cocos真正的初始化是在init()方法中

2CCScene中的? autorelease()完成了析構(gòu)的過程

3CCPointZero 表示的位置是CCPointMake(0,0);

4 CCApplicationProtocolCCApplicationAppDelegate)三個類的類關(guān)系介紹:

抽出代碼具體實(shí)現(xiàn):

優(yōu)點(diǎn):屏蔽了平臺的差異性,實(shí)現(xiàn)跨平臺

1?? CCApplicationProtocol 定義了接口

#ifndef __CC_APPLICATION_PROTOCOL_H__

#define __CC_APPLICATION_PROTOCOL_H__

?

NS_CC_BEGIN

?

enum TargetPlatform

{

??? kTargetWindows,

??? kTargetLinux,

??? kTargetMacOS,

??? kTargetAndroid,

??? kTargetIphone,

??? kTargetIpad,

??? kTargetBlackBerry,

??? kTargetNaCl,

??? kTargetEmscripten,

??? kTargetTizen,

??? kTargetWinRT,

??? kTargetWP8

};

?

/**

?* @addtogroup platform

?* @{

?* @js NA

?* @lua NA

?*/

?

class CC_DLL CCApplicationProtocol

{

public:

?

??? virtual ~CCApplicationProtocol() {}

?

??? /**

??? @brief??? Implement CCDirector and CCScene init code here.

??? @return true??? Initialize success, app continue.

??? @return false?? Initialize failed, app terminate.

??? */

??? virtual bool applicationDidFinishLaunching() = 0;???? //這個類是一個純虛函數(shù)

?

??? /**

??? @brief? The function be called when the application enter background

??? @param? the pointer of the application

??? */

??? virtual void applicationDidEnterBackground() = 0;

?

??? /**

??? @brief? The function be called when the application enter foreground

??? @param? the pointer of the application

??? */

??? virtual void applicationWillEnterForeground() = 0;

?

??? /**

??? @brief??? Callback by CCDirector for limit FPS.

??? @interval?????? The time, expressed in seconds, between current frame and next.

??? */

??? virtual void setAnimationInterval(double interval) = 0;

?

??? /**

??? @brief Get current language config

??? @return Current language config

??? */

virtual ccLanguageType getCurrentLanguage() = 0;

?

??? /**

???? @brief Get target platform

???? */

??? virtual TargetPlatform getTargetPlatform() = 0;

};

?

// end of platform group

/// @}

?

NS_CC_END

?

#endif??? // __CC_APPLICATION_PROTOCOL_H__

?

2? CCApplication 各個平臺不同的邏輯

3? AppDelegate 私有繼承了CCApplication 僅實(shí)現(xiàn)CCApplicationProtocol 里的接口

?

總結(jié)

以上是生活随笔為你收集整理的1 游戏逻辑架构,Cocos2d-x游戏项目创建,HelloWorld项目创建,HelloWorld程序分析,(CCApplicationProtocol,CCApplication,AppDeleg的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。