Stuck in Infinite Loop while PostInvalidating
- by Nicholas Roge
I'm trying to test something, however, the loop I'm using keeps getting stuck while running. It's just a basic lock thread while doing something else before continuing kind of loop. I've double checked that I'm locking AND unlocking the variable I'm using, but regardless it's still stuck in the loop.
Here are the segments of code I have that cause the problem:
ActualGame.java:
Thread thread=new Thread("Dialogue Thread"){
@Override public void run(){
Timer fireTimer=new Timer();
int arrowSequence=0;
gameHandler.setOnTouchListener(
new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent me) {
//Do something.
if(!gameHandler.fireTimer.getActive()){
exitLoop=true;
}
return false;
}
}
);
while(!exitLoop){
while(fireTimer.getActive()||!gameHandler.drawn);
c.drawBitmap(SpriteSheet.createSingleBitmap(getResources(), R.drawable.dialogue_box,240,48),-48,0,null);
c.drawBitmap(SpriteSheet.createSingleBitmap(getResources(),R.drawable.dialogue_continuearrow,32,16,8,16,arrowSequence,0),-16,8,null);
gameHandler.drawn=false;
gameHandler.postInvalidate();
if(arrowSequence+1==4){
arrowSequence=0;
exitLoop=true;
}else{
arrowSequence++;
}
fireTimer.startWait(100);
}
gameHandler.setOnTouchListener(gameHandler.defaultOnTouchListener);
}
};
thread.run();
And the onDraw method of GameHandler:
canvas.scale(scale,scale);
canvas.translate(((screenWidth/2)-((terrainWidth*scale)/2))/scale,((screenHeight/2)-((terrainHeight*scale)/2))/scale);
canvas.drawColor(Color.BLACK);
for(int layer=0;layer(less than)tiles.length;layer++){
if(layer==playerLayer){
canvas.drawBitmap(playerSprite.getCurrentSprite(),
playerSprite.getPixelLocationX(),
playerSprite.getPixelLocationY(),
null);
continue;
}
for(int y=0;y(less than)tiles[layer].length;y++){
for(int x=0;x(less than)tiles[layer][y].length;x++){
if(layer==0&&tiles[layer][y][x]==null){
tiles[layer][y][x]=nullTile;
}
if(tiles[layer][y][x]!=null){
runningFromTileEvent=false;
canvas.drawBitmap(tiles[layer][y][x].associatedSprite.getCurrentSprite(),x*tiles[layer][y][x].associatedSprite.spriteWidth,y*tiles[layer][y][x].associatedSprite.spriteHeight,null);
}
}
}
}
for(int i=0;i(less than)canvasEvents.size();i++){
if(canvasEvents.elementAt(i).condition(this)){
canvasEvents.elementAt(i).run(canvas,this);
}
}
Log.e("JapaneseTutor","Got here.[1]");
drawn=true;
Log.e("JapaneseTutor","Got here.[2]");
If you need to see the Timer class, or the full length of the GameHandler or ActualGame classes, just let me know.