Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to run multiple threads from the same method with differant values
    primarykey
    data
    text
    <p>i have some code that should make differant plants grow at differant times.... but atm when the next plant is placed it just carries on the method instead of creating a new instance of that method.... iv only been programming about 6 months just learnt online.... not sure if i should be using aSyncTask or Threads... any help would be greatly appreciated....... I MAINLY NEED TO <code>SETV1SUNFLOWER()</code> TO KEEP WHATEVER VALUES <code>selectedPlant*</code> is and start a new instance of <code>setLv1Sunflower()</code> so that all plants run seperetly from each other</p> <p>here is my screen UI class</p> <pre><code> 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 FOR PLANTS final int a = 1; final int b = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game_screen); //PLACE HOLDER FOR PLANT 1 placeHolder1Image = (ImageView) findViewById(R.id.placedHolder1Image); //INVISABLE FOR NEW GAME placeHolder1Image.setVisibility(View.INVISIBLE); //PLACE HOLDER FOR PLANT 2 placeHolder2Image = (ImageView) findViewById(R.id.placedHolder2Image); //INVISABLE FOR NEW GAME placeHolder2Image.setVisibility(View.INVISIBLE); //PLACE HOLDER FOR PLANT 3 placeHolder3Image = (ImageView) findViewById(R.id.placedHolder3Image); //INVISABLE FOR NEW GAME placeHolder3Image.setVisibility(View.INVISIBLE); //PLACE HOLDER FOR PLANT 4 placeHolder4Image = (ImageView) findViewById(R.id.placedHolder4Image); //INVISABLE FOR NEW GAME placeHolder4Image.setVisibility(View.INVISIBLE); //PLACE HOLDER FOR PLANT 5 placeHolder5Image = (ImageView) findViewById(R.id.placedHolder5Image); //INVISABLE FOR 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 a 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 AND TURNS waterModeOn TO 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 IS 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 IS 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 IS 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 IS 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 IS 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 WAS FOUND OR NOT public void showFoundAnimalDialog(){ if (placeHolder3FoundAnimal == true){ final AlertDialog.Builder foundAnimalDialog = new AlertDialog.Builder (GameScreen.this); foundAnimalDialog.setTitle("You Found An Animal!"); foundAnimalDialog.setMessage("You just found a Bee, and received a 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 A NEW PLANT TO A PLACE HOLDER public void showSeedMenu(){ final CharSequence[] items = {"Lv1 SunFlower", "Lv1 Daisy", "Lv1 Violet"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Pick A 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 A New Seed."); foundAnimalDialog.setMessage("Pick where you want to place a 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 } </code></pre> <p>and my values class that holds the values for each placehoder</p> <pre><code>package com.gameplay.mysticgarden; import android.app.Activity; import android.view.View; //LEVEL 1 SEED VALUES FOR PLACE HOLDERS public class Lv1Seeds extends Activity { //INTS FOR LOOPS final static int a = 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 TO 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 AND SETS TOAST MSG WHILE PLANT IS SEEDLING while (a &lt; b){ //SETS ANIMAL TO GONE FOR WHEN LOOP STARTS AGAIN GameScreen.selectedPlantFoundAnimal = false; //IF PLANT IS NOT FULLY GROWN UPDATE UI AND TOAST FOR 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 TO STAGE 2 OF PLANT GameScreen.selectedPlantImage.post(new Runnable() { @Override public void run() { //IF PLANT IS NOT FULLY GROWN UPDATE UI AND TOAST FOR 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 TO FULLY GROWN PLANT GameScreen.selectedPlantImage.post(new Runnable() { @Override public void run() { //IF PLANT IS NOT YET FULLY GROWN UPDATE UI AND TOAST FOR FULLY GROWN PLANT AND SETS plantFullyGrown TO 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 IS FULLY 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 IS FULLY 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 FOR THIS 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 } </code></pre> <p>and the xml for GameScreen</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>please help!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload