Home >WeChat Applet >WeChat Development >An introduction to WeChat mini games based on WeChat development tools

An introduction to WeChat mini games based on WeChat development tools

php是最好的语言
php是最好的语言Original
2018-07-26 15:04:438121browse

The article explains it in detail, allowing you to get started quickly. First install the development tools,Go to the Developer Tools Download Page, and download the corresponding installation package according to your operating system for installation. Next, you can develop WeChat mini games.

Your first mini game

Create a new project, select the mini program project, and select the hard disk path where the code is stored . Currently, the mini-game does not provide public registration. You can click to experience the mini-game using the AppID-less mode. Give your project a nice name, and finally, check "Create a game quick start template" (note: you have to select an empty directory to have this option), click OK, and you will get your first It's a little game.

Click Compile on the top menu to preview your first mini-game in the IDE.

Real device preview

Click tool You can see the performance of this mini-game in the simulator interface on the left side of the tool by pressing the compile button. Click the preview button to experience your first mini-game on your phone through WeChat scanning.

##File structure

Mini games only The following two necessary files:

  • game.js mini-game entry file

  • game .json configuration file

Configuration

Small game developers write in the root directory A game.json file is configured. Developer tools and clients need to read this configuration to complete related interface rendering and property settings.

##deviceOrientation

Example configuration

wx API
##1

2

3

4

5

##6

7

##8

9

##
{
    "deviceOrientation": "portrait",
    "networkTimeout": {
        "request": 5000,
        "connectSocket": 5000,
        "uploadFile": 5
000,
        "downloadFile": 5000
    }
}

You can only use JavaScript to write mini-games. The running environment of the mini game is a JavaScript VM bound with some methods. Unlike the browser, this running environment does not have BOM and DOM API, only wx API. Next, we will introduce how to use wx API to complete basic functions such as creating canvas, drawing graphics, displaying pictures, and responding to user interaction.

Create Canvas

Call the wx.createCanvas() interface to create a Canvas object.

##1

varcanvas = wx.createCanvas()

此时创建的 canvas 已经显示在了屏幕上,且与屏幕等宽等高。

1

console.log(canvas.width, canvas.height)

但是由于没有在 canvas 上进行绘制,所以 canvas 是透明的。使用 2d 渲染上下文的进行简单的绘制,可以在屏幕左上角看到一个 100x100 的红色矩形。

1

2

3

var context = canvas.getContext('2d')
context.fillStyle = 'red'
context.fillRect(0, 0, 100, 100)

通过 Canvas.getContext() 方法可以获取 2d 或 WebGL 渲染上下文 RenderingContext,调用渲染上下文的绘制方法可以在 Canvas 上进行绘制。小游戏基本上支持 2d 和 WebGL 1.0 所有的属性和方法,详情请见 RenderingContext。由于使用 WebGL 的绘制过程较为复杂,所以本文中的示例代码都以 2d 渲染上下文的绘制方法编写。

通过设置 width 和 height 属性可以改变 Canvas 对象的宽高,但这也会导致 Canvas 内容的清空和渲染上下文的重置。

1

2

canvas.width = 300
canvas.height = 300

显示图片

通过 wx.createImage() 接口,可以创建一个 Image 对象。Image 对象可以加载图片。当 Image 对象被绘制到 Canvas 上时,图片才会显示在屏幕上。

1

varimage = wx.createImage()

设置 Image 对象的 src 属性可以加载一张本地图片或网络图片,当图片加载完毕时会执行注册的 onload 回调函数,此时可以将 Image 对象绘制到 Canvas 上。

1

2

3

4

5

image.onload = function () {
    console.log(image.width, image.height)
    context.drawImage(image, 0, 0)
}
image.src = 'logo.png'

 创建多个 Canvas

在整个小游戏运行期间,首次调用 wx.createCanvas 接口创建的是一个上屏 Canvas。在这个 canvas 上绘制的内容都将显示在屏幕上。而第二次、第三次等后几次调用 wx.createCanvas 创建的都会是离屏 Canvas。在离屏 Canvas 上绘制的内容仅仅只是绘制到了这个离屏 Canvas 上,并不会显示在屏幕上。

以如下代码为例,运行后会发现屏幕上并没有在 (0, 0) 的位置显示 100x100 的红色矩形。因为我们是在一个离屏的 Canvas 绘制的。

1

2

3

4

5

var screenCanvas = wx.createCanvas()
var offScreenCanvas = wx.createCanvas()
var offContext = offScreenCanvas.getContext('2d')
offContext.fillStyle = 'red'
offContext.fillRect(0, 0, 100, 100)

为了让这个红色矩形显示在屏幕上,我们需要把离屏的 offScreenCanvas 绘制到上屏的 screenCanvas 上。

1

2

var screenContext = screenCanvas.getContext('2d')
screenContext.drawImage(offScreenCanvas, 0, 0)

 动画

在 JavaScript 中,一般通过 setInterval/setTimeout/requestAnimationFrame 来实现动画效果。小游戏对这些 API 提供了支持:

  • setInterval()

  • setTimeout()

  • requestAnimationFrame()

  • clearInterval()

  • clearTimeout()

  • cancelAnimationFrame()

另外,还可以通过 wx.setPreferredFramesPerSecond() 修改执行 requestAnimationFrame 回调函数的频率,以降低性能消耗。

触摸事件

响应用户与屏幕的交互是游戏中必不可少的部分,小游戏参照 DOM 中的 TouchEvent 提供了以下监听触摸事件的 API:

  • wx.onTouchStart()

  • wx.onTouchMove()

  • wx.onTouchEnd()

  • wx.onTouchCancel()

01

02

03

04

05

06

07

08

09

10

11

12

13

14

wx.onTouchStart(function (e) {
    console.log(e.touches)
}) 

wx.onTouchMove(function (e) {
    console.log(e.touches)
})

wx.onTouchEnd(function (e) {
    console.log(e.touches)
})

wx.onTouchCancel(function (e) {
    console.log(e.touches)

相关文章:

五个微信小游戏的技术要点

微信公众平台开发实现2048游戏的方法,公众2048

相关视频:

0基础微信开发入门视频教程

The above is the detailed content of An introduction to WeChat mini games based on WeChat development tools. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn