Flickering gap caused by Thread/Draw sync?
in
Programming Questions
•
2 years ago
Hi all,
The code below is an example of some code I'm using for a game. The problem is the 1px(?) line appearing intermittently after the last
Thing in the array. I suspect that this is because
draw() is rendering what's happening irrelevant of whether or not the
ThingMover (on a separate Thread) has finished updating or not, which was really the point of using the thread in the first place.
Does anyone have any ideas on how I might get rid of the flickering gap? I've had a go at using a boolean to make sure only either draw or the
ThingMover's
run() method is being accessed at any one time with little success, so any help would be much appreciated.
Thanks very much
Dave
Processing user on Google Plus?
http://gplus.to/calx
int numThings = 15;
int thingSize = 64;
String here = "<< here";
ThingMover thingMover;
void setup()
{
size(800, 480);
thingMover = new ThingMover();
thingMover.start();
noStroke();
noSmooth();
frameRate(6000);
}
void draw()
{
background(0, 255, 0);
thingMover.displayThings();
}
class Thing
{
float x, y, w, h, speed;
int id;
Thing(float x, float y, float w, float h, float speed, int id)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = speed;
this.id = id;
}
void move()
{
x+=speed;
if (x <= -thingSize)
{
x = (numThings-1)*thingSize;
}
}
void display()
{
fill(255);
rect(x, y, w, h);
if (id == 0)
{
fill(0);
text(here, x, y+100);
}
}
}
class ThingMover extends Thread
{
Thing[] things = new Thing[numThings];
boolean running;
ThingMover()
{
for (int i = 0; i < things.length; i++)
{
things[i] = new Thing(i*thingSize, (height/2)+(i*-2), 64, 400, -1, i);
}
running = true;
}
void run()
{
while (running)
{
try
{
sleep(5);
}
catch (Exception e)
{
//one day I really will fill this out.
}
updateThings();
}
}
void updateThings()
{
for (int i = 0; i < things.length; i++)
{
things[i].move();
}
}
void displayThings()
{
for (int i = 0; i < things.length; i++)
{
things[i].display();
}
}
}
1