Note that there are some explanatory texts on larger screens.

plurals
  1. POIs my code too repetitive?
    primarykey
    data
    text
    <p>I was working on my text adventure game, and was wondering if there is an easier way to program repetitive blocks of code such as what I have below. </p> <p>In this block, the user is presented with N,E,S,W for North, East, South, and West. So I write each listener separately and include a try/catch block in each listener. But the whole block of code now seems really repetitive. </p> <p>Here is the block of code: </p> <pre><code>btnNorth.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { tvIntro.setText("You go north"); String testString = "nothing"; try { testString = Encounter.EncounterGeneratorText(); } catch (Exception e) { e.printStackTrace(); } testString = Encounter.EncounterGeneratorText(); tvIntro.setText(testString); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } tvIntro.setText(testString); } }); btnEast.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { tvIntro.setText("You go east"); String testString = "nothing"; try { testString = Encounter.EncounterGeneratorText(); } catch (Exception e) { e.printStackTrace(); } testString = Encounter.EncounterGeneratorText(); tvIntro.setText(testString); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } tvIntro.setText(testString); } }); btnSouth.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { tvIntro.setText("You go south"); String testString = "nothing"; try { testString = Encounter.EncounterGeneratorText(); } catch (Exception e) { e.printStackTrace(); } testString = Encounter.EncounterGeneratorText(); tvIntro.setText(testString); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } tvIntro.setText(testString); } }); btnWest.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { tvIntro.setText("you go west"); String testString = "nothing"; try { testString = Encounter.EncounterGeneratorText(); } catch (Exception e) { e.printStackTrace(); } testString = Encounter.EncounterGeneratorText(); tvIntro.setText(testString); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } tvIntro.setText(testString); }; }); </code></pre>
    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.
 

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