java - Going back to Main Activity on Android app with back key to see user score -
hopefully can advice on this.
my program has sprite that, when hit/clicked, score tallied on screen. when game on (time runs out), want user able press key on phone go main/start screen/activity began game, show score (how many hits).
i'm struggling figure out how go back key press. can figure out high score bit @ later date if need be. want start screen.
update:
i think may have manifest why it's not working. i've tried add 'gameview' class manifest won't accept it. says has no default constructor. ideas?
<application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="cct.mad.lab.mainmenu" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".gameactivity" android:label="@string/app_name" > </activity> <activity android:name=".gameview" android:label="@string/app_name" > </activity>
my gameview code below:
public class gameview extends surfaceview implements surfaceholder.callback { /* member (state) fields */ private gameloopthread gameloopthread; private paint paint; //reference paint object /** drawable use background of animation canvas */ private bitmap mbackgroundimage; // creating game sprite private sprite sprite; // recording number of hits private int hitcount; // track if game on private boolean gameover; // play sound private soundplayer sound; //int backbuttoncount = 0; public gameview(context context) { super(context); // focus must on gameview events can handled. this.setfocusable(true); // intercepting events on surface. this.getholder().addcallback(this); // background image added mbackgroundimage = bitmapfactory.decoderesource(this.getresources(), r.drawable.half_moon); //sound = new soundplayer(this); } /* called after surface created */ public void surfacecreated(surfaceholder holder) { // can safely setup game start game loop. resetgame();//set new game - called 'play again option' mbackgroundimage = bitmap.createscaledbitmap(mbackgroundimage, getwidth(), getheight(), true); gameloopthread = new gameloopthread(this.getholder(), this); gameloopthread.running = true; gameloopthread.start(); } // countdown timer private long starttime; // timer count down private final long interval = 1 * 1000; // 1 sec interval private countdowntimer countdowntimer; // reference class private boolean timerrunning = false; private string displaytime; // display time on screen //to initialise/reset game private void resetgame(){ /* set paint details */ paint = new paint(); paint.setcolor(color.white); paint.settextsize(20); sprite = new sprite(this); hitcount = 0; // set timer starttime = 10; // start @ 10s count down // create new object - convert starttime milliseconds countdowntimer = new mycountdowntimer(starttime*1000, interval); countdowntimer.start(); // start time running timerrunning = true; gameover = false; } // countdown timer - private class private class mycountdowntimer extends countdowntimer { public mycountdowntimer (long starttime, long interval) { super(starttime, interval); } public void onfinish() { //displaytime = "time up!"; timerrunning = false; countdowntimer.cancel(); gameover = true; } public void ontick (long millisuntilfinished) { displaytime = " " + millisuntilfinished / 1000; } } //this class updates , manages assets prior drawing - called thread public void update(){ sprite.update(); } /** * draw game screen * called thread, synchronisation can done */ public void dodraw(canvas canvas) { //draw objects on canvas canvas.drawbitmap(mbackgroundimage, 0, 0, null); if (!gameover) { sprite.draw(canvas); canvas.drawtext("time remaining: " + displaytime, 35, 50, paint); canvas.drawtext("number of hits: " + hitcount, 250, 50, paint); } else canvas.drawtext("game over!", 185, 100, paint); canvas.drawtext("to go main menu, press 'back' key", 15, 150, paint); } //to used if need find screen touched public boolean ontouchevent(motionevent event) { if (sprite.wasittouched(event.getx(), event.gety())) { // renews sprite sprite = new sprite(this); //sound.playzapsound(); hitcount++; } return true; } public void surfacedestroyed(surfaceholder holder) { gameloopthread.running = false; // shut down game loop thread cleanly. boolean retry = true; while(retry) { try { gameloopthread.join(); retry = false; } catch (interruptedexception e) {} } } public void gethitcount() { } public void surfacechanged(surfaceholder holder, int format, int width, int height) { } }
my mainactivity below:
public class mainmenu extends activity { private static final int score_request_code = 1;// request code intent textview tvscore; string score; intent gameintent; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.game_start); } public void startgame(view v){ gameintent = new intent(this,gameactivity.class); startactivityforresult(gameintent, score_request_code ); } /* create options menu */ public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main_menu, menu); return true; } // respond item selected on options menu public boolean onoptionsitemselected(menuitem item) { // handle item selection switch (item.getitemid()) { //put data in intent case r.id.easy: toast.maketext(this, "easy chosen", toast.length_short).show(); return true; case r.id.medium: toast.maketext(this, "medium chosen", toast.length_short).show(); return true; case r.id.hard: toast.maketext(this, "hard chosen", toast.length_short).show(); return true; case r.id.other: toast.maketext(this, "other chosen", toast.length_short).show(); return true; default: return super.onoptionsitemselected(item); } } protected void onactivityresult(int requestcode, int resultcode, intent retintent) { // check request we're responding if (requestcode == score_request_code) { // make sure request successful if (resultcode == result_ok) { if (retintent.hasextra("game_score")) { int scorefromgame = retintent.getextras().getint("game_score"); tvscore.settext(integer.tostring(scorefromgame)); } } } } }
as always, appreciated.
thanks
use onbackpressed.
@override public void onbackpressed() { intent i= new intent(your_present_activity.this,the_activity_you_want_to_jump_to.class); startactivity(i); finish(); }
you can take score main.class using newintent.putextra(name,"key");
Comments
Post a Comment