2017-03-03 63 views
1

我下面這個教程,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); 
} 

JSFiddle

精靈不旋轉和update功能不登錄控制檯。我該如何解決?謝謝。

回答

3

代碼中有兩個錯誤。

首先,在您的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); 
}; 
+0

但是,如果不喜歡,我們只建立一個精靈。我修改示例以在對象中創建多精靈 – DeLe

+0

@BdR對於他的代碼是正確的。這裏是我分開做的一個更正的小提琴,所以它不包括'anchor'修飾:https://jsfiddle.net/31odrwLz/ –