android - how to run multiple threads from the same method with differant values -
i have code should make differant plants grow @ differant times.... atm when next plant placed carries on method instead of creating new instance of method.... iv been programming 6 months learnt online.... not sure if should using asynctask or threads... appreciated....... need setv1sunflower()
keep whatever values selectedplant*
, start new instance of setlv1sunflower()
plants run seperetly each other
here screen ui class
package com.gameplay.mysticgarden; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.os.bundle; import android.os.handler; import android.view.view; import android.widget.button; import android.widget.imageview; import android.widget.textview; import android.widget.toast; public class gamescreen extends activity { static imageview selectedplantimage; static string selectedplanttoastmsg = ""; static boolean selectedplantfoundanimal = false; static boolean selectedplantfullygrown = false; static boolean placeholderempty = true; //plant 1 static imageview placeholder1image; final static string placeholder1toastmsg = ""; final static boolean placeholder1foundanimal = false; final static boolean placeholder1empty = true; final static boolean plant1fullygrown = false; //plant 2 static imageview placeholder2image; final static string placeholder2toastmsg = ""; final static boolean placeholder2foundanimal = false; final static boolean placeholder2empty = true; final static boolean plant2fullygrown = false; //plant 3 static imageview placeholder3image; static string placeholder3toastmsg = ""; static boolean placeholder3foundanimal = false; static boolean placeholder3empty = true; static boolean plant3fullygrown = false; //plant 4 static imageview placeholder4image; static string placeholder4toastmsg = ""; static boolean placeholder4foundanimal = false; static boolean placeholder4empty = true; static boolean plant4fullygrown = false; //plant 5 static imageview placeholder5image; static string placeholder5toastmsg = ""; static boolean placeholder5foundanimal = false; static boolean placeholder5empty = true; static boolean plant5fullygrown = false; //players points static textview waterpointsview; static int waterpoints = 50; //garden modes static boolean watermodeon = false; static boolean seedmodeon = false; //updates ui handler handler; //loop plants final int = 1; final int b = 2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.game_screen); //place holder plant 1 placeholder1image = (imageview) findviewbyid(r.id.placedholder1image); //invisable new game placeholder1image.setvisibility(view.invisible); //place holder plant 2 placeholder2image = (imageview) findviewbyid(r.id.placedholder2image); //invisable new game placeholder2image.setvisibility(view.invisible); //place holder plant 3 placeholder3image = (imageview) findviewbyid(r.id.placedholder3image); //invisable new game placeholder3image.setvisibility(view.invisible); //place holder plant 4 placeholder4image = (imageview) findviewbyid(r.id.placedholder4image); //invisable new game placeholder4image.setvisibility(view.invisible); //place holder plant 5 placeholder5image = (imageview) findviewbyid(r.id.placedholder5image); //invisable new game placeholder5image.setvisibility(view.invisible); //shown on new game showseedmenu(); setwatermodeon(); showwaterpoints(); //end of oncreate } //displays players water points public void showwaterpoints(){ waterpointsview = (textview) findviewbyid(r.id.waterpoints); waterpointsview.settext(string.valueof(waterpoints)); } //shows water dialog public void setwatermodeon(){ button waterbtn = (button) findviewbyid(r.id.waterbutton); waterbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { showwaterdialog(); } }); //end of setwatermodeon } //water dialog public void showwaterdialog(){ final alertdialog.builder waterdialog = new alertdialog.builder (gamescreen.this); waterdialog.settitle("water plant"); waterdialog.setmessage("will cost 1 water point"); waterdialog.setnegativebutton(r.string.no, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { } }); //if ok pressed -1 water point , turns watermodeon true waterdialog.setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { waterpoints--; textview waterpointsview = (textview) findviewbyid(r.id.waterpoints); waterpointsview.settext(string.valueof(waterpoints)); watermodeon = true; } }); waterdialog.create(); waterdialog.show(); } //place holder onclick shows info on plant or if watermodeon true shows found animal or no animal public void placeholder1(view view){ if (watermodeon == false){ toast.maketext(getapplicationcontext(), placeholder1toastmsg, toast.length_long).show(); } if (seedmodeon == true){ showseedmenu(); selectedplantimage = placeholder1image; selectedplanttoastmsg = placeholder1toastmsg; selectedplantfoundanimal = placeholder1foundanimal; selectedplantfullygrown = plant1fullygrown; } else{ showfoundanimaldialog(); } } //place holder onclick shows info on plant or if watermodeon true shows found animal or no animal public void placeholder2(view view){ if (watermodeon == false){ toast.maketext(getapplicationcontext(), placeholder2toastmsg, toast.length_long).show(); } if (seedmodeon == true){ showseedmenu(); selectedplantimage = placeholder2image; selectedplanttoastmsg = placeholder2toastmsg; selectedplantfoundanimal = placeholder2foundanimal; selectedplantfullygrown = plant2fullygrown; } else{ showfoundanimaldialog(); } } //place holder onclick shows info on plant or if watermodeon true shows found animal or no animal public void placeholder3(view view){ if (watermodeon == false){ toast.maketext(getapplicationcontext(), placeholder3toastmsg, toast.length_long).show(); } if (seedmodeon == true){ showseedmenu(); selectedplantimage = placeholder3image; selectedplanttoastmsg = placeholder3toastmsg; selectedplantfoundanimal = placeholder3foundanimal; selectedplantfullygrown = plant3fullygrown; } else{ showfoundanimaldialog(); } } //place holder onclick shows info on plant or if watermodeon true shows found animal or no animal public void placeholder4(view view){ if (watermodeon == false){ toast.maketext(getapplicationcontext(), placeholder4toastmsg, toast.length_long).show(); } if (seedmodeon == true){ showseedmenu(); selectedplantimage = placeholder4image; selectedplanttoastmsg = placeholder4toastmsg; selectedplantfoundanimal = placeholder4foundanimal; selectedplantfullygrown = plant4fullygrown; } else{ showfoundanimaldialog(); } } //place holder onclick shows info on plant or if watermodeon true shows found animal or no animal public void placeholder5(view view){ if (watermodeon == false){ toast.maketext(getapplicationcontext(), placeholder5toastmsg, toast.length_long).show(); } if (seedmodeon == true){ showseedmenu(); selectedplantimage = placeholder5image; selectedplanttoastmsg = placeholder5toastmsg; selectedplantfoundanimal = placeholder5foundanimal; selectedplantfullygrown = plant5fullygrown; } else{ showfoundanimaldialog(); } } //shows whether animal found or not public void showfoundanimaldialog(){ if (placeholder3foundanimal == true){ final alertdialog.builder foundanimaldialog = new alertdialog.builder (gamescreen.this); foundanimaldialog.settitle("you found animal!"); foundanimaldialog.setmessage("you found bee, , received lv2 sunflower seed."); foundanimaldialog.setnegativebutton(r.string.ok, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { watermodeon = false; placeholder3foundanimal = false; } }); foundanimaldialog.create(); foundanimaldialog.show(); } else{ final alertdialog.builder noanimaldialog = new alertdialog.builder (gamescreen.this); noanimaldialog.settitle("sorry!!!"); noanimaldialog.setmessage("no animal's here yet, keep trying"); noanimaldialog.setnegativebutton(r.string.ok, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { watermodeon = false; placeholder3foundanimal = false; } }); noanimaldialog.create(); noanimaldialog.show(); } } //sets new plant place holder public void showseedmenu(){ final charsequence[] items = {"lv1 sunflower", "lv1 daisy", "lv1 violet"}; alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("pick seed"); builder.setitems(items, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int item) { if (item == 0){ lv1seeds.setlv1sunflower(); } seedmodeon = false; } }); alertdialog alert = builder.create(); alert.show(); } public void showselectplantdialog(view view){ final alertdialog.builder foundanimaldialog = new alertdialog.builder (gamescreen.this); foundanimaldialog.settitle("plant new seed."); foundanimaldialog.setmessage("pick want place new plant."); foundanimaldialog.setnegativebutton(r.string.ok, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { seedmodeon = true; pickaspotimage(); } }); foundanimaldialog.create(); foundanimaldialog.show(); } public void pickaspotimage(){ placeholder1image.setvisibility(view.visible); placeholder2image.setvisibility(view.visible); placeholder3image.setvisibility(view.visible); placeholder4image.setvisibility(view.visible); placeholder5image.setvisibility(view.visible); } enter code here //end of class }
and values class holds values each placehoder
package com.gameplay.mysticgarden; import android.app.activity; import android.view.view; //level 1 seed values place holders public class lv1seeds extends activity { //ints loops final static int = 1; final static int b = 2; //lv1 sunflower values public static void setlv1sunflower(){ gamescreen.selectedplantfoundanimal = false; gamescreen.selectedplantimage.setimageresource(r.drawable.plant_stage1); gamescreen.selectedplantimage.setvisibility(view.visible); //first thread start /*runnable runnable = new runnable() { @override public void run() {*/ //2 lines of test code new thread(new runnable() { public void run() { //udate ui stage 1 of plant , sets toast msg while plant seedling while (a < b){ //sets animal gone when loop starts again gamescreen.selectedplantfoundanimal = false; //if plant not grown update ui , toast seedling stage if (gamescreen.selectedplantfullygrown == false){ gamescreen.selectedplanttoastmsg = "lv1 seedling sunflower \\(sunnious brightium\\) \nwill attract bee's!"; } //wait 20 seconds //change of values makes seedling stage last longer try { thread.sleep(1000 * 10 * 1); } catch (interruptedexception e) { e.printstacktrace(); } //update ui stage 2 of plant gamescreen.selectedplantimage.post(new runnable() { @override public void run() { //if plant not grown update ui , toast stage 2 if (gamescreen.selectedplantfullygrown == false){ gamescreen.selectedplantimage.setimageresource(r.drawable.plant_stage_2); gamescreen.selectedplanttoastmsg = "lv1 half grown sunflower \\sunnious brightium\\ \nwill attract bee's!"; } } }); //wait 20 seconds //change of value makes stage 2 last longer try { thread.sleep(1000 * 10 * 1); } catch (interruptedexception e) { e.printstacktrace(); } //update ui grown plant gamescreen.selectedplantimage.post(new runnable() { @override public void run() { //if plant not yet grown update ui , toast grown plant , sets plantfullygrown true if (gamescreen.selectedplantfullygrown == false){ gamescreen.selectedplantimage.setimageresource(r.drawable.flower_daisy); gamescreen.selectedplanttoastmsg = "lv1 sunflower \\(sunnious brightium\\) \nwill attract bee's!"; gamescreen.selectedplantfullygrown = true; } //used when plant grown else{ gamescreen.selectedplanttoastmsg = "lv1 sunflower \\(sunnious brightium\\) \nwill attract bee's!"; } } }); //wait 20 seconds //change of values make time longer before animal comes, once plant grown try { thread.sleep(1000 * 10 * 1); } catch (interruptedexception e) { e.printstacktrace(); } //sets animal in garden gamescreen.selectedplantimage.post(new runnable() { @override public void run() { gamescreen.selectedplantfoundanimal = true; } }); //animal stays time period //change of value makes animal stay longer try { thread.sleep(1000 * 10 * 1); } catch (interruptedexception e) { e.printstacktrace(); } } } }).start(); //end of sunflower() } //end of class }
and xml gamescreen
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_sun_1" > <button android:id="@+id/waterbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:background="@drawable/water_drop" /> <textview android:id="@+id/waterpoints" android:layout_width="300sp" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/waterbutton" android:layout_alignbottom="@+id/waterbutton" android:layout_alignparentleft="true" android:layout_marginleft="176dp" android:text="large text" android:textappearance="?android:attr/textappearancelarge" android:textsize="36sp" /> <imageview android:id="@+id/placedholder1image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_toleftof="@+id/placedholder2image" android:background="@drawable/blackspaceflower" android:onclick="placeholder1" android:visibility="invisible" /> <imageview android:id="@+id/placedholder2image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_toleftof="@+id/placedholder3image" android:background="@drawable/blackspaceflower" android:onclick="placeholder2" android:visibility="invisible" /> <imageview android:id="@+id/placedholder3image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:background="@drawable/blackspaceflower" android:onclick="placeholder3" android:visibility="invisible" /> <imageview android:id="@+id/placedholder4image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_torightof="@+id/placedholder3image" android:background="@drawable/blackspaceflower" android:onclick="placeholder4" android:visibility="invisible" /> <imageview android:id="@+id/placedholder5image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_torightof="@+id/placedholder4image" android:background="@drawable/blackspaceflower" android:onclick="placeholder5" android:visibility="invisible" /> <button android:id="@+id/seedbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="36dp" android:layout_margintop="22dp" android:onclick="showselectplantdialog" android:text="@string/seeds" /> </relativelayout>
please help!!!
Comments
Post a Comment