# 大小
你可以通过设置精灵的width
和height
属性来改变它的大小。下面是如何给猫一个80像素的宽度和120像素的高度。
cat.width = 80;
cat.height = 120;
1
2
2
将这两行代码添加到setup
函数中,如下所示:
function setup() {
//Create the `cat` sprite
let cat = new Sprite(resources["images/cat.png"].texture);
//Change the sprite's position
cat.x = 96;
cat.y = 96;
//Change the sprite's size
cat.width = 80;
cat.height = 120;
//Add the cat to the stage so you can see it
app.stage.addChild(cat);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
显示结果如下:
你可以看到猫的宽度和高度发生了变化,而位置(左上角)没有变化。
# 缩放
精灵也有scale.x
和scale.y
属性,它们按比例改变精灵的宽度和高度。这里是把猫的大小缩放一半:
cat.scale.x = 0.5;
cat.scale.y = 0.5;
1
2
2
比例值是介于0和1之间的数字,表示精灵大小的百分比。1表示100%(全尺寸),0.5表示50%(半尺寸)。您可以通过将其缩放值设置为2来将精灵的大小加倍,就像这样:
cat.scale.x = 2;
cat.scale.y = 2;
1
2
2
Pixi提供了一种更简洁的方法,可以在一行代码中使用scale.set
对精灵进行缩放。如下所示:
cat.scale.set(0.5, 0.5);
1