我正试图从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 = 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);
};发布于 2013-06-16 10:28:35
您的问题是,您创建了一个带有映像url的SpriteSheet,这已经触发了一个加载过程,而预加载js队列实际上没有加载任何内容,因为图像的加载已经在进行中,所以"onComplete"-event总是在图像实际加载之前触发的,这是合理的,因为不需要加载图像两次。
我已经让你的烦躁不安,并添加了一些评论: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 (用于开发) Error18,但是对于生产,您应该只从同一台服务器加载img,这样图像操作就不会产生错误。
简单地说,:,您的问题是由于执行错误的顺序造成的,请记住:如果您使用预压:先做预压,然后再做其他事情,直到预压没有完成为止。(除了加载-图形/动画/指示器)。
发布于 2014-01-11 00:37:21
使用scaleX很容易
if(right == true)
{
//walking direction en sprite switch
rogerWalk.scaleX = -1 * rogerWalk.scaleX;
right = false;
}//我用来编写的代码(在本例中,roger向右走(标准的sprite方向)
if(right == false)
{
rogerWalk.scaleX = -1 * rogerWalk.scaleX;
right = true;//这是我拥有的键左/右键函数中的
我知道这个帖子很旧,但还是有人可以使用它:)
https://stackoverflow.com/questions/17086676
复制相似问题