Phaser官方教程
準(zhǔn)備資料
bomb.png
dude.png
platform.png
sky.png
star.png
phaser.min.js
代碼是根據(jù)Phaser官方教程來(lái)的承疲。歡迎大家一起學(xué)習(xí)俗慈、討論。
第一步 生成主要框架
加載需要的資源,定義場(chǎng)景的寬高
<!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
})
}
/**
* 創(chuàng)建場(chǎng)景中的對(duì)象
**/
function create (){}
/**
* update
**/
function update(){}
</script>
</body>
</html>
第二步 繪制場(chǎng)景
在create方法中創(chuàng)建場(chǎng)景有需要的精靈等
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' });
//靜態(tài)物理組
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);
//創(chuàng)建星星
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
* 在第四步中實(shí)現(xiàn)
**/
//我們也將檢測(cè)玩家是否與星星重疊
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)
{
//設(shè)置水平速度
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)
{
//設(shè)置垂直速度
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);
//計(jì)算存活的星星
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