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
##1 2 3 4 5 ##6 7 ##8 9 ## { "deviceOrientation": "portrait", "networkTimeout": { "request": 5000, "connectSocket": 5000, "uploadFile": 5 000, "downloadFile": 5000 } } Copy after login |
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() Copy after login 此时创建的 canvas 已经显示在了屏幕上,且与屏幕等宽等高。
但是由于没有在 canvas 上进行绘制,所以 canvas 是透明的。使用 2d 渲染上下文的进行简单的绘制,可以在屏幕左上角看到一个 100x100 的红色矩形。
通过 Canvas.getContext() 方法可以获取 2d 或 WebGL 渲染上下文 RenderingContext,调用渲染上下文的绘制方法可以在 Canvas 上进行绘制。小游戏基本上支持 2d 和 WebGL 1.0 所有的属性和方法,详情请见 RenderingContext。由于使用 WebGL 的绘制过程较为复杂,所以本文中的示例代码都以 2d 渲染上下文的绘制方法编写。 通过设置 width 和 height 属性可以改变 Canvas 对象的宽高,但这也会导致 Canvas 内容的清空和渲染上下文的重置。
显示图片 通过 wx.createImage() 接口,可以创建一个 Image 对象。Image 对象可以加载图片。当 Image 对象被绘制到 Canvas 上时,图片才会显示在屏幕上。
设置 Image 对象的 src 属性可以加载一张本地图片或网络图片,当图片加载完毕时会执行注册的 onload 回调函数,此时可以将 Image 对象绘制到 Canvas 上。
创建多个 Canvas 在整个小游戏运行期间,首次调用 wx.createCanvas 接口创建的是一个上屏 Canvas。在这个 canvas 上绘制的内容都将显示在屏幕上。而第二次、第三次等后几次调用 wx.createCanvas 创建的都会是离屏 Canvas。在离屏 Canvas 上绘制的内容仅仅只是绘制到了这个离屏 Canvas 上,并不会显示在屏幕上。 以如下代码为例,运行后会发现屏幕上并没有在 (0, 0) 的位置显示 100x100 的红色矩形。因为我们是在一个离屏的 Canvas 绘制的。
为了让这个红色矩形显示在屏幕上,我们需要把离屏的 offScreenCanvas 绘制到上屏的 screenCanvas 上。
动画 在 JavaScript 中,一般通过 setInterval/setTimeout/requestAnimationFrame 来实现动画效果。小游戏对这些 API 提供了支持:
另外,还可以通过 wx.setPreferredFramesPerSecond() 修改执行 requestAnimationFrame 回调函数的频率,以降低性能消耗。 触摸事件 响应用户与屏幕的交互是游戏中必不可少的部分,小游戏参照 DOM 中的 TouchEvent 提供了以下监听触摸事件的 API:
相关文章: 相关视频: 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!
Related labels:
source:php.cn
Previous article:About the implementation code of WeChat’s custom sharing function
Next article:WeChat public account development to implement a countdown function (pure code)
Statement of this Website
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|