Kinetic.js [How can I stop one sprite]

风格不统一 提交于 2020-01-04 07:02:14

问题


I´m starting with Kinetic.js and I have my first app working perfectly, but I don´t find the way to stop sprites... for example, I have one sprite and I want to "play" that sprite just once, see the frames and in the last one, one simple stop(). How can I do that? (Stop after the last "frame" of the sprite).

Here´s my code (for that sprite):

    var myImage = new Image();
    myImage.onload = function() {
    var mySprite = new Kinetic.Sprite({
        x: 250,
        y: 40,
        width: 70,
        height: 109,
        image: myImage,
        animation: 'idle',
        animations: animations,
        frameRate: 5
    });
    layer1.add(mySprite);
    mySprite.start();
    };
    myImage.src = 'sheet2.png';

Thanks in advance!


回答1:


You can use the afterFrame function, which will enable you to trigger an event after a specific frame.

Then you can call stop() after frame 5, for example. (you'll need to update this to however many frames there are in your sprite.)

mySprite.afterFrame(5, function(){
    mySprite.stop();
});

You can see an example of the use of afterFrame in the following two posts:

http://ramkulkarni.com/blog/sprite-animation-in-html5-canvas-with-kineticjs/ http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-sprite-tutorial/



来源:https://stackoverflow.com/questions/16303292/kinetic-js-how-can-i-stop-one-sprite

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