phaser2 微信小遊戲入手

phaser2小遊戲基本沒什麼什麼問題,能夠下常開發遊戲.若是遇到什麼問題, 能夠提出來共同討論.javascript

下面來個例子java

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

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

var config = {
    width: 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,高度是動態的.這裏也是須要作下處理的git

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開發三款小遊戲。沒有遇到什麼大問題...github

聲音的話,sound.play方法,而後用wx的播放聲音的方法代替.canvas

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

有問題留言或 QQ羣  881784250. 謝謝~~~this

相關文章
相關標籤/搜索