移相器 - 创建播放器对象
问题描述:
我下面这个教程,https://phaser.io/examples/v2/sprites/extending-sprite-demo-2,我有以下几点:移相器 - 创建播放器对象
MonsterBunny = function (game, x, y, rotateSpeed) {
Phaser.Sprite.call(this, game, x, y);
var test = game.add.sprite(x, y, 'player');
test.rotateSpeed = rotateSpeed;
};
MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);
MonsterBunny.prototype.constructor = MonsterBunny;
MonsterBunny.prototype.update = function() {
this.angle += this.rotateSpeed;
console.log('a');
};
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.crossOrigin = 'anonymous';
game.load.image('player', 'http://examples.phaser.io/_site/images/prize-button.png');
}
function create() {
var wabbit = new MonsterBunny(game, 0, 100, 1);
var wabbit2 = new MonsterBunny(game, 150, 100, 0.5);
}
精灵不旋转和update
功能不登录控制台。我该如何解决?谢谢。
答
代码中有两个错误。
首先,在您的MonsterBunny
构造函数中,您添加了2个不是1的sprites。var test = game.add.sprite..
不应该存在,因为通过调用sprite构造函数Phaser.Sprite.call已经添加了一个sprite。
二,在Phaser.Sprite
的构造函数调用中,你忘记添加key
参数,所以使用哪个图片,在你的情况下叫做'player'
。正因为如此,它实际上添加了一个精灵,但它根本不显示。
所以,这样的事情应该工作:
MonsterBunny = function (game, x, y, rotateSpeed) {
// call sprite constructor to create sprite
Phaser.Sprite.call(this, game, x, y, 'player');
// set extra variables
this.rotateSpeed = rotateSpeed;
this.anchor.setTo(0.5, 0.5); // for centered rotation
// add sprite to game world
game.add.existing(this);
};
但是,如果不喜欢,我们只建立一个精灵。我修改示例以在对象中创建多精灵 – DeLe
@BdR对于他的代码是正确的。这里是我分开做的一个更正的小提琴,所以它不包括'anchor'修饰:https://jsfiddle.net/31odrwLz/ –