pixi 小游戏_PIXI兼容微信小游戏

pixi 小游戏_PIXI兼容微信小游戏首先导入官方的 weapp adapter 然后导入 pixi min js 微信小程序使用 ES6 的 module 引用模块 具体参见 ES6 的 Module import libs weapp adapter import asPIXIfrom libs pixi min const pixelRatio windowWidth windowHeight wx get

首先导入官方的weapp-adapter,然后导入pixi.min.js,微信小程序使用ES6的module引用模块,具体参见ES6的Module。

import ‘./libs/weapp-adapter’;

import * as PIXI from ‘./libs/pixi.min’;

const { pixelRatio, windowWidth, windowHeight } = wx.getSystemInfoSync()

let game = new PIXI.Application({

width:windowWidth*pixelRatio,

height:windowHeight*pixelRatio,

view:canvas

});

let graphics = new PIXI.Graphics();

graphics.beginFill(0xfff000);

graphics.lineStyle(0, 0xffffff, 1);

graphics.drawRect(80,80,100,100);

graphics.endFill();

game.stage.addChild(graphics);

let clktext = new PIXI.Text(“Click Me!”,{fill:”#ffffff”,fontSize:32});

clktext.interactive = true;

let times = 0;

clktext.on(“pointerdown”,()=>{

clktext.text = `times${++times}`;

});

clktext.x = 200;

clktext.y = 300;

game.stage.addChild(clktext);

目录结构如下

pixi 小游戏_PIXI兼容微信小游戏

保存运行如下:

pixi 小游戏_PIXI兼容微信小游戏

完美运行,但是当点击屏幕的任何位置时,报错了,内容如下:

pixi 小游戏_PIXI兼容微信小游戏

TouchEvent未定义,为什么呢?看pixi源码:

1 InteractionManager.prototype.normalizeToPointerData = functionnormalizeToPointerData(event) {2 var normalizedEvents =[];3

4 if (this.supportsTouchEvents && event instanceofTouchEvent) {5 for (var i = 0, li = event.changedTouches.length; i < li; i++) {6 var touch =event.changedTouches[i];7

8 if (typeof touch.button === ‘undefined’) touch.button = event.touches.length ? 1 : 0;9 if (typeof touch.buttons === ‘undefined’) touch.buttons = event.touches.length ? 1 : 0;10 if (typeof touch.isPrimary === ‘undefined’) {11 touch.isPrimary = event.touches.length === 1 && event.type === ‘touchstart’;12 }13 if (typeof touch.width === ‘undefined’) touch.width = touch.radiusX || 1;14 if (typeof touch.height === ‘undefined’) touch.height = touch.radiusY || 1;15 if (typeof touch.tiltX === ‘undefined’) touch.tiltX = 0;16 if (typeof touch.tiltY === ‘undefined’) touch.tiltY = 0;17 if (typeof touch.pointerType === ‘undefined’) touch.pointerType = ‘touch’;18 if (typeof touch.pointerId === ‘undefined’) touch.pointerId = touch.identifier || 0;19 if (typeof touch.pressure === ‘undefined’) touch.pressure = touch.force || 0.5;20 touch.twist = 0;21 touch.tangentialPressure = 0;22 //TODO: Remove these, as layerX/Y is not a standard, is deprecated, has uneven

23 //support, and the fill ins are not quite the same

24 //offsetX/Y might be okay, but is not the same as clientX/Y when the canvas’s top

25 //left is not 0,0 on the page

26 if (typeof touch.layerX === ‘undefined’) touch.layerX = touch.offsetX =touch.clientX;27 if (typeof touch.layerY === ‘undefined’) touch.layerY = touch.offsetY =touch.clientY;28

29 //mark the touch as normalized, just so that we know we did it

30 touch.isNormalized = true;31

32 normalizedEvents.push(touch);33 }34 }35 //apparently PointerEvent subclasses MouseEvent, so yay

36 else if (event instanceof MouseEvent && (!this.supportsPointerEvents || !(event instanceofwindow.PointerEvent))) {37 if (typeof event.isPrimary === ‘undefined’) event.isPrimary = true;38 if (typeof event.width === ‘undefined’) event.width = 1;39 if (typeof event.height === ‘undefined’) event.height = 1;40 if (typeof event.tiltX === ‘undefined’) event.tiltX = 0;41 if (typeof event.tiltY === ‘undefined’) event.tiltY = 0;42 if (typeof event.pointerType === ‘undefined’) event.pointerType = ‘mouse’;43 if (typeof event.pointerId === ‘undefined’) event.pointerId =MOUSE_POINTER_ID;44 if (typeof event.pressure === ‘undefined’) event.pressure = 0.5;45 event.twist = 0;46 event.tangentialPressure = 0;47

48 //mark the mouse event as normalized, just so that we know we did it

49 event.isNormalized = true;50

51 normalizedEvents.push(event);52 } else{53 normalizedEvents.push(event);54 }55

56 returnnormalizedEvents;57 };

第四行判断event是否是TouchEvent类型,报错显示就是TouchEvent未定义,TouchEvent是window的事件类型,打印一下window,发现window里没有TouchEvent属性,所以报错

pixi 小游戏_PIXI兼容微信小游戏

其实只要把window.TouchEvent暴露出来即可,把weapp-adapter.js源码下载下来,查看源码发现有TouchEvent,但是没有对外导出,导出一下,然后在window.js文件再导出一下,打包一下。

pixi 小游戏_PIXI兼容微信小游戏

pixi 小游戏_PIXI兼容微信小游戏

webpack打包一下,替换原来的weapp-adapter.js文件,发现没问题了。但是点击事件出了问题,监听不到了,怎么回事呢?

问题出在事件的点击位置的转换上,pixi源码

InteractionManager.prototype.mapPositionToPoint = functionmapPositionToPoint(point, x, y) {var rect = void 0;//IE 11 fix

if (!this.interactionDOMElement.parentElement) {

rect= { x: 0, y: 0, width: 0, height: 0};

}else{

rect= this.interactionDOMElement.getBoundingClientRect();

}var resolutionMultiplier = navigator.isCocoonJS ? this.resolution : 1.0 / this.resolution;

point.x= (x – rect.left) * (this.interactionDOMElement.width / rect.width) *resolutionMultiplier;

point.y= (y – rect.top) * (this.interactionDOMElement.height / rect.height) *resolutionMultiplier;

};

向上追溯源码发现this.interactionDOMElement就是new Application()时传进来的canvas,打印发现就是一个canvas,没有parent。

这个重新映射的原理很简单。简单说就是canvas的尺寸与渲染尺寸。

以iphone5为例,全屏canvas(landscape)大小是568×320而渲染尺寸(devicePixelRatio=2)是1136×640。事件监听捕获到的位置是基于canvas(设备)的,比如有个sprite在屏幕右下角,此时pixi.js获取到的点击坐标是568, 320,而sprite在渲染尺寸的位置是1136, 640,如果不进行正确的映射就无法触发pixi.js内部实现的监听函数。

pixi 小游戏_PIXI兼容微信小游戏

pixi 小游戏_PIXI兼容微信小游戏

因为在微信小游戏里canvas肯定是全屏的,所以直接计算position即可

PIXI.interaction.InteractionManager.prototype.mapPositionToPoint = (point, x, y) =>{

point.x= x *pixelRatio

point.y= y *pixelRatio

}

或者

app.renderer.plugins.interaction.mapPositionToPoint = (point, x, y) => {

point.x = x * pixelRatio

point.y = y * pixelRatio

}

再次运行完美!

pixi 小游戏_PIXI兼容微信小游戏

还有一个PIXI.loader 和 ajax 相关的问题,

//weapp-adapter 源码//src/XMLHttpRequest.js//添加 addEventListener 方法

addEventListener(ev, cb) {this[`on${ev}`] =cb

}

基本完成了。大部分内容来自简书

https://www.jianshu.com/p/38fcbcaf2930,之所以一步步去实现,主要是因为对一些东西还不了解。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/212280.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午8:24
下一篇 2026年3月18日 下午8:24


相关推荐

  • java getmapping(_@getMapping与@postMapping详解「建议收藏」

    首先要了解一下@RequestMapping注解。@RequestMapping用于映射url到控制器类的一个特定处理程序方法。可用于方法或者类上面。也就是可以通过url找到对应的方法。@RequestMapping有8个属性。value:指定请求的实际地址。method:指定请求的method类型(GET,POST,PUT,DELETE)等。consumes:指定处理请求的提交内容类型(Cont…

    2022年4月9日
    347
  • C语言文件操作(含详细步骤)

    C语言文件操作(含详细步骤)文章目录一 为什么使用文件 二 什么是文件 1 程序文件 2 数据文件 3 文件名三 文件的打开和关闭 1 文件指针 2 文件的打开和关闭 4 文件的顺序读写一 为什么使用文件 当我们在编写一个项目的时候 自然而然想到要把之前写入的数据保存起来 而只有我们自己选择删除数据的时候 数据才不复存在 这就涉及到了数据持久化的问题 我们一般数据持久化的方法有 把数据存放在磁盘文件 存放到数据库等方式 此处我们就讲到如何将数据放入到磁盘文件当中 二 什么是文件 磁盘上的文件就是文件 例如电脑当中的 C 盘内放入的文件夹内

    2026年3月19日
    2
  • 两个队列实现一个栈思路c语言_栈和队列的主要区别

    两个队列实现一个栈思路c语言_栈和队列的主要区别用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )示例 1:输入:[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”][[],[3],[],[]]输出:[null,null,3,-1]示例 2:输入:[“CQueue”,“deleteHead”,“appendTail”,“ap

    2022年8月8日
    13
  • 控件combox

    控件combox目录 一 combox 显示二取数据三实例来自 CODE 的代码片 TestControle 一 combox 显示 nbsp nbsp 首先 combox 有两个属性来存储数据 DisplayMembe 显示成员 ValueMember 值成员 DisplayMembe 是我们在 combox 界面上看到的 ValueMember 是隐藏的数据 一般来说我

    2026年3月17日
    2
  • ADB连接Android设备的三种方法

    ADB连接Android设备的三种方法https blog csdn net weixin article details 使用 adb 命令安装安卓 apk 包 1 连接设备 adbconnect 设备 IP 2 adbinstall rapk 所在路径 ADB 连接 Android 设备的三种方法 ADB 连接 Android 设备的三种方法连接方式有三种方法 一 WiFi 连接 手机 设备 IP

    2026年3月18日
    2
  • ntp协议原理_ntp服务器连接失败

    ntp协议原理_ntp服务器连接失败ntp协议,c语言实现PAGEPAGE26ntp协议,c语言实现篇一:NTP协议格式(中文)NTP协议格式(中文)NTP协议格式1.NTP时间戳格式SNTP使用在RFC1305及其以前的版本所描述标准NTP时间戳的格式。与因特网标准标准一致,NTP数据被指定为整数或定点小数,位以big-endian风格从左边0位或者高位计数。除非不这样指定,全部数量都将设成unsigned的类型,并…

    2022年10月10日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号