WeChat game development-study notes

clipboard.png

Official tutorial address

-Basic skills for playing WeChat mini games

JS ES6 writing

-A pure jscore program for mini games, no DOM operation; H5 games are all playing with canvas; so the official has a weapon-adapter.js, which simulates canvas

import './js/libs/weapp-adapter';

After the introduction, there will be a canvas object globally; Adapter download address

-How to write singleton

let instance;
export default class Instant {
  constructor() {
    if (instance)
      return instance;
    instance = this;
    //不管怎么new xxx 不会是新的对象
    this.xxx="xxxx";
  }
}

transfer

import Instant from './js/tt/instant';
let instant = new Instant();

Or in the class

import Instant from './js/tt/instant';
export default class Main{
  constructor() {
    this.instant = new Instant();
  }
}

-Timeline window.requestAnimationFrame
usage

let render= e => {
  //刷新逻辑
  window.requestAnimationFrame(render, canvas);
}
window.requestAnimationFrame(render, canvas);

-Pictures

let img = new Image();
img.src = 'images/enemy.png';

The x and y attributes of the picture are read-only;
it is so fun to refresh the position

let img = new Image();
let ctx = canvas.getContext('2d');
img.src = 'images/enemy.png';
var render = e => {
  //清空canvas内容
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  //重新绘制img对象到canvas xx yy 是 鼠标点击的坐标位置
  ctx.drawImage(img, xx, yy, img.width, img.height);
  window.requestAnimationFrame(render, canvas);
}
window.requestAnimationFrame(render, canvas);
let [xx, yy] = [0, 0];
canvas.addEventListener('touchstart', ((e) => {
  e.preventDefault();
  let x = e.touches[0].clientX;
  let y = e.touches[0].clientY;
  [xx, yy] = [x,y]
}));

-Button interaction events (disgusting)
can only be judged and compared with the width and height of the object's location by the xy attribute of touch! ! !
Official tutorial code:

//游戏结束后的触摸事件处理逻辑
  touchEventHandler(e) {
    e.preventDefault()
    let x = e.touches[0].clientX
    let y = e.touches[0].clientY
    let area = this.gameinfo.btnArea
    if (x >= area.startX
      && x <= area.endX
      && y >= area.startY
      && y <= area.endY)
      this.restart()
  }

I have used egret to locate the ui element of the picture, which is also a variety of xy, very tired! !

Guess you like

Origin www.cnblogs.com/homehtml/p/12722111.html