转载请注明出处: http://qiudeqing.com/phaser/2015/08/16/phaser.html

Phaser官方网站, 本文主要基于2.4.2版本, 官方有详细的API,例子不是很充足,但是提供了很多例子, 可以通过学习例子+查阅API进行学习

常见功能

设置Sprite原点

Sprite.anchor

var sprite = game.add.sprite(0, 0, 'sprite');
sprite.anchor.set(0.5, 0.5);

上面的代码设置元素中心坐标为0,0.anchor默认值为(0, 0)也就是说把元素的左上角放到指定坐标.

设置文本固定在界面

var text = game.add.text(8, 8, 'fixed');
text.fixedToCamera = true;

监听键盘事件

fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
fireButton.onDown.add(fire, game);

function fire() {
  console.log('fire');
}

添加渐变

Tween API

var flame = game.add.sprite(0, 0, 'flame');
flame.alpha = 1;
flame.visible = true;

game.add.tween(flame).to({
  alpha: 0
}, 100, 'Linear', true);

视口跟随sprite

Camera API

var bullet = game.add.sprite(0, 0, 'bullet');
game.camera.follow(bullet);

设置group所有sprite不受重力影响

Arcade.Body API

var targets = game.add.group(game.world. 'target', false, true, Phaser.PhysicsARCADE);
targets.create(300, 390, 'target');
targets.create(500, 390, 'target');

targets.setAll('body.allowGravity', false);

设置资源baseURL

game.load.baseURL = 'http://qiudeqing.com/';
game.load.corssOrigin = 'anonymous';

将元素从Physics系统计算中排除

Sprite API

var bullet = game.add.sprite(0, 0, 'bullet');
bullet.exists = false;

bullet.exists = false同时会影响bullet.visible

设置元素为活跃状态

bullet.reset(x, y);

This moves the Game Object to the given x/y world coordinates and sets fresh, exists, visible and renderable to true.