Domanda

Sto cercando di creare uno sprite animato in cocos2d-js, ma non voglio usare un foglio di spritesheet come ho fatto nel mio progetto Cocos2D-iPhone:

NSMutableArray *animationFrames = [NSMutableArray array];
    int frameCount = 0;
    for(int i = 1; i <= 9; ++i)
    {
    CCSpriteFrame *spriteFrame = [CCSpriteFrame frameWithImageNamed:[NSString stringWithFormat:@"hero-%d.png",i]];
    [animationFrames addObject:spriteFrame];
    }
NSLog(@"cria sprite com frames");
_player = [CCSprite spriteWithSpriteFrame:animationFrames.firstObject];
.

Come posso farlo in cocos2d-js?Non ho trovato le stesse funzioni nella documentazione di Cocos2D-JS.

È stato utile?

Soluzione

Forse il seguente codice è un po 'più complicato come potrebbe essere.Ma funziona e carica gli sprificati da file e mette insieme un'animazione e lo usa in una runack su uno sprite (animframe è un array vuoto, "questo" è un cclyer).

var str = "";
for (var i = 1; i < 9; i++) {
    str = "mosquito_fly" + (i < 10 ? ("0" + i) : i) + ".png";
    var texture = cc.textureCache.addImage("res/Murbiks/"+str);
    var spriteFrame = cc.SpriteFrame.create(texture,cc.rect(0,0,96,96));
    animFrames.push(spriteFrame);
}

var animation = cc.Animation.create(animFrames, 0.06+Math.random()*0.01, 10);
var animate = this.animateMostafa = cc.Animate.create(animation);

// Create sprite and set attributes
mostafa = cc.Sprite.create(res.Mostafa_single_png);        
mostafa.attr({
    x: 0,
    y: 0,
    scale: 0.60+Math.random()*0.3,
    rotation: 0
});
this.addChild(mostafa, 0);
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top