【问题标题】:Pixi.js draw falling squaresPixi.js 绘制下降方块
【发布时间】:2016-02-13 19:04:32
【问题描述】:

我使用 PIXI.js 在画布上绘制了一个基于网格的系统。

我正在尝试为这个东西设置动画,首先每个粒子position.y-200,然后使用Tween.js 我试图让它们下落。

我将位置更改为正确的位置,即particle._y

您会注意到,跌倒后您会看到一些空白区域,并且 CPU 过热。

http://jsbin.com/wojosopibe/1/edit?html,js,output

function animateParticles() {
    for (var k = 0; k < STAGE.children.length; k++) {

        var square = STAGE.children[k];
        new Tween(square, 'position.y', square._y, Math.floor(Math.random() * 80), true);

    }
}

我觉得我做错了什么。

谁能解释一下我做错了什么以及为什么摔倒后会有一些空白?

【问题讨论】:

    标签: javascript canvas pixi.js


    【解决方案1】:

    空白的原因是您的某些动画没有开始。原因就在这一行:

    new Tween(square, 'position.y', square._y, Math.floor(Math.random() * 80), true);
    

    查看 Tween.js 的函数定义,我看到了:

    function Tween(object, property, value, frames, autostart)
    

    第四个参数是frames。我假设这是完成动画所需的帧数。 那么你的 Math.floor 函数有时会返回零,这意味着动画将没有帧并且不会开始!

    您可以改用 math.ceil() 来解决此问题。这样动画总是至少有 1 帧:

    new Tween(square, 'position.y', square._y, Math.ceil(Math.random() * 80), true);
    

    现在,至于性能,我建议设置不同的...

    动画所有这些图形对象是非常密集的。我的建议是绘制一个红色正方形,然后使用 RenderTexture 从正方形生成位图。然后你可以将 Sprites 添加到舞台上,它们在制作动画时表现得更好。

    //Cretae a single graphics object
    var g = new PIXI.Graphics();
    g.beginFill(0xFF0000).drawRect(0, 0, 2, 2).endFill();
    
    //Render the graphics into a Texture
    var renderTexture = new PIXI.RenderTexture(RENDERER, RENDERER.width, RENDERER.height);
    renderTexture.render(g);
    
    for (var i = 0; i < CONFIG.rows; i++) {
        for (var j = 0; j < CONFIG.cols; j++) {
    
            var x = j * 4;
            var y = i * 4;
    
            //Add Sprites to the stage instead of Graphics
            var PARTICLE = new PIXI.Sprite(renderTexture);
            PARTICLE.x = x;
            PARTICLE.y = -200;
    
            PARTICLE._y = H - y;
    
            STAGE.addChild(PARTICLE);
        }
    }
    

    此链接将包含更多 RenderTexture 示例: http://pixijs.github.io/examples/index.html?s=demos&f=render-texture-demo.js&title=RenderTexture

    【讨论】:

    • 它对我来说仍然很迟钝,我还能做什么,goodboydigital.com/pixijs/bunnymark 我在这里画了 60K 兔子,但仍然在 60FPS
    • 我已经从图形生成纹理并以 60FPS 运行。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2018-06-30
    • 2016-03-06
    • 2018-02-21
    • 2020-10-01
    相关资源
    最近更新 更多