【问题标题】:how to port cocos2dx game to android?如何将 cocos2dx 游戏移植到安卓?
【发布时间】:2014-06-19 07:43:10
【问题描述】:

我对 cocos2dx 非常陌生。我有一个用 xcode 制作的 cocos2dx 项目。对于手势识别器,我使用 .mm 文件在 cocos2dx 中使用了 cocoa touch 的原生代码(UIGestureRecognizers)。

现在我也想用 android sdk 构建这个项目。

我在终端中使用 build_native.sh 文件构建 android 项目时遇到的错误如下所示

1

../android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/HelloWorldScene.o: in function HelloWorld::menuCloseCallback(cocos2d::CCObject*):jni/../../Classes/HelloWorldScene.cpp:88: error: undefined reference to 'XBridge::doSth()'

2

../android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/HelloWorldScene.o: in function HelloWorld::menuCloseCallback(cocos2d::CCObject*):jni/../../Classes/HelloWorldScene.cpp:85: error: undefined reference to 'XBridge::imageName'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libcocos2dcpp.so] Error 1
make: Leaving directory `../cocos2d-x/projects/MyGame/proj.android'

我在这里缺少什么?我刚开始学习 cocos2dx。 我什至可以在 android 项目中使用这样的原生 iOS 代码吗?还是我只是在拍摄空白?

我的代码如下所示。

我的 HelloWorldScene.cpp 中的代码

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    cocos2d::CCString * imageNameString = cocos2d::CCString::create("image.png");

XBridge::imageName = imageNameString->getCString();
XBridge::doSth();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    //exit(0);
#endif
#endif
}    

XBridge.h 中的代码

#ifndef xbridge_XBridgeViewController_h
#define xbridge_XBridgeViewController_h

#include "cocos2d.h"

class XBridge {

public:
    static std::string imageName;
    static void doSth();
};

#endif

XBridge.mm 中的代码

#include "XBridge.h"
#include "AppController.h"
#include "RootViewController.h"
#include "SpriteVC.h"

using namespace cocos2d;
std::string XBridge::imageName;
void XBridge::doSth()
{
    id sth = [[UIApplication sharedApplication] delegate];
    if ([sth isKindOfClass:[AppController class]])
    {

        printf("XBridge::doSth imageName %s\n",imageName.c_str());

        SpriteVC *SPVC = [[SpriteVC alloc] initWithNibName:nil bundle:nil];

        SPVC.imageNameString = [NSString stringWithFormat:@"%s",imageName.c_str()];
        [SPVC setUpImage];
        NSLog(@"XBridge::doSth imageName == %@",[NSString stringWithFormat:@"%s",imageName.c_str()]);

        //SPVC.imageView.frame = CGRectMake(480, 320, 333, 333);

        AppController *controller = (AppController *)sth;

        [controller.viewController.view addSubview:SPVC.photoImage];
    }
}

【问题讨论】:

标签: android c++ ios cocos2d-x


【解决方案1】:

即使您实际上可以使用 Android NDK Clang 工具链编译 Objective-C 和 Objective-C++ 代码,您也不能使用适用于 Android 的 iOS Cocoa Touch API。因为 Android NDK 根本不提供 iOS API,如 UIApplication、AppController 等。 Cocos2d-x 只是提供了 C++ 的 Cocos2d-x API 和一些 Android 插件。

如果您想使用适用于 Android 的 iOS API,请查看Apportable

【讨论】:

  • Apportable 不再是“免费”的东西了。总有一天我会被告知。他们正在将他们的 SDK 转移到 SpriteBuilder 中,在那里他们可以尝试从他们的移植工作中获利。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多