赞
踩
在开发微信小游戏中,要将游戏游戏UI中的摸个按钮映射到屏幕上(计算出按钮在屏幕的rect)。
- https://developers.weixin.qq.com/minigame/dev/api/open-api/user-info/wx.createUserInfoButton.html
-
- wx.createUserInfoButton(Object object); 中 style 参数 left|top|width|heigth 如何获取。
- 将游戏UI中按钮映射到手机屏幕中,获取对应屏幕坐标。参考如下代码:
- /**
- * 将按钮映射到手机屏幕,获取到屏幕 rect
- * @param btn 需要被映射的按钮
- * @param caNode 父节点(在cocos 里面设置的 Canvas 宽高的父节点。)
- */
- public static GetBtnRankRect(btn: cc.Node, caNode: cc.Node) {
- // wx.getSystemInfo 的同步版本
- let sys = window["wx"].getSystemInfoSync();
- let rect = btn.getBoundingBoxToWorld();
- let ratio = cc.view.getDevicePixelRatio();
- let scale = cc.view.getScaleX();
- let factor = scale / ratio;
-
- let diffY = caNode.height - cc.view.getDesignResolutionSize().height;
- let left = rect.x * factor;
- let top = sys.screenHeight - (rect.y + rect.height) * factor - diffY / 2 * factor;
- let w = rect.width * factor;
- let h = rect.height * factor;
-
- // 手机屏幕 的区域 rect
- let srect = { left: left, top: top, w: w, h: h };
- return srect;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。