【cocos2d-x 每天一学】(4)修改HelloWorld

来源:互联网 发布:手机铃声制作软件 编辑:程序博客网 时间:2024/05/18 01:27

目标:用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 librariesAndroid 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 librariesmake.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.setupmake.exe: *** No rule to make target `E:\cocos_workspace\MyCocosTest\proj.android\../cocos2d/cocos/2d/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 SUCCESSFULTotal time: 13 secondsMove apk to E:\cocos2d-x-3.0\projects\MyCocosTest\bin\debug\androidbuild succeeded.Runing command: deployDeploying mode: debuginstalling on devicerunning: 'D:\Android\android-sdk\platform-tools\adb uninstall com.test.cocostest'* daemon not running. starting it now on port 5037 ** daemon started successfully *Successrunning: 'D:\Android\android-sdk\platform-tools\adb install "E:\cocos2d-x-3.0\projects\MyCocosTest\bin\debug\android\MyCocosTest-debug.apk"'

 运行如下:







0 0