目标:用vs2012简单修改HelloWorld代码并运行
今天我们来对新建的项目代码做些小修改,再用android运行跑通。项目: E:\cocos2d-x-3.0\projects\MyCocosTest
1、将项目导入vs2012
vs2012中打开项目, 找到E:\cocos2d-x-3.0\projects\MyCocosTest\proj.win32\MyCocosTest.sln, 发现方案中共有4个项目。
Classes中无任何文件报错, 还记得昨天我们的eclipse中各种错吗,无语。 ctrl+F5跑下试试, 第一次编得多等会儿, ok毫无压力。
2、Classes中文件解析
我们看到,classes中共四个文件。
AppDelegate.cpp
AppDelegate.h
--AppDelegate,继承cocos2d::Application,简单理解就是cocos入口,调用layer
HelloWorldScene.cpp
HelloWorldScene.h
--继承Layer, 在init()中实现布局等初始化操作,我们再界面上看到的helloworld,图片等就在这里加载。
具体就请看init()里代码的注解,很清晰了。
3、修改代码
记住,不管什么时候,只在Classes中修改代码,其他地方的勿动。
时间关系,我们就简单修改下, 修改helloworld的文字并置于图的下面。
我们在HelloWorldScene::init()中看到有这么几行代码:
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
auto label = LabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen
label->setPosition(Point(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);可见文本控件就是LableTTF, 我们修改为如下即可:
// add a label shows "Hello World"
// create and initialize a label
auto label = LabelTTF::create("Cocos2d-x 3.0, fighting!", "Arial", 24);
// position the label on the center of the screen
label->setPosition(Point(origin.x + visibleSize.width/2,
origin.y + 0 + label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);运行结果:
3、移植到android上跑
这边有2个办法:
①在项目目录下, proj.android 中 运行 build_native.py后在eclipse中 run as android application.
②在项目目录下, cocos run Test -p android
其实都是调用的build_native.py. 只是run命令会自动打包并调android设备运行。
我编译出了些问题:
Android NDK: WARNING:E:\cocos2d-x-3.0\projects\MyCocosTest\proj.android\../cocos2d/cocos/2d/Android.
mk:cocos2dx_static: LOCAL_LDLIBS is always ignored for static libraries
Android NDK: WARNING:E:\cocos2d-x-3.0\projects\MyCocosTest\proj.android\../cocos2d/cocos/2d/platform
/android/Android.mk:cocos2dxandroid_static: LOCAL_LDLIBS is always ignored for static libraries
make.exe: Entering directory `E:/cocos2d-x-3.0/projects/MyCocosTest/proj.android'
[armeabi] Gdbserver : [arm-linux-androideabi-4.8] libs/armeabi/gdbserver
[armeabi] Gdbsetup : libs/armeabi/gdb.setup
make.exe: *** No rule to make target `E:\cocos_workspace\MyCocosTest\proj.android\../cocos2d/cocos/2
d/base64.cpp', needed by `obj/local/armeabi/objs-debug/cocos2dx_static/base64.o'. Stop.
make.exe: Leaving directory `E:/cocos2d-x-3.0/projects/MyCocosTest/proj.android'
Error running command, return code: 2有点莫名其妙的, 清理下环境, 删除proj.android/obj中所有东西。重新编译,这次通过了,
BUILD SUCCESSFUL
Total time: 13 seconds
Move apk to E:\cocos2d-x-3.0\projects\MyCocosTest\bin\debug\android
build succeeded.
Runing command: deploy
Deploying mode: debug
installing on device
running: 'D:\Android\android-sdk\platform-tools\adb uninstall com.test.cocostest'
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
Success
running: 'D:\Android\android-sdk\platform-tools\adb install "E:\cocos2d-x-3.0\projects\MyCocosTest\b
in\debug\android\MyCocosTest-debug.apk"'运行如下:
这篇博客介绍了如何使用Visual Studio 2012打开并修改cocos2d-x 3.0的HelloWorld项目,包括项目导入、代码修改以及将修改后的项目移植到Android平台的步骤。在Classes中,主要关注AppDelegate和HelloWorldScene的源文件,修改了HelloWorld文字的位置。移植到Android时,提到了两种方法,并遇到及解决了编译问题。
1133

被折叠的 条评论
为什么被折叠?



