cocoslua与android交互,cocos2dx 获取手机屏幕分辨率 objective-c android 和 Lua 交互 向lua 传参...

//cocos2dx 获取IOS 分辨率 然后传给Lua

IOS代码:

+(NSString*) getHostResolving {

CGRect rect = [[UIScreen mainScreen] bounds];

CGSize size = rect.size;

CGFloat width = size.width;

CGFloat height = size.height; NSLog(@"print=============555 %f,%f",width,height);  //分辨率

CGFloat scale_screen = [UIScreen mainScreen].scale;

NSString *_width = [NSString stringWithFormat:@"%f",width*scale_screen];

NSString *_heigh = [NSString stringWithFormat:@"%f",height*scale_screen];

NSError *error = nil;

NSMutableDictionary *info = [NSMutableDictionary dictionaryWithCapacity:10];

[info setValue:_width forKey:@"width"];

[info setValue:_heigh forKey:@"height"];

NSLog(@"print===============88889999=2== %@",info);

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info

options:NSJSONWritingPrettyPrinted

error:&error];

NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSLog(@"print===============88889999=3== %@",jsonData);

NSLog(@"print===============88889999=4== %@",jsonString);

NSLog(@"print===============22===",info,jsonString);

return jsonString;

}

+ (NSString*) getHostResolving

{

return [Utils getHostResolving];

}

Lua:

--获取屏幕分辨率

function Bridge_ios.getClientResolving()

local sigs = "()Ljava/lang/String;"

local ok,ret = luaoc.callStaticMethod(BRIDGE_CLASS,"getHostResolving")

if not ok then

print("luajssssssssssssssssssss error:" .. ret)

return ""

else

print("gggggggggggggggggggggggggggggggggis:" .. ret)

return ret

end

end--

、、android代码如下:

// 分辨率全局变量

static int w = -1;

// 分辨率

static int h = -1;

在AppActivity的onCreate里加

Display display = getWindowManager().getDefaultDisplay();

Point size = new Point();

display.getSize(size);

int width = size.x;

int height = size.y;

w = width;

h = height;

public static String getHostResolving() throws JSONException

{

//2.Set the format of windo

int[] data = new int[2];

data[0] = w;

data[1] = h;

Log.i(TAG, "-> " + w); //统一TAG

Log.i(TAG, "-> " + h); //统一TAG

//JsonObject object = new JsonObject();

JSONObject jsonObject = new JSONObject();

jsonObject.put("width",w );

jsonObject.put("height",h);

return jsonObject.toString();

}

lua:

--获取屏幕分辨率

function Bridge_android.getClientResolving()

local sigs = "()Ljava/lang/String;"

local ok,ret = luaj.callStaticMethod(BRIDGE_CLASS,"getHostResolving",{},sigs)

if not ok then

print("luajssssssssssssssssssss error:" .. ret)

return ""

else

print("gggggggggggggggggggggggggggggggggis:" .. ret)

return ret

end

end-

获取:

function MultiPlatform:getHostResolving()

local plat = self:getSupportPlatform()

if nil ~= g_var(PLATFORM[plat]) and nil ~= g_var(PLATFORM[plat]).getClientResolving then

return g_var(PLATFORM[plat]).getClientResolving()

else

print("unknow platform ==> " .. plat)

return ""

end

end

local fenbian = MultiPlatform:getInstance():getHostResolving()

local jsonStr = cjson.decode(fenbian)

print("88888888888888888888888555",jsonStr.width,jsonStr.height)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值