d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Android Studio / Java
Add Reply New Topic New Poll
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Oct 27 2015 07:44pm
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 code
Code
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 code

Code
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?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 27 2015 10:20pm
You need to 1) log and 2) use those logs to explain what the problem is.

Quote
I am able to go from splash screen > to my game view but I cant get from game view > to game over screen


i assume this is the code in question?
Code
@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;
}

Since you gave no details, i'll have to ask you the obvious questions. 1) is this code executed? 2) is that comment relevant? if so, did you google it? http://stackoverflow.com/questions/3918517/calling-startactivity-from-outside-of-an-activity-context

Quote
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.


1) what's this supposed to do?
Code
long millis = millisUntilFinished / 1000;

2) is the code you expect to draw it on screen getting executed? is there an exception?
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Oct 28 2015 08:57pm
Quote (carteblanche @ Oct 28 2015 12:20am)
You need to 1) log and 2) use those logs to explain what the problem is.



i assume this is the code in question?
Code
@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;
}

Since you gave no details, i'll have to ask you the obvious questions. 1) is this code executed? 2) is that comment relevant? if so, did you google it? http://stackoverflow.com/questions/3918517/calling-startactivity-from-outside-of-an-activity-context

yes the code runs, i put that comment in there to help anyone whos trying to help me, yes i googled about activities and errors about calling activities outside the activity,

1) what's this supposed to do?

this is supposed to be my timer, i need it to count down from 1 second, when it expires it calls my
timeExpire() method which has other code in it such as
Code

m_timer.cancel
miss++
if (miss == MAX_MISSCOUNT (Var set to 3)){
gameOver()
}
else{
repositionTarget();
m_timer.start;
}

Code
long millis = millisUntilFinished / 1000;

2) is the code you expect to draw it on screen getting executed? is there an exception?


the code is getting drawn on screen but only one time. i want it to redraw when my timer expires
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll