sprite_add_ext

此函数用于从外部文件或 URL 异步加载图像,并将其添加为新的精灵。

这是 sprite_add 的异步版本,而不是阻止代码执行从而冻结游戏,而是在调用函数后继续执行游戏代码,并在精灵完全加载后触发异步图像已加载事件。

支持的图像文件格式为 PNG、JPEG、GIF、QOIF 和 Spine JSON 文件(Spine JSON 文件要求将其关联的图集和图像文件放在它们旁边)。

注意 Spine JSON文件仅支持本地存储的文件,不支持通过 HTTP 请求的文件(与 sprite_add相同)。

async_loadDS 映射 将在异步 图像已加载 事件中包含以下字段:

精灵添加错误常量
常量描述
sprite_add_ext_error_unknownThis is a generic error code when none of the others apply (the HTML5 runner only returns this constant in case of failure).-1
sprite_add_ext_error_cancelledThis constant indicates that the request was cancelled while it was in progress.-2
sprite_add_ext_error_spritenotfoundThis constant indicates that a sprite was removed somehow partway through the loading process.-3
sprite_add_ext_error_loadfailedThis constant indicates that a file loading operation failed.-4
sprite_add_ext_error_decompressfailedThis constant indicates that image decompression failed (which could be due to e.g. a corrupted file or unsupported image format).-5
sprite_add_ext_error_setupfailedIndicates that, even though all data was loaded and decompressed, sprite resource creation itself failed.-6

 

语法:

sprite_add_ext(fname, imgnum, xorig, yorig, prefetch);

参数类型描述
fnameString要添加为精灵的图像文件的路径(本地文件路径或网址)
imgnumReal文件中的子图像数 (1 表示单个图像、GIF 或 Spine 精灵)
xorigReal新的精灵原点的 X 位置
yorigReal新的精灵原点的 Y 位置
prefetchBoolean是否立即将精灵加载到 GPU 内存中

 

返回:

Sprite Asset

 

例子:

创建事件

sprite_index = -1;
new_sprite = sprite_add_ext("my_new_sprite_index.png", 1, 0, 0, true);

异步图像加载事件

var _sprite_id = async_load[?"id"];
if (_sprite_id == new_sprite)
{
    sprite_index = _sprite_id;
}

绘制事件

if (sprite_index != -1)
{
    draw_self();
}

上面的示例在三个对象事件中定义了代码。在Create事件中,实例的sprite_index首先设置为-1,并调用函数sprite_add_ext并设置fname"my_new_sprite_index.png"(即datafiles目录根目录中的图像文件)。该函数返回的精灵 ID 存储在实例变量new_sprite中。

在异步图像加载事件中,将存储在 async_load[?"id"] 中的值与 new_sprite 中的值进行比较。如果两者相等,则意味着此图像加载事件是为前面在创建事件中调用 sprite_add_ext 而触发的(可能还有许多其他sprite_add_ext 的调用)。然后,将新加载的精灵的 ID 指定为该实例的 sprite_index

在对象调用的绘制事件实例中,如果它们的sprite_index未设置为-1(或者,更确切地说,不再设置为-1),则调用 draw_self