您好,欢迎访问一九零五行业门户网

coocs2d:使用Cocos2d

1、目录改动 每个版本都会有目录变动,这次的版本也不例外。最显的就是,在文件的根目录中增加了一个名为setup.py的配置文件。运行它可以配置系统的一些环境变量。 2、创建项目 新的版本我们来动手创建一个新项目吧。找到tools文件夹....额 打包的那个名为cr
1、目录改动每个版本都会有目录变动,这次的版本也不例外。最显眼的就是,在文件的根目录中增加了一个名为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 foundplease 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 foundplease enter its path (or press enter to skip): /users/fansy/documents/ant-1.9.3/binadded-> added: ant_root = /users/fansy/documents/ant-1.9.3/binset up successfull:ant_root was added into /users/fansy/.bash_profileplease 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 newruning command: newusage: 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 requiredlocalhost:~ 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: compilethe target platform is not specified.you can specify a target platform with -p or --platform.available platforms : win32, android, ios, mac, linuxlocalhost:hellonew2dx fansy$ cocos run -p ios
命令通过“-p”来设置平台,设置成ios的话,在一段编译的信息后,会自动打开模拟器。
......blablabla......** build succeeded **build succeeded.runing command: deploydeploying mode: debugruning command: runstarting applicationrunning: '/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: 960x640cocos2d: { 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 4runing command: compilebuilding mode: debugbuilding nativethe selected ndk toolchain version was 4.7 !......blablabla..........build successfultotal time: 4 secondsmove apk to /users/fansy/documents/code/hellonew2dx/bin/debug/androidbuild succeeded.runing command: deploydeploying mode: debuginstalling on devicerunning: '/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,管理员权限运行也无效,不知是怎么回事。可能是我注册表比较诡异?也希望知道如何解决这个问题的童鞋留言~
本篇博客出自阿修罗道http://blog.csdn.net/fansongy/article/details/21297445
其它类似信息

推荐信息