如何改变 PhysicsJS 中圆圈的颜色?
How to change color of a circle in PhysicsJS?
我有一组 cells
白色圆圈。每隔 world.step
左右,我想将一个随机圆圈更改为红色。我可以像这样访问和更改圆圈的颜色:
var n = Math.floor(Math.random()*100);
cells[n].styles.fillStyle = colors.red;
这一次有效。一旦我在 Physics.util.ticker.on
中调用 world.step
,不再有圆圈变红。这是我的完整功能:
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 函数运行并更新页面。遗憾的是,旧圈子的痕迹依旧存在
我使用 canvas 渲染器而不是 pixi.js 解决了这个问题。
库在 body.view
中存储了 body 的缓存图像,因此为了刷新它,您需要删除 属性 以便渲染器 re-draws 图片。
var cell = cells[n];
cell.styles.fillStyle = colors.red;
cell.view = null;
我有一组 cells
白色圆圈。每隔 world.step
左右,我想将一个随机圆圈更改为红色。我可以像这样访问和更改圆圈的颜色:
var n = Math.floor(Math.random()*100);
cells[n].styles.fillStyle = colors.red;
这一次有效。一旦我在 Physics.util.ticker.on
中调用 world.step
,不再有圆圈变红。这是我的完整功能:
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 函数运行并更新页面。遗憾的是,旧圈子的痕迹依旧存在
我使用 canvas 渲染器而不是 pixi.js 解决了这个问题。
库在 body.view
中存储了 body 的缓存图像,因此为了刷新它,您需要删除 属性 以便渲染器 re-draws 图片。
var cell = cells[n];
cell.styles.fillStyle = colors.red;
cell.view = null;