I'm building a small game for a college project. It's quite a simple game. There are a number of dots on the screen and the player has to select them in a specific order. It's kind of a "join the dots" thing, it's for my 4 year old son to play with.
So we've been pointed towards a few game tutorials by the college, I've been studying one of them, and I'm currently at this point:
http://www.javacodegeeks.com/2011/07/android-game-development-game-loop.html
However, I'm a little stuck here as this is where my app deviates from the tutorial. There is nothing moving on my screen, just pictures of dots that change colour when they are touched. At the moment I'm updating the status of each dot based on the position of the users finger relative to the dots using a switch statement in the onTouchEVent() method in my SurfaceView Class.
it looks like this:
NOTE: the method .checkCurrentStatus() updates the image on the point based on whether the point has been touched or not.
public boolean onTouchEvent(MotionEvent event){
Log.i(TAG, "Event: " + event.getActionMasked());
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
sounds.play(soundDrip, 1.0f, 1.0f, 0, 0, 1.5f);
image.handleActionDown((int) event.getX(), (int) event.getY());
for(LetterPoint p : capitalA){
p.handleActionDown((int) event.getX(), (int) event.getY());
Log.i(TAG, "Set state of point " + p + " 'isTouched()' to " + p.isTouched());
}
if(event.getY() > getHeight() - 50){
gameThread.setRunning(false);
Activity activity = (Activity) getContext();
activity.finish();
}else{
Log.i(TAG, "Co-ordinates: x= " + event.getX() + " y= " + event.getY());
}
break;
case MotionEvent.ACTION_MOVE:
for(int i = 0; i < capitalA.length; i++){
capitalA[i].handleActionDown((int) event.getX(), (int) event.getY());
Log.i(TAG, "Set state of point " + i + " 'isTouched()' to " + capitalA[i].isTouched());
capitalA[i].checkCurrentStatus();
if(i != 0){
if(capitalA[i-1].getCurrentState() == LetterPoint.status.green){
capitalA[i].setCurrentState(LetterPoint.status.orange);
capitalA[i].checkCurrentStatus();
Log.i(TAG, "Set state of point " + i + " to " + capitalA[i].getCurrentState().toString());
}
}
if(capitalA[i].getCurrentState() == LetterPoint.status.orange && capitalA[i].isTouched()){
capitalA[i].setCurrentState(LetterPoint.status.green);
capitalA[i].checkCurrentStatus();
if(!capitalA[i].isCorrectPlayed()){
sounds.play(soundCorrect, 1.0f, 1.0f, 0, 0, 1.5f);
capitalA[i].setCorrectPlayed(true);
}
Log.i(TAG, "Set state of point " + i + " to " + capitalA[i].getCurrentState().toString());
}
}
break;
case MotionEvent.ACTION_UP:
int count = 0;
for(LetterPoint p : capitalA){
if(p.getCurrentState() == LetterPoint.status.green) count++ ;
Log.i(TAG, "Green count is: " + count);
}
if(count == capitalA.length){
sounds.play(soundWin, 1.0f, 1.0f, 0, 0, 1.5f);
}else{
sounds.play(soundLoose, 1.0f, 1.0f, 0, 0, 1.5f);
for(int i = 0; i < capitalA.length; i++){
if(i < 1){
capitalA[i].setCurrentState(LetterPoint.status.orange);
}else{
capitalA[i].setCurrentState(LetterPoint.status.red);
}
capitalA[i].setTouched(false);
capitalA[i].checkCurrentStatus();
}
}
break;
}
So given that all my updating is handled in the onTouchEvent() method how can I best control the frame rate of the thread? Should I just store the variables for each touch point in the SurfaceView class on every touch event then update the SurfaceView periodically from a separate update method called by the Thread? EDIT: Also, where should I call the sound effects from? the Switch statement above? the update method? the thread when I'm drawing?