2014-09-05 80 views
9

我使用,我想縮小在實際遊戲中的一些大的圖像使遊戲中移相器:如何在不改變位置的情況下縮放Phaser/PixiJS中的精靈大小?

create() { 
    //Create the sprite group and scale it down to 30% 
    this.pieces = this.add.group(undefined, "pieces", true); 
    this.pieces.scale.x = 0.3; 
    this.pieces.scale.y = 0.3; 

    //Add the players to the middle of the stage and add them to the 'pieces' group 
    var middle = new Phaser.Point(game.stage.width/2, game.stage.height/2); 
    var player_two = this.add.sprite(middle.x - 50, middle.y, 'image1', undefined, this.pieces); 
    var player_one = this.add.sprite(middle.x, middle.y-50, 'image2', undefined, this.pieces); 
} 

然而,因爲精靈在大小縮放,他們的首發位置也被縮放,所以出現在舞臺中間,它們只出現在距離中間距離的30%。

如何在不影響其位置的情況下縮放精靈圖像?

(代碼是偶然的打字稿,但我認爲這個特定的例子也是這樣的javascript這可能是無關緊要的)

回答

7

設置Sprite.anchor到0.5,物理體的中心位於雪碧,縮放精靈圖像,而不它影響到他們的位置。

this.image.anchor.setTo(0.5, 0.5); 
5

如果我縮放這樣的精靈,例如:

var scaleX = 2; 
    var scaleY = 2;  
    sprite.scale.set(scaleX , scaleY); 

然後我需要這個比例係數來計算PO精靈的形式:

var positionX = 100; 
var positionY = 100; 

sprite.x = positionX/scaleX; 
sprite.y = positionY/scaleY; 

像這樣,你的精靈將在位置(100,100)。 問題是,sprite.x會自動乘以scaleX。

對不起,我的英語:)

1

關於相位,我想補充一點,在你自己創建weapon.bullets或其他團體你將不得不做,而不是這樣的特殊情況:

weapon.bullets.setAll('scale.x', 0.5); 
weapon.bullets.setAll('scale.y', 0.5); 

我被困在這,最後在這個線程,這是關閉,但在我的情況下,只是不是我所需要的。其他人希望能有一些使用:)

相關問題