| Author |
Animation Threads...
|
Anthony Yulo
Ranch Hand
Joined: Feb 22, 2003
Posts: 52
|
|
I have a main thread running the animation loop that increment timers and draw all sprites... I also have the sprite threads... in the sprite thread... and then in the main thread... [CODE] void run{ while(true) { //increment timer() //notify sprite threads of timer increment so they can udpate position //wait until all sprite threads have updated their positions //draw the sprite locations delay(timerdelay); } } So thats what i want to do..HOw do i implement this? Where do i put the synchronized keyword? what object do i put it against? DO i use Object.notifyAll() and Object.wait()?
|
it's alright, the gun isn't loaded... - hey
|
 |
Abdul Narayan D'souza TP
Greenhorn
Joined: Apr 01, 2004
Posts: 10
|
|
Anthony, You could try this. I have assumed that each sprite thread has a reference to a sprite object. i mean one to one mapping of sprite objects and sprite threads class YourTimer{ incrementTimer(){ //increment code goes here } } class Sprite{ //Keeps track of how many sprite intances are created / //threads are running static int spriteCount; updatePosition(){ // } drawSprite(Graphics g){ //drawing code goes here } } volatile boolean timerIncremented=false; volatile int drawingComplete=0; Main Thread void run{ while(true) { synchronized(yourTimer){ //increment timer() yourTimer.incrementTimer(); timerIncremented=true; //notify sprite threads of timer increment so //they can udpate position yourTimer.notifyAll(); } drawingComplete=Sprite.spriteCount; while(drawingComplete!=0){ //wait until all sprite threads have updated their //positions sleep(delay);//surround with try catch } //draw the sprite locations //call the drawSprite method of each sprite object delay(timerdelay); } } Sprite Thread void run() { while(true){ synchronized(yourTimer){ if(!timerIncremented) { yourTimer.wait();//surround with try catch } } //wake up when the timer has incremented.. // timer is in the mainthread sprite.updateposition(); //decrement drawingComplete drawingComplete--; } } I am pretty sure this will work. -
|
 |
 |
|
|
subject: Animation Threads...
|
|
|