Xcode代码混编

参考:http://www.himigame.com/iphone-cocos2dx/743.html

http://labs.easymobi.cn/?p=4900

http://blog.csdn.net/lzx_322/article/details/10079801


cocos2dx经常会用到ios或者安卓的东西,这时候就需要使用平台相关的语言去编写。

首先建立 .h 和.mm文件,.mm文件允许混编。


例如:自定义类 Interface.h

#define gInterfaceIntance Interface::instance()

class Interface{

public:

//宏,单例模式

    DEFINE_SINGLETON(Interface);

    

    /**

     *  打开网页

     *

     *  @param webUrl url

     */

    void openNet(std::string webUrl);



     /**

     *  弹出对话框

     *

     *  @param webUrl url

     */

    void popDialog();

private:

};

自定义类.mm


#include "Interface.h"

#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)

#include <jni.h>

#endif

IMPLEMENT_SINGLETON(Interface)


Interface::Interface(){

    

}


Interface::~Interface(){

    

}


void Interface::openNet(std::string webUrl){

//如果不是ios平台

#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)

    JniMethodInfo callPlatformFunc;//定义Jni函数信息结构体

    

    //getStaticMethodInfo 次函数返回一个bool值表示是否找到此函数

    bool isHave1 = JniHelper::getStaticMethodInfo(callPlatformFunc,"com/sg/android/platform/Platform_android","openNet","(Ljava/lang/String;)Ljava/lang/String;");

    

    if (!isHave1)

    {

        CCLog("jni:此函数不存在");

    }

    else

    {

        CCLog("jni:此函数存在 %s", functionName.c_str());

        //调用此函数

        jstring StringArg1 = callPlatformFunc.env->NewStringUTF(webUrl.c_str());

        jobject jobj = callPlatformFunc.env->CallStaticObjectMethod(callPlatformFunc.classID, callPlatformFunc.methodID,StringArg1);

        

        jstring jstr = (jstring) jobj;

        

        std::string str = JniHelper::jstring2string(jstr);

        

        CCLog("jni-java函数执行完毕");

        return  str;

    }

    

    return "{}";

#endif


//如果是Ios平台一句代码就搞定    

#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:[NSStringstringWithUTF8String:webUrl.c_str()]]];

#endif

}

//ios平台弹出对话框

void Interface::popDialog()

{

#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

UIAlertView *alert = [[UIAlertViewalloc]

                          initWithTitle:@"确认"

                          message:@"要返回主页么"

                          delegate:nil

                          cancelButtonTitle:@"不是"

                          otherButtonTitles:@"是的",

                          nil];

    

   

    //显示对话框

    [alert show];

    //清除对话框对象

    [alert release];

#endif

}


这种只需要.h和.mm文件的方法适用范围较小,如果需要判断点击对话框的那个按钮并进行相应的操作则此方法不合适。

此时可以定义一个新的OC类,在这个类中完成弹出对话,在.mm中调用这个类即可

新建oc类

BlockUIAlertView.h :

#import <Foundation/Foundation.h>


typedef void(^AlertBlock)(NSInteger);


@interface BlockUIAlertView : UIAlertView


@property(nonatomic,copy)AlertBlock block;


- (id)initWithTitle:(NSString *)title

            message:(NSString *)message

  cancelButtonTitle:(NSString *)cancelButtonTitle

        clickButton:(AlertBlock)_block

  otherButtonTitles:(NSString *)otherButtonTitles;


@end


BlockUIAlertView.m :

#import "BlockUIAlertView.h"


@implementation BlockUIAlertView


@synthesize block;


- (id)initWithTitle:(NSString *)title

            message:(NSString *)message

  cancelButtonTitle:(NSString *)cancelButtonTitle

        clickButton:(AlertBlock)_block

  otherButtonTitles:(NSString *)otherButtonTitles {

    

    self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitleotherButtonTitles:otherButtonTitles,nil];

    

    if (self) {

        self.block = _block;

    }

    

    return self;

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    self.block(buttonIndex);

}

@end


增加头文件

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

#include "BlockUIAlertView.h"

#endif


#include "cocos2d.h"

#include "../Scene/MainScene.h"

USING_NS_CC;


修改Interface.mm中的popDialog函数 

void Interface::popDialog()

{

#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

BlockUIAlertView *alertView = [[BlockUIAlertViewalloc] initWithTitle:NSLocalizedString(@"exit",@"EIXT") message:NSLocalizedString(@"exitdec",@"Are you sure to quit?") cancelButtonTitle:NSLocalizedString(@"confirm",@"Yes") clickButton:^(NSInteger indexButton){

        

        if (indexButton == 0) {

            CCDirector::sharedDirector()->replaceScene(MainScene::scene());

        }else if(indexButton ==1){

            

        }

        

    } otherButtonTitles:NSLocalizedString(@"cancel",@"No")];

    [alertView show];

    [alertView release];


#endif

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值