phaser2 微信小游戏入手

phaser2小游戏基本没什么什么问题,可以下常开发游戏.如果遇到什么问题, 可以提出来共同讨论.

下面来个例子

import './lib/weapp-adapter';
import Phaser from './lib/phaser';

let systemInfo = wx.getSystemInfoSync();
let {windowWidth, windowHeight, pixelRatio} = systemInfo;

var config = {
     windowWidth * pixelRatio,
    height: windowHeight * pixelRatio,
    renderer: Phaser.WEBGL,
    antialias: true,
    multiTexture: true,
    resolution:1,
    canvas:canvas
}

let game = new Phaser.Game(config);
game.state.add("boot", Boot, true);

function Boot() {

}

Boot.prototype.preload = function() {
	game.input.scale.x = pixelRatio;
	game.input.scale.y = pixelRatio;
	this.load.baseURL = "assets/";
	game.load.image("crate", "crate.png");
}

Boot.prototype.create = function() {
	// adding P2 physics to the game
	game.physics.startSystem(Phaser.Physics.P2JS);
	// setting gravity
	game.physics.p2.gravity.y = 250;
     // adding event listener for mousedown/touch event
	game.input.onDown.add(this.addRemove, this);	
}

Boot.prototype.addRemove = function(pointer){
	// checking for bodies under the mouse
	var bodyClicked = game.physics.p2.hitTest(pointer.position);
	if (bodyClicked.length==0){
		// creation of physics body and its graphic asset
		var crate = game.add.sprite(pointer.position.x, pointer.position.y, "crate");
		game.physics.p2.enable(crate);
	}
	else{
		// destruction of physics body and its graphic asset
		bodyClicked[0].parent.sprite.kill();
	}
};

				

基于一般的全屏小游戏的宽高是640,高度是动态的.这里也是需要做下处理的

import './lib/weapp-adapter';
import Phaser from './lib/phaser';

let systemInfo = wx.getSystemInfoSync();
let {windowWidth, windowHeight, pixelRatio} = systemInfo;
let width = 640;
let height = 640 * windowHeight / windowWidth;

var config = {
    width,
    height,
    renderer: Phaser.WEBGL,
    antialias: true,
    multiTexture: true,
    resolution:1,
    canvas:canvas
}


let game = new Phaser.Game(config);
game.state.add("boot", Boot, true);

function Boot() {

}

Boot.prototype.preload = function() {
	game.input.scale.x = config.width / windowWidth;
	game.input.scale.y = config.height / windowHeight;
	this.load.baseURL = "assets/";
	game.load.image("crate", "crate.png");
}

Boot.prototype.create = function() {
	// adding P2 physics to the game
	game.physics.startSystem(Phaser.Physics.P2JS);
	// setting gravity
	game.physics.p2.gravity.y = 250;
     // adding event listener for mousedown/touch event
	game.input.onDown.add(this.addRemove, this);	
}

Boot.prototype.addRemove = function(pointer){
	// checking for bodies under the mouse
	var bodyClicked = game.physics.p2.hitTest(pointer.position);
	if (bodyClicked.length==0){
		// creation of physics body and its graphic asset
		var crate = game.add.sprite(pointer.position.x, pointer.position.y, "crate");
		game.physics.p2.enable(crate);
	}
	else{
		// destruction of physics body and its graphic asset
		bodyClicked[0].parent.sprite.kill();
	}
};

				

phaser开发小游戏一点问题都没,笔者已经用phaser开发三款小游戏。没有遇到什么大问题...

声音的话,sound.play方法,然后用wx的播放声音的方法代替.

github:  https://gitee.com/redw1234567/phaser-ce

有问题留言或 QQ群  881784250. 谢谢~~~

原文地址:https://www.cnblogs.com/honghong87/p/9549714.html