Phaser官方教程
game.gif准备资料
bomb.png
dude.png
platform.png
sky.png
star.png
phaser.min.js
代码是根据Phaser官方教程来的。欢迎大家一起学习、讨论。
第一步 生成主要框架
加载需要的资源,定义场景的宽高
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="../js/dist/phaser.min.js"></script>
</head>
<body>
<script>
var config = {
type: Phaser.AUTO, //Phaser.CANVAS Phaser.WEBGL
width: 800,
height: 600,
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('ground', '../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
})
}
/**
* 创建场景中的对象
**/
function create (){}
/**
* update
**/
function update(){}
</script>
</body>
</html>
第二步 绘制场景
在create方法中创建场景有需要的精灵等
var score = 0,scoreText,platforms,player,cursors,stars,bombs;
function create ()
{
//背景
this.add.image(400, 300, 'sky');
//等分
scoreText = this.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });
//静态物理组
platforms = this.physics.add.staticGroup();
platforms.create(400, 568, 'ground').setScale(2).refreshBody();
platforms.create(600, 400, 'ground');
platforms.create(50, 250, 'ground');
platforms.create(750, 220, 'ground');
player = this.physics.add.sprite(100, 450, 'dude');
player.setBounce(0.2);
player.setCollideWorldBounds(true);
//创建星星
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.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('dude', {start: 0, end: 3}),
frameRate: 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
});
player.body.setGravityY(300);
//Collider 碰撞器
this.physics.add.collider(player, platforms);
//键盘管理器
cursors = this.input.keyboard.createCursorKeys();
/**
* collectStar
* hitBomb
* 在第四步中实现
**/
//我们也将检测玩家是否与星星重叠
this.physics.add.overlap(player, stars, collectStar, null, this);
bombs = this.physics.add.group();
this.physics.add.collider(bombs, platforms);
this.physics.add.collider(player, bombs, hitBomb, null, this);
}
第三步 定义键盘事件
在update方法定义键盘事件
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(-530);
}
}
第四部,完善等分,碰撞等事件
function hitBomb(player, bomb){
this.physics.pause();
player.setTint(0xff0000);
player.anims.play('turn');
gameOver = true;
}
//收集星星
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);
}
}
```GIS
网友评论