cocos2d_x_01_環境搭建,cocos2d_x_01搭建

來源:互聯網
上載者:User

cocos2d_x_01_環境搭建,cocos2d_x_01搭建
最終:


Cocos2d-x-3.3 Mac 安裝:

參考文檔: 


線上API列表: 
Cocos2d-x-3.3 版本從配置安裝到建立項目都是命令列

  • 官網下載最新版本Cocos2d-x-3.3,大小約為280M 

    • 解壓後,在【終端】中切換目錄到 解壓後的目錄,然後執行./setup.py,斷行符號,如所示.
    • 期間會有幾次詢問,是否要設定安卓SDK路徑,
    • 如果尚未安裝Android開發環境,可以直接Enter跳過,暫不設定
    • (將來  可以在下面的檔案中,進行配置:/etc/profile)

123
 ->Please enter the path of NDK_ROOT (or press Enter to skip): ->Please enter the path of ANDROID_SDK_ROOT (or press Enter to skip): ->Please enter the path of ANT_ROOT (or press Enter to skip):


根據提示,敲擊命令: source /Users/history/.bash_profile然後 Enter,這樣就算設定好了.

1
Please execute command: "source /Users/history/.bash_profile" to make added system variables take effect


  • 最後就是建立工程.
  • 繼續命令列
  • cd tools/cocos2d-console/bin,
  • 接著使用下面命令即可:
  • cocos new 工程名 -p 包名 -l 語言 -d 目標檔案夾,
  • 例如 :
  • cocos newcocos2d_x -pcom.beyond -lcpp -d /Users/beyond/Desktop/project


  • 執行後就有如下提示,表示OK~
123456
Running command: new> Copy template into /Users/beyond/Desktop/project/cocos2d_x> Copying cocos2d-x files...> Rename project name from 'HelloCpp' to 'cocos2d_x'> Replace the project name from 'HelloCpp' to 'cocos2d_x'> Replace the project package name from 'org.cocos2dx.hellocpp' to 'com.beyond'


開啟自動建立好的項目可以選擇案頭應用,直接command+R,編譯運行


等了n分鐘過後,終於跑出來了~
後續補充一下:NDK目錄的配置進入終端,輸入命令:sudo nano /etc/profile開啟配置環境變數的檔案

根據實際情況,添加NDK_ROOT、ANT、SDK目錄最終,樣本如下:

下面是關於HelloWorld程式分析: Main.m入口
應用代理AppDelegate.h
#ifndef  _APP_DELEGATE_H_#define  _APP_DELEGATE_H_#include "cocos2d.h"/**@方法說明:    The cocos2d Application.The reason for implement as private inheritance is to hide some interface call by Director.*/// : private 表示 繼承過來的東東,全變成私人class  AppDelegate : private cocos2d::Application{public:    // 空參建構函式    AppDelegate();    // 解構函式    virtual ~AppDelegate();    virtual void initGLContextAttrs();    /**    @方法說明:    Implement Director and Scene init code here.    @返回: true    初始化成功,應用運行    @返回: false   初始化失敗,應用終止    */    virtual bool applicationDidFinishLaunching();    /**    @方法說明:  應用程式 進入後台後 調用    @參數:  the pointer of the application    */    virtual void applicationDidEnterBackground();    /**    @方法說明:  應用程式 將進入前台時調用    @參數:  the pointer of the application    */    virtual void applicationWillEnterForeground();};#endif // _APP_DELEGATE_H_


應用代理AppDelegate.cpp
#include "AppDelegate.h"#include "HelloWorldScene.h"USING_NS_CC;AppDelegate::AppDelegate() {}AppDelegate::~AppDelegate() {}//如果 需要一個不同的 上下文 context,只要修改 glContextAttrs 的值即可//it will takes effect on all platformsvoid AppDelegate::initGLContextAttrs(){    //設定 OpenGL context 屬性,目錄只能設定6個屬性    //red,green,blue,alpha,depth,stencil    GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};    GLView::setGLContextAttrs(glContextAttrs);}bool AppDelegate::applicationDidFinishLaunching() {    // 執行個體化導演類 director    auto director = Director::getInstance();    auto glview = director->getOpenGLView();    // 如果 glview為空白,則建立一個    if(!glview) {        glview = GLViewImpl::create("My Game");        director->setOpenGLView(glview);    }    // 顯示幀率 FPS    director->setDisplayStats(true);    // 設定幀率 FPS. 預設就是 1/60秒    director->setAnimationInterval(1.0 / 60);    // 建立情境,自動釋放 it's an autorelease object    auto scene = HelloWorld::createScene();    // 導演運行情境    director->runWithScene(scene);    return true;}// This function will be called when the app is inactive. When comes a phone call,it's be invoked toovoid AppDelegate::applicationDidEnterBackground() {    Director::getInstance()->stopAnimation();    // if you use SimpleAudioEngine, it must be pause    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();}// this function will be called when the app is active againvoid AppDelegate::applicationWillEnterForeground() {    Director::getInstance()->startAnimation();    // if you use SimpleAudioEngine, it must resume here    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();}


圖層Layer  HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"// 其實是繼承自Layerclass HelloWorld : public cocos2d::Layer{public:    // cpp裡面沒有id類型, so we recommend returning the class instance pointer    static cocos2d::Scene* createScene();    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();          // 【關閉菜單】點擊時的回調方法    void menuCloseCallback(cocos2d::Ref* pSender);        // implement the "static create()" method manually    CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__<span style="font-family:Courier New;color:#393939;"><span style="font-size: 24px; line-height: 32px; background-color: rgb(245, 245, 245);"><strong></strong></span></span>


圖層Layer   HelloWorldScene.cpp
#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){    // 'scene' 自動釋放    auto scene = Scene::create();        // 'layer' 自動釋放    auto layer = HelloWorld::create();    // 將圖層 添加到情境中    scene->addChild(layer);    // 返回 填充好圖層的 情境    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. 調用父類的init , cpp 沒有super,直接寫父類名    if ( !Layer::init() )    {        return false;    }    // 螢幕尺寸    Size visibleSize = Director::getInstance()->getVisibleSize();    // 2維座標    Vec2 origin = Director::getInstance()->getVisibleOrigin();    /////////////////////////////    // 2. add a menu item with "X" image, which is clicked to quit the program    // 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 = LabelTTF::create("Hello Beyond", "Marker Felt", 50);        // 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);        return true;}void HelloWorld::menuCloseCallback(Ref* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");    return;#endif    Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}

















vs2010搭建cocos2d-x 開發環境

你好 我用VS2012 + cocos2d-x 沒碰到你這問題
在xcode上安裝cocos2d-x也沒有你這個問題
我給你一個參考吧
v.youku.com/v_show/id_XNDY4NDcyOTQw.html

這個無腦碼農 cocos2d入門教程還是不錯的 我就是看這個學習的
希望可以幫到你!
 
cocos2d-x+eclipse開發環境搭建,盡可可以的詳細些

照著這篇文章來做即可。
www.cnblogs.com/...0.html
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.