The problem I'm facing is that I have some floats set up and these are:
private volatile float yFromUI;
private volatile float yFromRenderThread;
private volatile float oldYFromUI;
private volatile float oldYFromRenderThread;
Within my onTouch method, my ACTION_MOVE case records the current and previous positions (I'm only concerned with Y coordinates). Like so:
case MotionEvent.ACTION_MOVE: {
yFromUI = event.getY();
if (yFromUI==oldYFromUI){
Log.v("Test","Values match (from onTouch)");
}
oldYFromUI=yFromUI;
break;
}
Then, in my rendering thread (within my main Game-loop as this app is a game) - I grab a 'snapshot' of the current coordinate of the pointer like so:
@Override
public void updateLogic() {
if (pointerDown){
yFromRenderThread = yFromUI;
if (oldYFromRenderThread==yFromRenderThread){
Log.v("NewTag","Values match");
}
oldYFromRenderThread = yFromRenderThread;
}
}
The problem
The problem is that when I move my finger down (or up) the screen, the Log statement is being fired multple time from within my Rendering thread (updateLogic) method. This is not what I would expect, these values should never match as long as my finger is moving.
By the same token, I would not expect the Log statement to fire from the UI Thread (onTouch / ACTION_MOVE) and it doesn't which is what I expect.
I'm guessing this is something to do with cross-thread communications, but I'm not sure how to solve it.
Can anyone spot why the old and current values are often logged as the same from my updateLogic method?
I've tried synchronizing the methods and also tried putting my variable manipulations within a synchronized(Object) block and as you can see I have declared them as volatile. So maybe I'm barking up the wrong tree with the cross-thread thing? But it just seems like it's something to do with that.
Any help and suggestions appreciated.