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

来源:互联网 发布:记事本软件无弹窗下载 编辑:程序博客网 时间:2024/05/18 00:48

//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)


原创粉丝点击