javascript - PIXI.JS 新手,正在寻找有关纹理缓存、加载程序问题的建议和故障排除
问题描述
我冒险进入了 PIXI 和 JS 的世界,这对我来说是一种新的编程语言,喜欢它现在的方式,但我有一个问题。
我对 TextureCache 和加载器有点困惑。如果您查看我的部分代码,我会尝试在屏幕上添加 3 个不同的图像。我一直在关注 pixi 网站的“入门部分”。我想添加他们的猫图像,我有,tileset 图像(全部)*,然后是 tileset 图像的一个图块。
问题是,我创建了 3 个新的精灵实例,当我希望它显示整个图块集时,图块集图像显示了我为图块(火箭)设置的区域。我已经在cahce 和加载器中加载了tileset。
为什么平铺显示裁剪的图像而不是整个图像?
我是否正确使用缓存来存储图像?
我是否正确使用资源方法从缓存或加载器中定位图像?
有没有一点缓存?
我的想法**当您使用矩形方法时,它会破坏原始图像并且裁剪后的版本现在是tileset1(我的图像的名称)?
<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”会给出以下答案,例如:
- https://github.com/pixijs/pixi.js/issues/4053
MySecret 在 2017 年 5 月 23 日发表评论:
文档没有关于 PIXI.utils.TextureCache 的规范
...
englercj 于 2017 年 5 月 26 日发表评论
这是故意的,它并不意味着是面向公众的 API。它是一些纹理创建方法使用的内部缓存。
- https://www.html5gamedevs.com/topic/17788-loader-and-texturecache-tutorial-anywhere/
xerver - Pixi.js 版主:
这是加载器:https ://github.com/englercj/resource-loader
该代码的文档非常详细,并且自述文件中有示例。pixi 示例也多次使用加载器:http: //pixijs.github.io/examples/
TextureCache 是一种内部机制,您几乎没有理由知道它的存在。
- https://www.html5gamedevs.com/topic/25575-pixiloaderresources-or-texturecache/
xerver - Pixi.js 版主:
不要这样做:
let e = PIXI.utils.TextureCache[player.img];
而是使用加载器为您加载的资源:
let e = PIXI.loader.resources[player.img].texture;
...
TLDR:通常不应直接使用 TextureCache :)
也可以看看:
推荐阅读
- laravel - 如何在 laravel 的 mailable 类中传递邮件正文
- kubernetes - kong-ingress-controller 的 EXTERNAL_IP 待定
- python - 在 Pillow 调整大小和转换后保留新图像的原始图像剪切路径
- flutter - 小部件库 RangeError(索引)捕获的异常:无效值:不在包含范围内 0..35:36
- php - Laravel MongoDB 在列中搜索数组值
- javascript - 当调用同步api函数时,node.js的内部函数是否会改变
- flutter - 知道如何在 Map 的 List 中调用特定值
扑 - node.js - 无法从 Flutter Web 上的 localhost API(node js express)获取 http 请求的响应
- php - 如何使用 PHP 回显 mysql SELECT COUNT(*)
- c# - System.TypeInitializationException:“'Teigha.Core.GlobalsPINVOKE' 的类型初始化程序引发了异常。”