phaser3 简介
Phaser 是一个快速,免费且有趣的开源 HTML5 游戏框架,可在台式机和移动 Web 浏览器上提供 WebGL 和 Canvas 渲染。可以使用第三方工具将游戏编译为 iOS,Android 和本机应用程序。可以使用 JavaScript 或 TypeScript 进行开发。
游戏展示
代码展示
查看 html 页面前我先通过 npm 安装了 http-server,建议阅读完 Getting Started with Phaser 3 后再进行操作。
最新 phaser3 下载地址 Phaser 3.24.1 下载
我直接下载了 phaser.min.js 文件来进行使用。
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Making your first Phaser 3 Game - Part 1</title>
<script src="js/phaser.min.js"></script>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
<script type="text/javascript">
var config = {
type: Phaser.AUTO,
800,
height: 600,
// 添加 Arcade (游乐场)物理系统(Physics system)到游戏配置中
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
function preload() {
this.load.image('sky', 'assets/sky.png');
this.load.image('platform', 'assets/platform.png');
this.load.image('star', 'assets/star.png');
this.load.image('bomb', 'assets/bomb.png');
this.load.spritesheet('dude', 'assets/dude.png',
{ frameWidth: 32, frameHeight: 48 });
}
// 搭建场景
var platforms;
var player;
var score = 0;
var scoreText;
function create() {
this.add.image(400, 300, 'sky'); // 图片的中心坐标
// 如果你先放 star 图像,它将被 sky 图像盖住
// this.add.image(400, 300, 'star');
platforms = this.physics.add.staticGroup();
// 用函数setScale(2)把它按 x2 缩放。现在它的尺寸是800 x 64 了。要调用refreshBody(),这是因为我们缩放的是一个静态物体,所以必须把所作变动告诉物理世界(physics world)
platforms.create(400, 568, 'platform').setScale(2).refreshBody();
platforms.create(600, 400, 'platform');
platforms.create(50, 250, 'platform');
platforms.create(750, 220, 'platform');
// 人物
player = this.physics.add.sprite(100, 450, 'dude');
player.setBounce(0.2); // 跳起后着地时始终会弹起那么一点点
player.setCollideWorldBounds(true); // (通过player.setCollideWorldBounds(true))把游戏(的世界边界)设置为800 x 600后,玩家就不能不跑出这个区域了
this.anims.create({
// 'left'动画使用0, 1, 2, 3帧,跑动时每秒10帧。'repeat -1'告诉动画要循环播放
key: 'left',
frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
frameRate: 10, // 跑动时每秒10帧
repeat: -1 // 告诉动画要循环播放
});
this.anims.create({
key: 'turn',
frames: [{ key: 'dude', frame: 4 }],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
frameRate: 10,
repeat: -1
});
cursors = this.input.keyboard.createCursorKeys();
stars = this.physics.add.group({
key: 'star',
repeat: 11,
setXY: { x: 12, y: 0, stepX: 70 }
});
stars.children.iterate(function (child) {
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
this.physics.add.collider(stars, platforms);
this.physics.add.collider(player, platforms); // 它接收两个对象,检测二者之间的碰撞,并使二者分开
this.physics.add.overlap(player, stars, collectStar, null, this);
scoreText = this.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });
bombs = this.physics.add.group();
this.physics.add.collider(bombs, platforms);
this.physics.add.collider(player, bombs, hitBomb, null, this);
}
function update() {
if (cursors.left.isDown) {
player.setVelocityX(-160);
player.anims.play('left', true);
}
else if (cursors.right.isDown) {
player.setVelocityX(160);
player.anims.play('right', true);
}
else {
player.setVelocityX(0);
player.anims.play('turn');
}
if (cursors.up.isDown && player.body.touching.down) {
player.setVelocityY(-330);
}
}
function collectStar(player, star) {
star.disableBody(true, true);
score += 10;
scoreText.setText('Score: ' + score);
if (stars.countActive(true) === 0) {
stars.children.iterate(function (child) {
child.enableBody(true, child.x, 0, true, true);
});
var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
var bomb = bombs.create(x, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
}
}
function hitBomb(player, bomb) {
this.physics.pause();
player.setTint(0xff0000);
player.anims.play('turn');
gameOver = true;
}
</script>
</body>
</html>