[英]How to resize a sprite pixi.js
我有一些生成精靈的 PixiJS 代碼:
let type = "WebGL";
if (!PIXI.utils.isWebGLSupported()) {
type = "canvas"
}
PIXI.utils.sayHello(type);
const app = new PIXI.Application({
width: 1000, height: 600, backgroundColor: 0x1099bb, resolution: window.devicePixelRatio || 1,
});
document.body.appendChild(app.view);
function renderSprite() {
const sprite = new PIXI.Sprite(PIXI.Texture.from("BEE-e1414622656271.png",));
sprite.anchor.set(0.5);
app.stage.addChild(sprite);
sprite.x = app.screen.width/2;
sprite.y = app.screen.height/2;
}
renderSprite();
該代碼有效。 然而,生成的精靈太大以至於超出了容器的邊界。
我需要設置精靈的大小,所以我嘗試使用baseTexture
的高度和寬度選項:
const sprite = new PIXI.Sprite(PIXI.Texture("BEE-e1414622656271.png", baseTexture=PIXI.baseTexture(options={
"height": 100,
"width": 100
})));
但我收到一條錯誤消息,指出 PIXI.baseTexture 不是 function。
如何調整精靈的大小以使其適合容器?
您可以控制 position 和創建后的精靈大小,如下所示:
var sprites = {};
sprites.cat = new PIXI.Sprite(catTexture);
//Change the sprite's position
sprites.cat.x = 96;
sprites.cat.y = 96;
//Change the sprite's size
sprites.cat.width = 30;
sprites.cat.height = 150;
//Add the cat to the stage so you can see it
stage.addChild(sprites.cat);
然后稍后——即使這個精靈被添加到舞台容器中——你也可以進一步修改它的寬度和高度。 嘗試在你的游戲循環中加入類似的東西:
sprites.cat.width += 0.5;
在文檔中查看更多信息: https://github.com/kittykatattack/learningPixi#size-and-scale
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.