phaser.js是一款开源的HTML5游戏框架,GitHub主页上star超过了2万。phaser.js支持使用JavaScript或TypeScript编写游戏,支持WebGL和Canvas渲染并可借助第三方工具编译成iOS,Android原生程序。phaser.js有两个版本:phaser 3和phaser CE(Community Edition)。phaser CE基于phaser 3并由社区主导,phaser 3是下一代phaser。
1、安装phaser 3
使用npm安装
使用时引入
官方针对Webpack发布了Webpack模板(点击)。 ‘
2、创建游戏对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import 'phaser';
let game;
const gameOptions = { rotationSpeed: 3, throwSpeed: 150, minAngle: 15 }
window.onload = function () { const config = { type: Phaser.CANVAS, width: 750, height: 1334, backgroundColor: 0x444444, scene: [playGame] }; game = new Phaser.Game(config); window.focus(); resize(); window.addEventListener("resize", resize, false); }
|
首先定义全局游戏设置,主要包括各种游戏参数。之后在窗口载入时声明配置信息并将配置信息传入Phaser.Game构造函数,获取焦点、调整窗口。
窗口调整方法如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function resize() { const canvas = document.querySelector("canvas"); const windowWidth = window.innerWidth; const windowHeight = window.innerHeight; const windowRatio = windowWidth / windowHeight; const gameRatio = game.config.width / game.config.height; if(windowRatio < gameRatio){ canvas.style.width = windowWidth + "px"; canvas.style.height = (windowWidth / gameRatio) + "px"; } else{ canvas.style.width = (windowHeight * gameRatio) + "px"; canvas.style.height = windowHeight + "px"; } }
|
高度始终为窗口高度,宽度根据高度等比例调整。
3、创建游戏主场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| class playGame extends Phaser.Scene { constructor() { super("PlayGame"); } preload() { this.load.image("target", "target.png"); this.load.image("knife", "knife.png"); } create(){ this.canThrow = true; this.knifeGroup = this.add.group(); this.knife = this.add.sprite(game.config.width / 2, game.config.height / 5 * 4, "knife"); this.target = this.add.sprite(game.config.width / 2, 400, "target"); this.target.depth = 1; this.input.on("pointerdown", this.throwKnife, this); } throwKnife() { if (this.canThrow) { this.canThrow = false; this.tweens.add({ targets: [this.knife], y: this.target.y + this.target.width / 2, duration: gameOptions.throwSpeed, callbackScope: this, onComplete: function (tween) { let legalHit = true; const children = this.knifeGroup.getChildren(); for (let i = 0; i < children.length; i++) { if(Math.abs(Phaser.Math.Angle.ShortestBetween( this.target.angle, children[i].impactAngle)) < gameOptions.minAngle) { legalHit = false; break; } } if (legalHit) { this.canThrow = true; const knife = this.add.sprite(this.knife.x, this.knife.y, "knife"); knife.impactAngle = this.target.angle; this.knifeGroup.add(knife); this.knife.y = game.config.height / 5 * 4; } else{ this.tweens.add({ targets: [this.knife], y: game.config.height + this.knife.height, rotation: 5, duration: gameOptions.throwSpeed * 4, callbackScope: this, onComplete: function(tween) { this.scene.start("PlayGame") } }); } } }); } } update() { this.target.angle += gameOptions.rotationSpeed; const children = this.knifeGroup.getChildren(); for (let i = 0; i < children.length; i++){ children[i].angle += gameOptions.rotationSpeed; const radians = Phaser.Math.DegToRad(children[i].angle + 90); children[i].x = this.target.x + (this.target.width / 2) * Math.cos(radians); children[i].y = this.target.y + (this.target.width / 2) * Math.sin(radians); } } }
|
场景类中除构造函数外主要有4种方法:preload、create、throwKnife和update。preload主要用于在场景开始前预加载资源,如图片、音乐等。create用于场景创建,在场景第一次被创建时调用一次,主要用于创建游戏元素和初始化一些参数。throwKnife是create方法中pointerdown事件的回调函数,当检测到点击事件时,判断是否可以投掷飞刀并调用tweens动画管理器执行动画。update每帧都会执行,主要用于添加动画。
游戏截图如下。

完整程序见我的GitHub。git clone后先执行npm install安装phaser,之后执行webpack打包,最后启动http-server访问index.html。