【问题标题】:Change texture of Pixi's AnimatedSprite更改 Pixi 的 AnimatedSprite 的纹理
【发布时间】:2017-05-28 14:16:15
【问题描述】:

我使用此代码设置纹理图集动画:

    PIXI.loader
        .add('out2', 'assets/out2.png')
        .load(function (loader, resources){
        onRotationsLoaded(loader, resources)
    });

    function onRotationsLoaded(loader, resources) {
      first = new PIXI.extras.AnimatedSprite(setupFrames(resources["out2"].texture.baseTexture));

      app.renderer.plugins.prepare.upload(first, function(){
         console.log("loaded first");
      // ready to go

    });
    }


function setupFrames(name) {
    var frames = [];
    array is an array that stores correct position for each frame of animation
    for (var i = 0; i < array.length; i++) {
        var rect = new PIXI.Rectangle(array[i].frame.x, array[i].frame.y, array[i].frame.w, array[i].frame.h);
        frames.push(new PIXI.Texture(name, rect));
    }
    return frames;
}

我想在点击事件或其他东西中更改 AnimatedSprite first 的纹理。需要从服务器获取新纹理(我不想在开始时加载它,因为它们太多了)。我可以销毁first 并创建second AnimatedSprite,但是有没有办法改变它的纹理图集图像?

【问题讨论】:

    标签: javascript pixi.js


    【解决方案1】:

    我想说简单地替换 AnimatedSprite._textures 就可以了。

    first.textures = setupFrames('secondOne');
    

    如果新纹理的帧数与前一个不同,您可能希望在替换纹理后立即调用AnimatedSprite.prototype.gotoAndPlay(frame) 以重置当前帧。

    【讨论】:

    • 谢谢。这就是我实施的。不幸的是,如果您只是切换纹理,则准备插件不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    相关资源
    最近更新 更多