cocos2d_x_01_环境搭建_普通网友的博客-程序员宅基地

技术标签: python  游戏  移动开发  

终于效果图:



Cocos2d-x-3.3 Mac 安装
下载地址:


參考文档:  



在线API列表: 

Cocos2d-x-3.3 版本号
配置安装创建项目都是命令行
  • 官网下载最新版本号Cocos2d-x-3.3,大小约为280M 

    • 解压后,在【终端】中切换文件夹到 解压后的文件夹,然后运行./setup.py,回车,例如以下图所看到的.
    • 期间会有几次询问,是否要设置安卓SDK路径,
    • 假设尚未安装Android开发环境,能够直接Enter跳过,暂不设置
    • (将来  能够在以下的文件里,进行配置:/etc/profile)

1
2
3
 ->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


  • 最后就是创建project.
  • 继续命令行
  • cd tools/cocos2d-console/bin,
  • 接着使用以下命令就可以:
  • cocos new project名 -p 包名 -l 语言 -d 目标目录,
  • 比如 :
  • cocos newcocos2d_x -pcom.beyond -lcpp -d /Users/beyond/Desktop/project


  • 运行后就有例如以下提示,表示OK~
1
2
3
4
5
6
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 platforms
void 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 too
void 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 again
void 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"
// 事实上是继承自Layer
class 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 instance
bool 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
}
















版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_33800593/article/details/85901715

智能推荐

charAt()方法获取指定位置的字符_Inspire2023的博客-程序员宅基地

package com;public class GetCharAt { public static void main(String[] args) { String str ="明月几时有,把酒问青天"; System.out.println(str); char c =str.charAt(2); System.out.println("字符串中索引为2的字符是:"+...

item_get - 获得淘宝商品详情(淘宝)_johnny3282278043的博客-程序员宅基地

item_get - 获得淘宝商品详情返回数据结果:返回值说明:

prometheus学习_scrape duration_小卒曹阿瞒的博客-程序员宅基地

类似于直方图,但它还会计算百分位数。直方图可以很好地展现时间序列数据,尤其适用于数据的可视化(如应用程序延迟等)。通常来说,单个指标对我们价值很小,往往需要联合并可视化多个指标,这其中需要应用一些数学变换。例如,我们可能会将统计函数应用于指标或指标组,一些可能应用的常见函数包括:·计数:计算特定时间间隔内的观察点数。·求和:将特定时间间隔内所有观察点的值累计相加。·平均值:提供特定时间间隔内所有值的平均值。·中间数:数值的几何中点,正好50%的数值位于它前面,而另外50%则位于它后面。_scrape duration

为什么你要在2021年学习Go | Gopher Daily (2021.05.21) ʕ◔ϖ◔ʔ_Tony Bai的博客-程序员宅基地

每日一谚:"Make it correct, make it clear, make it concise, make it fast. In that order." -...

有关Java进制转换_javaprintf转换进制_华夏天骄的博客-程序员宅基地

Java进制转换printf 实现进制转换通过printf及格式控制串进行转换代码示例public class Main{ public static void main(String[] args) { System.out.printf("0"+"%o",6666); System.out.printf(" 0X"+"%X",6666); }}格式控制串控制串功能“%d”表示以十进制整数形式输出“%o”_javaprintf转换进制

随便推点

java nio2 nio_5种调优Java NIO和NIO.2的方式_林道蕴的博客-程序员宅基地

Java NIO(New Input/Output)——新的输入/输出API包——是2002年引入到J2SE 1.4里的。Java NIO的目标是提高Java平台上的I/O密集型任务的性能。过了十年,很多Java开发者还是不知道怎么充分利用NIO,更少的人知道在Java SE 7里引入了更新的输入/输出 API(NIO.2)。这篇教程展示了5个在Java编程的一些常见场景里使用NIO和NIO.2包..._java nio2 nio

CSDN泄出最牛密码_josonchen的博客-程序员宅基地

季军:FLZX3000cY4yhx9day(飞流直下三千尺,疑似银河下九天); CSDN的600万用户数据被泄露后,有人发现其中几个最牛的密码: 季军:FLZX3000cY4yhx9day(飞流直下三千尺,疑似银河下九天); 亚军:hanshansi.location()!∈[gusucity](姑苏城外寒山寺); 冠军:hold?fish:palm(鱼和熊掌不可兼得)。 原来程序员都很文青

apache性能配置优化_threadsperchild=70_白天的猫头鹰的博客-程序员宅基地

最近在进行apache性能优化设置。在修改apache配置文件之前需要备份原有的配置文件夹conf,这是网站架设的好习惯。 httpd相关查看命令了解 查看当前安装模块mpm(多路处理器) httpd -l 查看httpd进程数(即各个mpm模式下Apache能够处理的并发请求数) ps -ef|grep httpd|wc -l_threadsperchild=70

Informix技巧 _informix系统休眠_狂风细雨的博客-程序员宅基地

Informix技巧 收藏 Informix技巧4则本文内容均来自网络,这里仅是组织整理,方便记忆和查阅,以及供更多的人学习,如果有兴趣可到文中的“出处”查看原文。 -------------------------------------------------------------------------------- 1. UPDATE STATISTICS FOR TABLE tablename( 出处:ChinaUnix ) Informix 数据库服务器中的优化器为SQL语句的查询_informix系统休眠

随心所欲b超工作站图像处理_正版B超随心所欲超声工作站vista版最新版模拟数字影像软件win7...-程序员宅基地

超声影像系统:超声影像工作站超声影像工作站系统可与各种型号的B超、彩超连接,实时显示、采集、处理、打印、存储超声图像,超大容量的存储空间,丰富的查询模式,多种多样的报告输出样式大大地提高了临床医生的工作效率。为医院带来好良的社会效益和经济效益。◆便捷的工作流程,高效简洁操作步骤,较同类工作站效率提高1~3倍以上;◆高分辨率同步显示实时影像,数字化捕获图像,确保图片清晰、逼真;◆图像支持脚踏开关、鼠..._超声信息vista

H3C S3100交换机基础配置_weixin_34278190的博客-程序员宅基地

端口表示方法E1/0/1(百兆电口) 25端口 G1/1/1(千兆电口)G1/1/2 (千兆光口)26端口 G1/2/1(千兆电口)G1/2/2 (千兆光口)显示系统版本信息:display version显示诊断信息:display diagnostic-information显示系统当前配置:display current-configurat...

推荐文章

热门文章

相关标签