I'm trying to flip horizontally the sprites from a SpriteSheet animation. I'm starting from this example
I've added the flipped frames right after creating the SpriteSheet
var ss = new createjs.SpriteSheet(spriteSheet);
createjs.SpriteSheetUtils.addFlippedFrames(ss, true, false, false);
grant = new createjs.BitmapAnimation(ss);
...
And I always get this error :
Uncaught TypeError: Cannot read property 'length' of null (SpriteSheetUtils.js l.174)
I've seen that someone has got the same problem in this github thread And fixed it using the preload mecanism.
The example is also using the preload so I've tried to call the addFlippedFrames after the resource loading, but I'm still seeing the same error.
Does anyone have a clue for me ? how can I use addFlippedFrames ?
EDIT : here is the code I tried :
Init the stage with my 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);
Then use the preload like this :
var manifest = [{
src: "characters/grant/runningGrant.png",
id: "grant"
}
];
var loader = new createjs.LoadQueue(false);
loader.onFileLoad = handleFileLoad;
loader.onComplete = onResourcesLoaded;
loader.loadManifest(manifest);
and the callback functions :
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);
};