您的位置:首页 > 移动开发 > Cocos引擎

cocos2d_x_01_环境搭建

2014-09-29 20:59 295 查看


最终效果图:





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

最后就是创建工程.
继续命令行
cd tools/cocos2d-console/bin
,

接着使用下面命令即可:
cocos new 工程名 -p 包名 -l 语言 -d 目标文件夹
,

例如 :

cocos newcocos2d_x -pcom.beyond -lcpp
-d /Users/beyond/Desktop/project




执行后就有如下提示,表示OK~

1
2
34
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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: