当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to Double Score the point for a word?

问题描述:

I have my first Android app and I am having difficulty on how can I double the point for a word/words whenever a player click an imageview. Basically, I want it to function like this: If the player hasn't click the imageview, the pointing system stays the same. If the player clicks the imageview the a message will pop-out (a toast perhaps telling the player that the points for the next 3 words will be doubled). How can I make this happen? How can I double the points for the 3 consecutive words without exceeding?

Here's my code for searching and calculation

//CALCULATE SCORE

private int optionTxtView = 0 ; //which textview to use for addition purposes

private int addClick = 0 ; //No. of clicks for search for calculation

private void calculate(){

x = Integer.parseInt(tv3.getText().toString().replaceAll("\\s",""));

y = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));

z = x + y;

score.setText(Integer.toString(z));

}

//SEARCH WORD, DISPLAYING SCORE

public void viewWord(View view)

{

String s1= search.getText().toString(); //What word to search

String s2= dbHelper.getData(s1); // If in db, display equivalent score

if(optionTxtView == 0){

//display the score on textview1

tv2.setText(s2);

optionTxtView = 1;

}

else{

if(optionTxtView == 1){

//display the score on textview2

tv3.setText(s2);

optionTxtView = 0;

}

}

//Display search word/s

adapter.add(text.getText().toString());

adapter.notifyDataSetChanged();

if(addClick ==0){

calculate();

addClick = 1;

text.setText("");

generatedString="";

}

else{

if(addClick == 1){

calculate();

addClick = 2;

text.setText("");

generatedString="";

}}

网友答案:
boolean sDouble = false
imgView.setOnClickListener(new View.OnClickListener() {
   //@Override
   public void onClick(View v) {
      // Show your Toast message
      // double = true;
   }        
});

and where you are running your game code check the value of sDouble, if its true then double the scoring system inside a for loop, or set an integer value to 0 and increment it for next 3 words, after that set the value of sDouble back to false.

You may also need to make sure that the user doesn't click on the image for the next 3 tries after clicking it once.

Update:

Since your score is a String, you need to parse it to an Integer before performing any mathematical operation, use Integer.parseInt(s2).

分享给朋友:
您可能感兴趣的文章:
随机阅读: