1

我有cells一系列白色圆圈。每隔一段时间world.step,我想将一个随机圆圈更改为红色。我可以像这样访问和更改圆圈的颜色:

var n = Math.floor(Math.random()*100);
cells[n].styles.fillStyle = colors.red;

这工作一次。一旦我打电话world.stepPhysics.util.ticker.on,就没有更多的圆圈变红了。这是我的完整功能:

 Physics.util.ticker.on(function(time) {
     var n = Math.floor(Math.random()*100);
     cells[n].styles.fillStyle = colors.red;
     world.add(cells);
     world.step();
});

我尝试了提供的建议并得到了以下代码:

switch (newState) {
    case states.cancerInterphase:
        cells[n].styles.fillStyle = colors.red;
        break;
    case states.cancerMitosis:
        cells[n].styles.fillStyle = colors.pink;
        break;
    case states.interphase:
        cells[n].styles.fillStyle = colors.white;
        break;
    case states.mitosis:
        cells[n].styles.fillStyle = colors.blue;
        break;
}
cells[n].state.vel = new Physics.vector(speed[0], speed[1]);
cells[n].view = null;
world.add(cells);

最终,step 函数运行并更新页面。可惜,老圈子的痕迹还留着。

我通过使用画布渲染器而不是 pixi.js 解决了这个问题。

4

1 回答 1

1

该库将主体的缓存图像存储在 中body.view,因此为了使其刷新,您需要删除该属性,以便渲染器重新绘制图像。

var cell = cells[n];
cell.styles.fillStyle = colors.red;
cell.view = null;
于 2018-02-20T17:59:26.560 回答