[英]How to change color of a circle in PhysicsJS?
I have a cells
array of white circles.我有一个白色圆圈的cells
数组。 Every world.step
or so, I want to change one random circle to red.每个world.step
左右,我想将一个随机圆圈更改为红色。 I can access and change the color of the circle like this:我可以像这样访问和更改圆圈的颜色:
var n = Math.floor(Math.random()*100);
cells[n].styles.fillStyle = colors.red;
This works once.这工作一次。 Once I call world.step
in the Physics.util.ticker.on
, no more circles turn red.一旦我在Physics.util.ticker.on
调用world.step
,就不会再有圆圈变成红色了。 Here is my complete function:这是我的完整功能:
Physics.util.ticker.on(function(time) {
var n = Math.floor(Math.random()*100);
cells[n].styles.fillStyle = colors.red;
world.add(cells);
world.step();
});
I tried the provided suggestion and got this code:我尝试了提供的建议并得到了这个代码:
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);
Eventually, the step function runs and updates the page.最终,step 函数会运行并更新页面。 Unfortunately, the trace of the old circle is still left behind.可惜,旧圈子的踪迹还留了下来。
I fixed this issue by using a canvas renderer rather than pixi.js.我通过使用画布渲染器而不是 pixi.js 解决了这个问题。
The library stores a cached image of the body in body.view
, so in order to get it to refresh you'll need to remove that property so the renderer re-draws the image.该库将主体的缓存图像存储在body.view
,因此为了使其刷新,您需要删除该属性,以便渲染器重新绘制图像。
var cell = cells[n];
cell.styles.fillStyle = colors.red;
cell.view = null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.