参考: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
#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
}