首页 > 代码库 > Cocos2d-x创建android项目(cocos2d-x系列三)

Cocos2d-x创建android项目(cocos2d-x系列三)

不例外。最显眼的就是,在文件的根目录中增加了一个名为setup.py的配置文件。运行它可以配置系统的一些环境变量。

2、创建项目

新的版本我们来动手创建一个新项目吧。找到tools文件夹....额 打包的那个名为create-project.py的文件似乎木有了!原来,在新版本中,创建新项目已经可以通过命令行来创建了,相对于先前版本的图形界面,命令行更简洁。我们要在setup.py中设置参数。以mac平台为例,我们进入到对应目录,运行脚本setup.py:

Setting up cocos2d-x...

-> Adding COCOS2D_CONSOLE_ROOT environment variable... OK
-> Added: COCOS_CONSOLE_ROOT = /Users/fansy/Documents/cocos2d-x-3.0rc0/tools/cocos2d-console/bin

-> Looking for NDK_ROOT envrironment variable... FOUND

-> Looking for ANDROID_SDK_ROOT envrironment variable... NOT FOUND
Please enter its path (or press Enter to skip): /Users/fansy/lib/android-sdk-macosx 
ADDED
  -> Added: ANDROID_SDK_ROOT = /Users/fansy/lib/android-sdk-macosx
  -> Looking for ANT_ROOT envrironment variable... NOT FOUND
Please enter its path (or press Enter to skip): /Users/fansy/Documents/ant-1.9.3/bin
ADDED
-> Added: ANT_ROOT = /Users/fansy/Documents/ant-1.9.3/bin

Set up successfull:
ANT_ROOT was added into /Users/fansy/.bash_profile

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

显然,我们在这里设置了cocos2dx命令行的路径,android_sdk的路径,ant_root的路径。这些路径都是打Android包需要使用的。接下来我们运行:

source ~/.bash_profile

这样就可以将变量配置到bash_profile中。接下来我们就可以创建项目了,这里使用了一个cocos new命令,有点像c++的分配一块内存 哈 触控的程序员兄弟挺幽默呢。

localhost:~ fansy$ cocos new
Runing command: new
usage: cocos new [-h] [-p PACKAGE_NAME] -l {cpp,lua,js} [-d DIRECTORY]
             [-t TEMPLATE_NAME] [--no-native]
             [PROJECT_NAME]
cocos new: error: argument -l/--language is required
localhost:~ fansy$ cocos new HelloNew2dx -p com.fansy.hello -l cpp -d ~/Documents/Code/
Runing command: new
> Copy template into /Users/fansy/Documents/Code/HelloNew2dx
> Copying cocos2d-x files...
> Rename project name from ‘HelloCpp‘ to ‘HelloNew2dx‘
> Replace the project name from ‘HelloCpp‘ to ‘HelloNew2dx‘
> Replace the project package name from ‘org.cocos2dx.hellocpp‘ to ‘com.fansy.hello‘

这样我们的项目就创建好了。

3、打包运行

进入项目目录,我们可以直接运行"cocos run"命令行,在各平台上运行。

localhost:~ fansy$ cd ~/Documents/Code/HelloNew2dx/
localhost:HelloNew2dx fansy$ cocos run 
Runing command: compile
The target platform is not specified.
You can specify a target platform with "-p" or "--platform".
Available platforms : win32, android, ios, mac, linux
localhost:HelloNew2dx fansy$ cocos run -p ios

命令通过“-p”来设置平台,设置成ios的话,在一段编译的信息后,会自动打开模拟器。

......blablabla......
** BUILD SUCCEEDED **

build succeeded.
Runing command: deploy
Deploying mode: debug
Runing command: run
starting application
running: ‘/Users/fansy/Documents/cocos2d-x-3.0rc0/tools/cocos2d-console/bin/../plugins/project_run/bin/ios-sim launch /Users/fansy/Documents/Code/HelloNew2dx/bin/debug/ios/HelloNew2dx.app &‘

localhost:HelloNew2dx fansy$ 2014-03-15 19:08:35.432 HelloNew2dx iOS[1522:80b] cocos2d: surface size: 960x640

cocos2d: 
{
    cocos2d.x.version: 3.0-rc0
    cocos2d.x.compiled_with_gl_state_cache: true
    cocos2d.x.build_type: DEBUG
    gl.supports_vertex_array_object: true
    cocos2d.x.compiled_with_profiler: false
    gl.renderer: Apple Software Renderer
    gl.vendor: Apple Computer, Inc.
    gl.max_texture_size: 4096
    gl.max_samples_allowed: 4
    gl.version: OpenGL ES 2.0 APPLE-9.2.1
    gl.supports_S3TC: false
    gl.supports_ATITC: false
    gl.supports_ETC1: false
    gl.max_texture_units: 8
    gl.supports_PVRTC: true
    gl.supports_NPOT: true
    gl.supports_discard_framebuffer: true
    gl.supports_BGRA8888: false
}


libpng warning: iCCP: known incorrect sRGB profile

然后就是我们熟悉的日志了。

 

接下来测试Android打包。同样运行“cocos run”。会打出debug的apk,并自动连接到设备上。

localhost:HelloNew2dx fansy$ cocos run -p android -j 4
Runing command: compile
Building mode: debug
building native
The Selected NDK toolchain version was 4.7 !

......blablabla..........
BUILD SUCCESSFUL
Total time: 4 seconds
Move apk to /Users/fansy/Documents/Code/HelloNew2dx/bin/debug/android
build succeeded.
Runing command: deploy
Deploying mode: debug
installing on device
running: ‘/Users/fansy/lib/android-sdk-macosx/platform-tools/adb uninstall com.fansy.hello‘

- waiting for device -

这时我们打开android模拟器,或连接真机,即可安装运行我们的apk了。

4、遇到的小问题

我运行Android模拟器时黑屏,查看logcat显示:

E/libEGL  (  331): called unimplemented OpenGL ES API

目测是openGL调用的问题。我又连接真机,发现可以运行,估计有可能是模拟器配置的问题?希望有知道的童鞋留言告诉我。

另外在win32平台上运行setup.py报错: 该问题是由于python的版本引起的,将python版本从3.3更改到2.7可以修复

C:\OutFile\cocos2d-x-3.0rc0>python setup.py
  File "setup.py", line 114
    print ‘Warning: Could not add "%s" into registry‘ % key
                                                    ^
SyntaxError: invalid syntax

我看了一下代码,似乎是取注册表时出问题了。

def _set_environment_variable_win32(self, key, value):

    import _winreg
    try:
        env = None
        env = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER,
                            ‘Environment‘,
                            0,
                            _winreg.KEY_SET_VALUE | _winreg.KEY_READ)
        _winreg.SetValueEx(env, key, 0, _winreg.REG_SZ, value)
        _winreg.FlushKey(env)
        _winreg.CloseKey(env)

    except Exception:
        if env:
            _winreg.CloseKey(env)
        print ‘Warning: Could not add "%s" into registry‘ % key
        return False
    return True

我的环境是win7 64位。已关360,管理员权限运行也无效,不知是怎么回事。可能是我注册表比较诡异?也希望知道如何解决这个问题的童鞋留言~