微信小游戏 demo 飞机大战 代码分析(一)(main.js)html
微信小游戏 demo 飞机大战 代码分析(二)(databus.js)canvas
微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js)数组
本博客将使用逐行代码分析的方式讲解该demo,本文适用于对其余高级语言熟悉,对js还未深刻了解的同窗,博主会尽量将全部遇到的不明白的部分标注清楚,如有不正确或不清楚的地方,欢迎在评论中指正浏览器
本文的代码均由微信小游戏自动生成的demo飞机大战中获取微信
用于实现敌人对象dom
代码函数
import Animation from '../base/animation' import DataBus from '../databus' const ENEMY_IMG_SRC = 'images/enemy.png' const ENEMY_WIDTH = 60 const ENEMY_HEIGHT = 60 const __ = { speed: Symbol('speed') } let databus = new DataBus() function rnd(start, end){ return Math.floor(Math.random() * (end - start) + start) } export default class Enemy extends Animation { constructor() { super(ENEMY_IMG_SRC, ENEMY_WIDTH, ENEMY_HEIGHT) this.initExplosionAnimation() } init(speed) { this.x = rnd(0, window.innerWidth - ENEMY_WIDTH) this.y = -this.height this[__.speed] = speed this.visible = true } // 预约义爆炸的帧动画 initExplosionAnimation() { let frames = [] const EXPLO_IMG_PREFIX = 'images/explosion' const EXPLO_FRAME_COUNT = 19 for ( let i = 0;i < EXPLO_FRAME_COUNT;i++ ) { frames.push(EXPLO_IMG_PREFIX + (i + 1) + '.png') } this.initFrames(frames) } // 每一帧更新子弹位置 update() { this.y += this[__.speed] // 对象回收 if ( this.y > window.innerHeight + this.height ) databus.removeEnemey(this) } }
分别是敌机的图片位置,高度和宽度动画
敌人类,继承与Animation类this
构造器code
初始化敌机速度
定义爆炸帧动画
逻辑更新函数,更新物体的参数,基本每一个具体物体都具备该函数
按速度没回合加上必定的y坐标(因为敌机是往下走的,所以加上)
若发现对象移动出屏幕,则将其回收
子弹的实现
子弹实现类,继承于精灵类(没有继承于动画类,其无需动画)
代码
import Sprite from '../base/sprite' import DataBus from '../databus' const BULLET_IMG_SRC = 'images/bullet.png' const BULLET_WIDTH = 16 const BULLET_HEIGHT = 30 const __ = { speed: Symbol('speed') } let databus = new DataBus() export default class Bullet extends Sprite { constructor() { super(BULLET_IMG_SRC, BULLET_WIDTH, BULLET_HEIGHT) } init(x, y, speed) { this.x = x this.y = y this[__.speed] = speed this.visible = true } // 每一帧更新子弹位置 update() { this.y -= this[__.speed] // 超出屏幕外回收自身 if ( this.y < -this.height ) databus.removeBullets(this) } }
构造器
初始化坐标位置和速度
逻辑更新函数
玩家类
代码
import Sprite from '../base/sprite' import Bullet from './bullet' import DataBus from '../databus' const screenWidth = window.innerWidth const screenHeight = window.innerHeight // 玩家相关常量设置 const PLAYER_IMG_SRC = 'images/hero.png' const PLAYER_WIDTH = 80 const PLAYER_HEIGHT = 80 let databus = new DataBus() export default class Player extends Sprite { constructor() { super(PLAYER_IMG_SRC, PLAYER_WIDTH, PLAYER_HEIGHT) // 玩家默认处于屏幕底部居中位置 this.x = screenWidth / 2 - this.width / 2 this.y = screenHeight - this.height - 30 // 用于在手指移动的时候标识手指是否已经在飞机上了 this.touched = false this.bullets = [] // 初始化事件监听 this.initEvent() } /** * 当手指触摸屏幕的时候 * 判断手指是否在飞机上 * @param {Number} x: 手指的X轴坐标 * @param {Number} y: 手指的Y轴坐标 * @return {Boolean}: 用于标识手指是否在飞机上的布尔值 */ checkIsFingerOnAir(x, y) { const deviation = 30 return !!( x >= this.x - deviation && y >= this.y - deviation && x <= this.x + this.width + deviation && y <= this.y + this.height + deviation ) } /** * 根据手指的位置设置飞机的位置 * 保证手指处于飞机中间 * 同时限定飞机的活动范围限制在屏幕中 */ setAirPosAcrossFingerPosZ(x, y) { let disX = x - this.width / 2 let disY = y - this.height / 2 if ( disX < 0 ) disX = 0 else if ( disX > screenWidth - this.width ) disX = screenWidth - this.width if ( disY <= 0 ) disY = 0 else if ( disY > screenHeight - this.height ) disY = screenHeight - this.height this.x = disX this.y = disY } /** * 玩家响应手指的触摸事件 * 改变战机的位置 */ initEvent() { canvas.addEventListener('touchstart', ((e) => { e.preventDefault() let x = e.touches[0].clientX let y = e.touches[0].clientY // if ( this.checkIsFingerOnAir(x, y) ) { this.touched = true this.setAirPosAcrossFingerPosZ(x, y) } }).bind(this)) canvas.addEventListener('touchmove', ((e) => { e.preventDefault() let x = e.touches[0].clientX let y = e.touches[0].clientY if ( this.touched ) this.setAirPosAcrossFingerPosZ(x, y) }).bind(this)) canvas.addEventListener('touchend', ((e) => { e.preventDefault() this.touched = false }).bind(this)) } /** * 玩家射击操做 * 射击时机由外部决定 */ shoot() { let bullet = databus.pool.getItemByClass('bullet', Bullet) bullet.init( this.x + this.width / 2 - bullet.width / 2, this.y - 10, 10 ) databus.bullets.push(bullet) } }
玩家类,继承于Spirit类
判断玩家手指是否在飞机上
根据手指的位置设置飞机的位置
保证手指处于飞机中间
同时限定飞机的活动范围限制在屏幕中
监听函数
玩家射击函数