gpt4 book ai didi

easeljs - 预加载图像后使用 EaselJS 翻转 SpriteSheet 动画

转载 作者:行者123 更新时间:2023-12-02 05:01:09 27 4
gpt4 key购买 nike

我正在尝试水平翻转 SpriteSheet 动画中的 Sprite 。我从 this example 开始

我在创建 SpriteSheet

后立即添加了翻转帧
    var ss = new createjs.SpriteSheet(spriteSheet);

createjs.SpriteSheetUtils.addFlippedFrames(ss, true, false, false);

grant = new createjs.BitmapAnimation(ss);
...

而且我总是得到这个错误:

Uncaught TypeError: Cannot read property 'length' of null    (SpriteSheetUtils.js l.174)

我看到有人在这个 github thread 中遇到了同样的问题并使用预加载机制修复它。

该示例也使用了预加载,因此我尝试在资源加载后调用 addFlippedFrames,但我仍然看到相同的错误。

有人知道我的线索吗?我如何使用 addFlippedFrames ?

编辑:这是我试过的代码:

用我的 spritesheet 初始化舞台:

    spriteSheet = new createjs.SpriteSheet({
"animations": {
"none": [0, 0],
"run": [0, 25],
"jump": [26, 63]
},
"images": ["characters/grant/runningGrant.png"],
"frames": {
"height": h,
"width": w,
"regX": 0,
"regY": 0,
"count": 64
}
});



sprite = new createjs.BitmapAnimation(spriteSheet);
sprite.x = startPosition.x;
sprite.y = startPosition.y;

// Add the sprite to the stage.
stage.addChild(this.sprite);

然后像这样使用预加载:

var manifest = [{
src: "characters/grant/runningGrant.png",
id: "grant"
}
];

var loader = new createjs.LoadQueue(false);
loader.onFileLoad = handleFileLoad;
loader.onComplete = onResourcesLoaded;
loader.loadManifest(manifest);

和回调函数:

handleFileLoad = function(event) {
assets.push(event.item);
};

onResourcesLoaded = function() {

for (var i = 0; i < assets.length; i++) {
var item = assets[i];
var id = item.id;
var result = loader.getResult(id);

if (item.type == createjs.LoadQueue.IMAGE) {
var bmp = new createjs.Bitmap(result);
//does this Bitmap creation trigger the real bitmap loading ?
//is this loading asynchrous which would explain my problem.
//in this case, how can I manage the loading callback ?
}
}

//I would expect here to have the image fully loaded but its not
//A timeout of 2 seconds makes the addFlippedFrames works.
setTimeout(function(){
createjs.SpriteSheetUtils.addFlippedFrames(spriteSheet, true, false, false);
sprite.gotoAndPlay("run_h"); //I think "run_h" is used to launch the flipped version of "run"
}, 2000);
};

最佳答案

你的问题是,你创建了一个带有图像 url 的 SpriteSheet,这已经触发了一个加载过程,而 preload-js 队列实际上没有加载任何东西,因为图像的加载已经在进行中,所以“onComplete "-事件总是在图像实际加载之前触发 - 这是有道理的,因为不需要加载图像两次

我更新了您的 jsfiddle 并添加了一些评论:http://jsfiddle.net/K4TmS/1/ (完整代码)

spriteSheet = new createjs.SpriteSheet({
"animations": {
"none": [0, 0],
"run": [0, 25],
"jump": [26, 63]
},
//this line in YOUR version triggered an own loading process, independent from the queue
"images": [result],
"frames": {
"height": 292.5,
"width":165.75,
"regX": 0,
"regY": 0,
"count": 64
}
});

// this was executed instantly, because the image was already in another loading progress, and therefore wasn't even added to the queue
loader.onComplete = onResourcesLoaded;

DOM Security Error18 可以用这里的参数忽略(用于开发)Disable same origin policy in Chrome但是对于生产环境,您应该只从同一台服务器加载 img,这样图像操作就不会出错。

简而言之:您的问题是由于执行顺序错误造成的,请记住:如果您使用预加载:在其他所有事情之前进行预加载,并且在预加载未完成之前什么都不做. (加载图形/动画/指示器除外)。

关于easeljs - 预加载图像后使用 EaselJS 翻转 SpriteSheet 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17086676/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com