cocos2d-js moveTo and animate action

醉酒当歌 提交于 2019-12-12 02:07:08

问题


I have a moveTo sprite action and I am trying to have the sprite animate while moving. It is a waling animation.

My trouble is I can make the sprite moveTo or animate but not both together so that when the sprite stops moving the animation goes back to the standing frame.

I am using cocos2d-js v3.0

this.sprite = new cc.Sprite.create("#player-stand-f-0");
this.sprite.setPosition(new cc.Point(300,300));
this.addChild(this.sprite);

    var animFrames = [];
    var str = "";
    for (var i = 0; i < 5; i++) {
        str = "player-walk-f-" + i;
        var spriteFrame = cc.spriteFrameCache.getSpriteFrame(str);
       var animFrame = new cc.AnimationFrame();
        animFrame.initWithSpriteFrame(spriteFrame, 1, null);
        animFrames.push(spriteFrame);
    }

var animation = cc.Animation.create(animFrames, 0.025);
var animate   = cc.animate(animation);

sprite_action = cc.MoveTo.create(2,cc.p(x,y));
this.sprite.runAction(sprite_action);
this.sprite.runAction(animate);

I have also tried the following but the walking would animate once and not continue until moveTo stops.

var seq = cc.sequence(animate, sprite_action);

回答1:


If you use "cc.sequence" action it will animate first then move. But if you want to animate the sprite sheet while moving it, there are two ways to achieve that:Look into "cc.Spawn" action. It is used for the purpose just like you need. Another convenient method is to run two actions concurrently.. Below mentioned code will give you the idea.

    // create sprite sheet
    cc.SpriteFrameCache.getInstance().addSpriteFrames(spritesheet_plist); // add Spritesheet Plist 
    var SpriteSheet = cc.SpriteBatchNode.create(spritesheet_png);  // add Spritesheet Png
    this.addChild(SpriteSheet,1);

    // Push the frames for animation
    var animFrames = [];
    for (var i = 0; i < 6; i++) {
        var str = "sequence_" + i + ".png";
        var frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(str);
        animFrames.push(frame);
    }


    // taadaa ...!!  Animate the sprites
    var animation = cc.Animation.create(animFrames, 0.06);
    var sprite = cc.Sprite.createWithSpriteFrameName("sequence_0.png");
    sprite.setAnchorPoint(0.5,0.5); // optional
    sprite.setScale(1.0,1.0); // optional
    sprite.setPosition(widhthPostion, heightPosition);
    sprite.runAction(cc.RepeatForever.create(cc.Animate.create(animation)));
    SpriteSheet.addChild(sprite,1);


    // Move the sprite
    var actionMove = cc.MoveTo.create(duration, cc.p(destinationX, destinationY));
    sprite.runAction(actionMove);


来源:https://stackoverflow.com/questions/25954410/cocos2d-js-moveto-and-animate-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!