cocos2d-js moveTo和动画动作 [英] cocos2d-js moveTo and animate action

查看:472
本文介绍了cocos2d-js moveTo和动画动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个moveTo sprite动作,我试图在移动时使sprite动画.这是个哭声动画.

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

我的麻烦是我可以使Sprite变为moveTo或设置动画,但不能同时使它们在一起,以便当Sprite停止移动时,动画返回到站立帧.

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.

我正在使用cocos2d-js v3.0

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);

我也尝试了以下方法,但是步行只会设置一次动画,直到moveTo停止后才继续.

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);

推荐答案

如果您使用"cc.sequence"动作,它将首先进行动画处理然后移动.但是,如果要在移动精灵表时对其进行动画处理,则有两种方法可以实现该目的:查看" cc.Spawn "动作.它用于您需要的目的.另一个方便的方法是同时运行两个动作.下面提到的代码将为您提供思路.

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);

这篇关于cocos2d-js moveTo和动画动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆