在Phaser 3中是否可以将每个精灵帧的边界框定义为地图集?当我在TexturePacker中生成一个精灵表时,我们可以在.json中找到一些参数:
"idle-0001.png":
{
"frame": {"x":1,"y":1,"w":423,"h":619},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":1,"y":1,"w": 423,"h":619,
"sourceSize": {"w":423,"h":619}
}
和我的精灵定义:
this.sprite = scene.physics.add.sprite(x,y,'player-idle','idle-0001.png');
this.sprite.setCollideWorldBounds(true);
当我更改spriteSourceSize或sourceSize时,它不会更改我的sprite上的任何内容吗?
这很奇怪,因为方法setSize很好地改变了边界框。在setSize的实现中,sourceSize是更新的,因此我认为可以直接使用地图集.json设置边界框吗?
/**
* Sizes and positions this Body's boundary, as a rectangle.
* Modifies the Body `offset` if `center` is true (the default).
* Resets the width and height to match current frame, if no width and height provided and a frame is found.
*
* @method Phaser.Physics.Arcade.Body#setSize
* @since 3.0.0
*
* @param {integer} [width] - The width of the Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame width.
* @param {integer} [height] - The height of the Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame height.
* @param {boolean} [center=true] - Modify the Body's `offset`, placing the Body's center on its Game Object's center. Only works if the Game Object has the `getCenter` method.
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setSize: function (width, height, center)
{
if (center === undefined) { center = true; }
var gameObject = this.gameObject;
if (!width && gameObject.frame)
{
width = gameObject.frame.realWidth;
}
if (!height && gameObject.frame)
{
height = gameObject.frame.realHeight;
}
this.sourceWidth = width;
this.sourceHeight = height;
this.width = this.sourceWidth * this._sx;
this.height = this.sourceHeight * this._sy;
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
this.updateCenter();
if (center && gameObject.getCenter)
{
var ox = gameObject.displayWidth / 2;
var oy = gameObject.displayHeight / 2;
this.offset.set(ox - this.halfWidth, oy - this.halfHeight);
}
this.isCircle = false;
this.radius = 0;
return this;
}