微信小程序开发栏目介绍如何用pixi.js开发微信小游戏。
1.使用pixijs渲染引擎微信小游戏是一个不同于浏览器的 javascript 运行环境,没有 bom 和 dom api。然而pixi.js是用 javascript 结合其他 html5 技术来显示媒体,创建动画或管理交互式图像。是依赖浏览器提供的 bom 和 dom api 的。所以如果要在微信小游戏中使用pixi.js,需要对引擎进行改造。
不过小游戏提供了对大部分 canvas 2d 和 webgl 1.0 特性的支持,支持情况参见 renderingcontext,pixi.js它能自动侦测使用webgl还是canvas来创建图形。
无论是怎样的引擎,最终在游戏运行时所做的大部分事情都是 随着用户的交互更新画面和播放声音。小游戏的开发语言是 javascript,那么在引擎的底层就需要通过 javascript 调用绘制 api 和音频 api。
一段 javascript 代码在运行时可以调用的 api 是依赖于 宿主环境 的。我们最常用的 console.log 甚至都不是 javascript 语言核心的一部分,而是浏览器这个宿主环境提供的。常见的宿主环境有浏览器、node.js 等。浏览器有 bom 和 dom api,而 node.js 则没有;node.js 有 fs、net 等 node.js 核心模块提供的文件、网络 api,而浏览器则不具备这些模块。例如,下面这段在浏览器中可以正常运行的代码,在 node.js 中运行就会报错。
let canvas = document.createelement('canvas')复制代码
因为 node.js 这个宿主环境根本没有提供 document 这个内置的全局变量。
referenceerror: document is not defined复制代码
小游戏的运行环境是一个不同于浏览器的宿主环境,没有提供 bom 和 dom api,提供的是 wx api。通过 wx api,开发者可以调用 native 提供的绘制、音视频、网络、文件等能力。
如果你想创建画布,你需要调用 wx.createcanvas()
let canvas = wx.createcanvas()let context = canvas.getcontext('2d')复制代码
如果你想创建一个音频对象,你需要调用 wx.createinneraudiocontext()
let audio = wx.createinneraudiocontext()// src 地址仅作演示,并不真实存在audio.src = 'bgm.mp3'audio.play()复制代码
如果你想获取屏幕的宽高,你需要调用 wx.getsysteminfosync()
let { screenwidth, screenheight } = wx.getsysteminfosync()复制代码
但是基于 pixi.js 渲染引擎会通过以下方式去创建stage 并挂载到页面
document.body.appendchild(app.view);复制代码
此时会产生错误,理由如前文所述,小游戏这个宿主环境根本没有提供 document 和 window 这两个在浏览器中内置的全局变量。因为小游戏环境是一个不同于浏览器的宿主环境。
referenceerror: document is not definedreferenceerror: window is not defined复制代码
所以,基本上基于pixi.js开发的小游戏,都不能直接迁移到小游戏中使用,因为pixi.js的实现可能或多或少都用到了 bom 和 dom 这些浏览器环境特有的 api。只有对pixi.js进行改造,将对 bom 和 dom api 的调用改成 wx api 的调用,才能运行在小游戏环境中。
但是pixi.js的代码我们不能改变,也没办法直接去修改api的实现,还有一种适配方式,即在渲染引擎和游戏逻辑代码之间加一层模拟 bom 和 dom api 的适配层,我们称之为 adapter。这层适配层在全局通过 wx api 模拟了引擎会访问到的那部分 window 和 document 对象的属性和方法,使引擎感受不到环境的差异。
adapter 是用户代码,不是基础库的一部分。关于 adapter 的介绍,参见教程 adapter。
2. adapter 适配器1. weapp-adapter小游戏的运行环境在 ios 上是 javascriptcore,在 android 上是 v8,都是没有 bom 和 dom 的运行环境,没有全局的 document 和 window 对象。因此当你希望使用 dom api 来创建 canvas 和 image 等元素的时候,会引发错误。
const canvas = document.createelement('canvas')复制代码
但是我们可以使用 wx.createcanvas 和 wx.createimage 来封装一个 document。
const document = { createelement: function (tagname) { tagname = tagname.tolowercase() if (tagname === 'canvas') { return wx.createcanvas() } else if (tagname === 'image') { return wx.createimage() } }}复制代码
这时代码就可以像在浏览器中创建元素一样创建 canvas 和 image 了。
const canvas = document.createelement('canvas')const image = document.createimage('image')复制代码
同样,如果想实现 new image() 的方式创建 image 对象,只须添加如下代码。
function image () { return wx.createimage()}复制代码
这些使用 wx api 模拟 bom 和 dom 的代码组成的库称之为 adapter。顾名思义,这是对基于浏览器环境的游戏引擎在小游戏运行环境下的一层适配层,使游戏引擎在调用 dom api 和访问 dom 属性时不会产生错误。
adapter 是一个抽象的代码层,并不特指某一个适配小游戏的第三方库,每位开发者都可以根据自己的项目需要实现相应的 adapter。官方实现了一个 adapter 名为 weapp-adapter, 并提供了完整的源码,供开发者使用和参考。
**
adapter 下载地址 weapp-adapter.zip
weapp-adapter 会预先调用 wx.createcanvas() 创建一个上屏 canvas,并暴露为一个全局变量 canvas。
require('./weapp-adapter')var context = canvas.getcontext('2d')context.fillstyle = 'red'context.fillrect(0, 0, 100, 100)复制代码
除此之外 weapp-adapter 还模拟了以下对象和方法:
document.createelementcanvas.addeventlistenerlocalstorageaudioimagewebsocketxmlhttprequest等等...需要强调的是,weapp-adapter 对浏览器环境的模拟是远不完整的,仅仅只针对游戏引擎可能访问的属性和调用的方法进行了模拟,也不保证所有游戏引擎都能通过 weapp-adapter 顺利无缝接入小游戏。直接将 weapp-adapter 提供给开发者,更多地是作为参考,开发者可以根据需要在 weapp-adapter 的基础上进行扩展,以适配自己项目使用的游戏引擎。
2. pixi-adapter小游戏基础库只提供 wx.createcanvas 和 wx.createimage 等 wx api 以及 settimeout/setinterval/requestanimationframe 等常用的 js 方法。
1.全局对象window对象是浏览器环境下的全局对象。小游戏运行环境中没有bom api,因此没有window对象。但是小游戏提供了全局对象gameglobal,所有全局定义的变量都是gameglobal的属性。
console.log(gameglobal.settimeout === settimeout);console.log(gameglobal.requestanimationframe === requestanimationframe);复制代码
以上代码执行结果均为true。 开发者可以根据需要把自己封装的类和函数挂载到gameglobal上。
gameglobal.render = function(){ // 具体的方法实现}render();复制代码
2. element 元素构造import { canvas } from './canvas'/** * base element */export class element { style = { cursor: null } appendchild() {} removechild() {} addeventlistener() {} removeeventlistener() {}}export const htmlcanvaselement = canvas.constructorexport const htmlimageelement = wx.createimage().constructorexport class htmlvideoelement extends element {}复制代码
3. document 构造import { canvas } from './canvas'import image from './image'import { element } from './element'const stack = {}/** * document 适配 */export default { body: new element('body'), addeventlistener(type, handle) { stack[type] = stack[type] || [] stack[type].push(handle) }, removeeventlistener(type, handle) { if (stack[type] && stack[type].length) { const i = stack[type].indexof(handle) i !== -1 && stack[type].splice(i) } }, dispatch(ev) { const queue = stack[ev.type] queue && queue.foreach(handle => handle(ev)) }, createelement(tag) { switch (tag) { case 'canvas': { return new canvas() } case 'img': { return new image() } default: { return new element() } } }}复制代码
4.统一入口import { noop } from './util'import image from './image'import { canvas } from './canvas'import location from './location'import document from './document'import websocket from './websocket'import navigator from './navigator'import touchevent from './touchevent'import xmldocument from './xmldocument'import localstorage from './localstorage'import xmlhttprequest from './xmlhttprequest'import { element, htmlcanvaselement, htmlimageelement, htmlvideoelement } from './element'const { platform } = wx.getsysteminfosync()gameglobal.canvas = canvas // 全局canvascanvas.addeventlistener = document.addeventlistenercanvas.removeeventlistener = document.removeeventlistener// 模拟器 挂载window上不能修改if (platform === 'devtools') { object.defineproperties(window, { image: {value: image}, element: {value: element}, ontouchstart: {value: noop}, websocket: {value: websocket}, addeventlistener: {value: noop}, touchevent: {value: touchevent}, xmldocument: {value: xmldocument}, localstorage: {value: localstorage}, xmlhttprequest: {value: xmlhttprequest}, htmlvideoelement: {value: htmlvideoelement}, htmlimageelement: {value: htmlimageelement}, htmlcanvaselement: {value: htmlcanvaselement}, }) // 挂载 document for (const key in document) { const desc = object.getownpropertydescriptor(window.document, key) if (!desc || desc.configurable) { object.defineproperty(window.document, key, {value: document[key]}) } }} else { gameglobal.image = image gameglobal.window = gameglobal gameglobal.ontouchstart = noop gameglobal.document = document gameglobal.location = location gameglobal.websocket = websocket gameglobal.navigator = navigator gameglobal.touchevent = touchevent gameglobal.addeventlistener = noop gameglobal.xmldocument = xmldocument gameglobal.removeeventlistener = noop gameglobal.localstorage = localstorage gameglobal.xmlhttprequest = xmlhttprequest gameglobal.htmlimageelement = htmlimageelement gameglobal.htmlvideoelement = htmlvideoelement gameglobal.htmlcanvaselement = htmlcanvaselement gameglobal.webglrenderingcontext = gameglobal.webglrenderingcontext || {}}复制代码
思路建议为先引入通用的 adapter 尝试运行,然后遇到的问题再逐个解决掉。
相关免费学习推荐:微信小程序开发
以上就是学习如何用pixi.js开发微信小游戏的详细内容。