我在Phaser中使用一些我想在实际游戏中缩小的大图像进行游戏:
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',this.pieces); }
但是,由于精灵尺寸的缩小,它们的起始位置也被缩放,所以相反出现在舞台的中间,它们只显示到中间距离的30%.
如何缩小精灵图片而不影响他们的位置?
(代码是附带的在Typescript,但我认为这个特定的样本也是javascript,所以这可能是无关紧要)
解决方法
将Sprite.anchor设置为0.5,因此物理身体以Sprite为中心,缩放精灵图像,而不会影响其位置.
this.image.anchor.setTo(0.5,0.5);