If someone could help me out figuring out my basic 'whack-a-mole' app I can pay all the fg I have, not much but hey its something..
1st:
I am able to go from splash screen > to my game view but I cant get from game view > to game over screen
2nd:
I am able to draw the target to the screen but I cannot draw it a second time and I believe it has something to do with my reflex timer method, I don't know what to put in it.
Play game view codeCode
package edu.davenport.cisp340.reflex;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import java.util.Random;
public class PlayGameView extends View {
private Context m_context;
private ReflexTimer m_timer;
private Bitmap m_targetBitmap;
private Rect m_targetRect;
private Bitmap m_target;
private Random rand;
private int scrnH;
private int scrnW;
private final int TARGET_DELAY = 2000;
private final int MAX_MISS_COUNT = 3;
private int headerHeight;
private int viewHeight;
private int hitCount;
private int missCount;
SoundPool hitSound, missSound;
int hSound, mSound;
public PlayGameView(Context context) {
super(context);
//Sounds for hit and miss
m_context = context;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
AudioAttributes hitMissAtributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_GAME)
.build();
hitSound = new SoundPool.Builder()
.setMaxStreams(10)
.setAudioAttributes(hitMissAtributes)
.build();
hSound = hitSound.load(m_context,R.raw.hit,1);
missSound = new SoundPool.Builder()
.setMaxStreams(10)
.setAudioAttributes(hitMissAtributes)
.build();
mSound = hitSound.load(m_context,R.raw.hit,1);
}
else {
hitSound = new SoundPool(10, AudioManager.STREAM_MUSIC,1);
hSound = hitSound.load(m_context,R.raw.miss,1);
missSound = new SoundPool(10, AudioManager.STREAM_MUSIC,1);
mSound = hitSound.load(m_context,R.raw.miss,1);
}
// context
m_context = context;
// hit count
hitCount = missCount = 0;
//random number
rand = new Random(System.currentTimeMillis());
//load bitmap
m_targetBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.target);
// get dimension
WindowManager winman = (WindowManager)m_context.getSystemService(Context.WINDOW_SERVICE);
scrnW = winman.getDefaultDisplay().getWidth();
scrnH = winman.getDefaultDisplay().getHeight();
m_targetRect = repositionTarget(scrnW,scrnH);
// draw game screen
invalidate();
//create a timer that will expire in 1 second and start it.
m_timer = new ReflexTimer(TARGET_DELAY, this);
m_timer.start();
}
public Rect repositionTarget(int height, int width){
Rect r = new Rect();
r.left = rand.nextInt(scrnW - m_targetBitmap.getWidth());
r.top = rand.nextInt(scrnH - m_targetBitmap.getHeight() - headerHeight);
r.right = r.left + m_targetBitmap.getWidth();
r.bottom = r.top + m_targetBitmap.getHeight();
return r;
}
@Override
public void onDraw (Canvas c){
c.drawBitmap(m_targetBitmap, m_targetRect.left, m_targetRect.top, null);
}
@Override
public void onSizeChanged ( int newW, int newH, int oldW, int oldH){
viewHeight = newH;
headerHeight = scrnH - viewHeight;
}
public void timeExpire() {
onMiss();
}
public void onHit(){
hitSound.play(hSound,1,1,1,0,1);
m_timer.cancel();
repositionTarget(scrnH,scrnW);
m_timer.start();
}
public void onMiss(){
missSound.play(mSound,1,1,1,0,1);
m_timer.cancel();
repositionTarget(scrnH,scrnW);
m_timer.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
m_timer.cancel();
float x = event.getRawX();
float y = event.getRawY();
y -= headerHeight;
if(m_targetRect.contains((int) x, (int) y)){
onHit();
}
else{
onMiss();
}
if( y < 100 ) // I am using this to simulate the game is over by clicking at a point less than 100 on the screen but it crashes bc of line 172 says the logcat 'Activity context requires the FLAG_ACTIVITY_NEW_TASK flag'
{
Intent gameOver = new Intent(m_context, GameOverActivity.class);
[COLOR=red]m_context.startActivity(gameOver);[/COLOR]
((Activity)m_context).finish();
}
return false;
}
}
Reflex Timer codeCode
package edu.davenport.cisp340.reflex;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
public class ReflexTimer extends CountDownTimer {
private PlayGameView pgv;
public ReflexTimer(int milliseconds, PlayGameView gameView){
super(milliseconds, milliseconds);
pgv = gameView;
}
@Override
public void onTick(long millisUntilFinished){
long millis = millisUntilFinished / 1000;
onFinish();
}
@Override
public void onFinish(){
pgv.timeExpire();
}
}
any suggestions?