我冒险进入PIXI和JS的世界,对我来说是一种新的编程语言,喜欢现在的样子,但是我遇到了问题。
我对TextureCache和加载器有些困惑。如果您查看我的代码的一部分,我会尝试在屏幕上添加3个不同的图像。我一直在关注pixi网站的“入门”部分。我想添加他们拥有的猫图像,tileset图像(全部)*,然后添加tileset图像的图块。
问题是,我创建了3个新的Sprite实例,当我希望它显示整个tileet时,tileset图像显示了tile(rocket)的区域ive集。我已经在cahce和加载器中加载了图块。
为什么图块显示裁切后的图像而不显示整个图像?我是否正确使用缓存来存储图像?我是否使用正确的资源方法从缓存或加载器中定位图像?缓存中有什么要点吗?
我的想法**当您使用矩形方法时,它会破坏原始图像,并且裁剪后的版本现在为tileet1(我的图像的名称)?
<html>
<body>
<script src="pixi.js"></script>
<script>
//Aliases
let Application = PIXI.Application,
loader = PIXI.loader,
resources = PIXI.loader.resources,
Sprite = PIXI.Sprite,
Rectangle = PIXI.Rectangle,
TextureCache = PIXI.utils.TextureCache;
let app = new PIXI.Application({
width: 1000,
height: 600,
antialias: true,
transparent: false,
resolution: 1
}
);
//Add the canvas that Pixi automatically created for you to the HTML document
document.body.appendChild(app.view);
TextureCache["tileset1.png","images/3.png"];
//load an image and run the `setup` function when it's done
loader.add(["images/3.png","tileset1.png"]).load(setup);
//This `setup` function will run when the image has loaded
function setup() {
let texture = TextureCache["tileset1.png"];
let rectangle = new Rectangle(96,64,32,32);
texture.frame = rectangle;
//Create the cat sprite, use a texture from the loader
let card = new Sprite(resources["images/3.png"].texture);
let tile = new Sprite(resources["tileset1.png"].texture);
let rocket = new Sprite(texture);
card.scale.set(0.06,0.06);
tile.x=400;
tile.y=400;
rocket.x=100;
rocket.y=100;
//Add the cat to the stage
app.stage.addChild(card);
app.stage.addChild(tile);
app.stage.addChild(rocket);
app.renderer.render(app.stage);
}
</script>
</body>
</html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/5.2.4/pixi.js"></script>
Is there any point of the cache?
-这个问题似乎对您的问题最重要:)
例如,搜索“ pixi.js”和“ TextureCache”会给出以下答案:
[MySecret在2017年5月23日发表评论:
该文档没有关于PIXI.utils.TextureCache的规范
...
englercj于2017年5月26日发表评论
这是有意的,并不意味着它是面向公众的API。它是某些纹理创建方法使用的内部缓存。
xerver-Pixi.js主持人:
这里是装载程序:https://github.com/englercj/resource-loader
该代码确实有充分的记录,并且自述文件中包含示例。 pixi示例还多次使用加载程序:http://pixijs.github.io/examples/
TextureCache是一种内部机制,几乎没有任何理由甚至应该知道它的存在。
xerver-Pixi.js主持人:
不要这样做:
let e = PIXI.utils.TextureCache[player.img];
而不是使用加载程序为您加载的资源:
let e = PIXI.loader.resources[player.img].texture;
...
TLDR:通常不应该使用TextureCache:)