我正在尝试水平翻转 SpriteSheet 动画中的精灵。我从这个例子开始
我在创建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 线程中遇到了同样的问题, 并使用预加载机制修复了它。
该示例还使用了预加载,因此我尝试在资源加载后调用 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);
};