【phaser.js学习笔记(3)】开发H5游戏“穿越小行星”并适配微信小游戏

这篇笔记主要记录使用phaser.js开发一个完整HTML5游戏的整个过程,并将web端程序适配到微信小游戏。  

1、游戏基本架构

由于phaser社区目前仅有phaser2对微信小程序的支持,因此我选择phaser v2.6.2作为游戏的引擎。为便于开发调试,以单独的phaser.min.js方式引入文件。游戏主要分三个场景,开始场景,游戏场景和重新开始场景,index.html文件如下。 

<!DOCTYPE html >
< html >
< head >
< meta charset= "UTF-8" >
< title >Game </ title >
< style >
body {
background: #000000;
margin: 0;
padding: 0;
}
canvas {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate( -50%, -50%);
margin: 0;
}
   < / style >
</ head >
< body >
< script src= "./js/phaser.min.js" ></ script >
< script src= "./js/start.js" ></ script >
< script src= "./js/game.js" ></ script >
< script src= "./js/restart.js" ></ script >
</ body >
</ html >


2、开始场景

开始场景需要星空背景、标题、开始按键和下方火焰,开发完成的效果如下。  


start.js为入口文件,内容如下。 

let game;

// 全局游戏设置
const gameOptions = {
// 初始分数
scoreInit: 1000,
// 本局分数
score: 0,
// 屏幕宽高
width: 750,
height: 1334,
// 重力
gravity: 200,
// 墙
rectWidth: 100,
wallWidth: 5,
// 地球
earthRadius: 100,
// 飞船速度
speed: 600
}

window. onload = () => {
// 配置信息
const config = {
// 界面宽度,单位像素
// width: 750,
width: gameOptions. width,
// 界面高度
height: gameOptions. height,
// 渲染类型
renderer: Phaser. AUTO,
parent: 'render'
};
// 声明游戏对象
game = new Phaser. Game( config);
// 添加状态
game. state. add( 'start', Start);
   game. state. add( 'game', Game);
game. state. add( 'restart', Restart);
// 开始界面
game. state. start( 'start');
}

class Start extends Phaser. State {
// 构造器
constructor() {
super( "Start");
}

// 预加载
preload() {
// 图片路径
const images = {
'earth' : './assets/img/earth.png',
'sat1' : './assets/img/sat1.png',
'sat2' : './assets/img/sat2.png',
'sat3' : './assets/img/sat3.png',
       'rocket' : './assets/img/rocket.png',
       'play' : './assets/img/play.png',
       'title' : './assets/img/title.png',
       'fire' : './assets/img/fire.png',
       'over' : './assets/img/over.png',
       'restart' : './assets/img/restart.png',
       'particle1' : './assets/img/particulelune1.png',
'particle2' : './assets/img/particulelune2.png',
'station' : './assets/img/station.png'
};
// 载入图片
     for ( let name in images) {
       this. load. image( name, images[ name]);
}
// 载入天空盒
this. load. spritesheet( 'skybox', './assets/img/stars.png', 480, 640, 5);
// 音乐路径
const audios = {
       'bgMusic' : './assets/audio/music.mp3',
       'jump' : './assets/audio/jump.wav',
       'explosion' : './assets/audio/explosion.mp3'
}
// 载入音乐
     for( let name in audios){
       this. load. audio( name, audios[ name]);
    }
}

create() {
// 播放背景音乐
const bgMusic = this. add. audio( 'bgMusic', 0.3, true);
bgMusic. play();
// 屏幕比例系数
const screenWidthRatio = gameOptions. width / 480;
const screenHeightRatio = gameOptions. height / 640;

// 星星闪烁
const skybox = game. add. sprite( 0, 0, 'skybox');
skybox. width = gameOptions. width;
skybox. height = gameOptions. height;
const twinkle = skybox. animations. add( 'twinkle');
skybox. animations. play( 'twinkle', 3, true);

// 标题
const title = this. add. sprite( gameOptions. width / 2, gameOptions. height / 5, 'title');
title. width *= screenWidthRatio;
title. height *= 0.8 * screenHeightRatio;
title. anchor. set( 0.5);
this. add. tween( title). to(
{ y: gameOptions. height / 4},
1500,
Phaser. Easing. Sinusoidal. InOut,
true, 0, - 1, true);

// 开始按钮
const startButton = this. add. group();
startButton. x = <
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值