如何将cocos2dx游戏移植到android?

时间:2014-06-19 07:43:11

标签: android c++ ios cocos2d-x

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

现在我想用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];
    }
}

1 个答案:

答案 0 :(得分:0)

即使您真的可以使用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