Let's say that I have a motion event, that when I hold a key my cannon shoots balls. Everything is okay but I needed to create a while() loop for this inside this event, cause balls were lagging.
The point is here I can't escape this event. The while loop is infinite and I can't listen for ACTION_UP. Is there any way to stop this on ACTION_UP while being in this loop?
EDIT: part of the code:
@Override
public void surfaceCreated(SurfaceHolder holder) {
game = new Game(holder, resources);
game.start();
shootingThread = new Thread(new Runnable() {
@Override
public void run(){
while(running) {
int size = game.gameLoop.balls.size();
if (size == 0) {
game.gameLoop.balls.add(new Ball(metrics.widthPixels, metrics.heightPixels, touched_x, touched_y, game.gameLoop.ball_bmp_width, metrics.heightPixels - game.gameLoop.ball_bmp_width / 2));
} else if (size > 0 && game.gameLoop.balls.get(size - 1).image_center_y < metrics.heightPixels - game.gameLoop.ball_bmp_width - 50)
game.gameLoop.balls.add(new Ball(metrics.widthPixels, metrics.heightPixels, touched_x, touched_y, game.gameLoop.ball_bmp_width, metrics.heightPixels - game.gameLoop.ball_bmp_width / 2));
}
// }
}
});
shootingThread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
touched_x = event.getX();
touched_y = event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
startShootTime = new Date().getTime();
running = true;
shootingThread.run();
Log.i("", "\n\ndown\n\n");
} else if (action == MotionEvent.ACTION_MOVE ) {
touched_x = event.getX();
touched_y = event.getY();
} else if (action == MotionEvent.ACTION_UP) {
Log.i("", "\n\nup\n\n");
running = false;
}
return true;
}