Loading app/src/main/java/com/example/game2d/ChalkActivity.java +27 −44 Original line number Diff line number Diff line Loading @@ -28,20 +28,17 @@ import android.widget.Toast; public class ChalkActivity extends AppCompatActivity implements View.OnClickListener{ MediaPlayer backgroundMusic; TextView totalQuestionsTextView; TextView questionTextView; Button ansA, ansB, ansC, ansD; Button submitBtn/*, helpBtn*/; ImageButton pauseBtn; int chalkscore = 0; int totalWrong = 0; int totalQuestion = ChalkQuestionAnswer.question.length; int currentQuestionIndex = 0; int sendQuestionIndex = 0; String selectedAnswer = ""; int totalQuestion = ChalkQuestionAnswer.question.length; // the total number of questions public int currentQuestionIndex = 0; // Starts off as zero, but increments after pressing submit. (The first question has an index of 1) String selectedAnswer = ""; // A string containing the selected answer once the user clicks on a choice //for pop up intro RelativeLayout layout; Loading @@ -65,7 +62,7 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList layout = findViewById(R.id.chalkActivity); // relative is the id of the layout of the page CreatepopUpwindow(); /*totalQuestionsTextView = findViewById(R.id.total_question);*/ // Set the text for the question, choices, and submit button based on the data from ChalkQuestionAnswer.java questionTextView = findViewById(R.id.question); ansA = findViewById(R.id.ans_A); ansB = findViewById(R.id.ans_B); Loading @@ -82,8 +79,6 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList submitBtn.setOnClickListener(this); /*helpBtn.setOnClickListener(this);*/ /*totalQuestionsTextView.setText("Total questions : "+totalQuestion);*/ //Pause menu implementation pauseBtn.setOnClickListener(new View.OnClickListener() { @Override Loading @@ -96,7 +91,6 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList }); loadNewQuestion(); } //for pop up intro Loading Loading @@ -126,9 +120,6 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList } @Override public void onClick(View view) { //Initializing Access to shared preferences; SharedPreferences preferences = getSharedPreferences("MY_PREFS", 0); sendQuestionIndex = preferences.getInt("ChalkQuestionIndex", 0); ansA.setBackgroundColor(Color.WHITE); ansB.setBackgroundColor(Color.WHITE); Loading @@ -136,23 +127,23 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList ansD.setBackgroundColor(Color.WHITE); Button clickedButton = (Button) view; if(clickedButton.getId()==R.id.submit_btn){ if(selectedAnswer == "") { if(clickedButton.getId()==R.id.submit_btn){ // When submit button is clicked if(selectedAnswer == "") { // If no answer has been selected new AlertDialog.Builder(this) .setMessage("Please select an answer") .setMessage("Please select an answer.") .show(); } else if(selectedAnswer.equals(ChalkQuestionAnswer.correctAnswers[currentQuestionIndex])){ // correct choice has been chosen //SFX MediaPlayer correctSound = MediaPlayer.create(this, R.raw.correct); if (currentQuestionIndex != 3) { correctSound.start(); } selectedAnswer = ""; selectedAnswer = ""; // clear selectedAnswer chalkscore++; currentQuestionIndex++; preferences.edit().putInt("ChalkQuestionIndex", currentQuestionIndex).apply(); loadNewQuestion(); } else { } else { // wrong choice has been chosen MediaPlayer wrongSound = MediaPlayer.create(this, R.raw.wrong); wrongSound.start(); selectedAnswer = ""; Loading @@ -160,22 +151,14 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList totalWrong++; Log.d("QUESTION", "totalWrong++"); Intent spawnGame = new Intent(getApplicationContext(), QuizToChalkActivity.class); spawnGame.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); spawnGame.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); spawnGame.putExtra("QuestionCount", currentQuestionIndex); Log.d("QUESTION", "Starting chalk game..."); startActivity(spawnGame); getApplicationContext().startActivity(spawnGame); currentQuestionIndex++; preferences.edit().putInt("ChalkQuestionIndex", currentQuestionIndex).apply(); if(currentQuestionIndex != 4) { loadNewQuestion(); } else { preferences.edit().putInt("lastChalkScore", chalkscore).apply(); } } } else { //choices button clicked // one of the choices buttons clicked selectedAnswer = clickedButton.getText().toString(); clickedButton.setBackgroundColor(Color.MAGENTA); } Loading @@ -185,7 +168,7 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList // log question numbers Log.d("QUESTION", "current question index: " + String.valueOf(currentQuestionIndex)); //Log.d("QUESTION", String.valueOf(currentQuestionIndex)); if(currentQuestionIndex == totalQuestion ){ if(currentQuestionIndex == totalQuestion ){ // If current question is the last question Log.d("QUESTION", "finishing quiz..."); finishQuiz(); return; Loading @@ -193,6 +176,7 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList questionTextView.setText(ChalkQuestionAnswer.question[currentQuestionIndex]); // Randomize orders of choices displayed int[] orders = new int[]{0, 1, 2, 3}; // Randomize orders[] Random rand = new Random(); Loading Loading @@ -227,9 +211,8 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList SharedPreferences preferences = getSharedPreferences("MY_PREFS", 0); SharedPreferences.Editor editor = preferences.edit(); editor.putInt("lastChalkScore",chalkscore).apply(); editor.putInt("ChalkQuestionIndex",0).apply(); editor.putInt("lastChalkScore",chalkscore); editor.apply(); Intent intent = new Intent(getApplicationContext(),ChalkResultActivity.class);// startActivity(intent); Loading Loading
app/src/main/java/com/example/game2d/ChalkActivity.java +27 −44 Original line number Diff line number Diff line Loading @@ -28,20 +28,17 @@ import android.widget.Toast; public class ChalkActivity extends AppCompatActivity implements View.OnClickListener{ MediaPlayer backgroundMusic; TextView totalQuestionsTextView; TextView questionTextView; Button ansA, ansB, ansC, ansD; Button submitBtn/*, helpBtn*/; ImageButton pauseBtn; int chalkscore = 0; int totalWrong = 0; int totalQuestion = ChalkQuestionAnswer.question.length; int currentQuestionIndex = 0; int sendQuestionIndex = 0; String selectedAnswer = ""; int totalQuestion = ChalkQuestionAnswer.question.length; // the total number of questions public int currentQuestionIndex = 0; // Starts off as zero, but increments after pressing submit. (The first question has an index of 1) String selectedAnswer = ""; // A string containing the selected answer once the user clicks on a choice //for pop up intro RelativeLayout layout; Loading @@ -65,7 +62,7 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList layout = findViewById(R.id.chalkActivity); // relative is the id of the layout of the page CreatepopUpwindow(); /*totalQuestionsTextView = findViewById(R.id.total_question);*/ // Set the text for the question, choices, and submit button based on the data from ChalkQuestionAnswer.java questionTextView = findViewById(R.id.question); ansA = findViewById(R.id.ans_A); ansB = findViewById(R.id.ans_B); Loading @@ -82,8 +79,6 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList submitBtn.setOnClickListener(this); /*helpBtn.setOnClickListener(this);*/ /*totalQuestionsTextView.setText("Total questions : "+totalQuestion);*/ //Pause menu implementation pauseBtn.setOnClickListener(new View.OnClickListener() { @Override Loading @@ -96,7 +91,6 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList }); loadNewQuestion(); } //for pop up intro Loading Loading @@ -126,9 +120,6 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList } @Override public void onClick(View view) { //Initializing Access to shared preferences; SharedPreferences preferences = getSharedPreferences("MY_PREFS", 0); sendQuestionIndex = preferences.getInt("ChalkQuestionIndex", 0); ansA.setBackgroundColor(Color.WHITE); ansB.setBackgroundColor(Color.WHITE); Loading @@ -136,23 +127,23 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList ansD.setBackgroundColor(Color.WHITE); Button clickedButton = (Button) view; if(clickedButton.getId()==R.id.submit_btn){ if(selectedAnswer == "") { if(clickedButton.getId()==R.id.submit_btn){ // When submit button is clicked if(selectedAnswer == "") { // If no answer has been selected new AlertDialog.Builder(this) .setMessage("Please select an answer") .setMessage("Please select an answer.") .show(); } else if(selectedAnswer.equals(ChalkQuestionAnswer.correctAnswers[currentQuestionIndex])){ // correct choice has been chosen //SFX MediaPlayer correctSound = MediaPlayer.create(this, R.raw.correct); if (currentQuestionIndex != 3) { correctSound.start(); } selectedAnswer = ""; selectedAnswer = ""; // clear selectedAnswer chalkscore++; currentQuestionIndex++; preferences.edit().putInt("ChalkQuestionIndex", currentQuestionIndex).apply(); loadNewQuestion(); } else { } else { // wrong choice has been chosen MediaPlayer wrongSound = MediaPlayer.create(this, R.raw.wrong); wrongSound.start(); selectedAnswer = ""; Loading @@ -160,22 +151,14 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList totalWrong++; Log.d("QUESTION", "totalWrong++"); Intent spawnGame = new Intent(getApplicationContext(), QuizToChalkActivity.class); spawnGame.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); spawnGame.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); spawnGame.putExtra("QuestionCount", currentQuestionIndex); Log.d("QUESTION", "Starting chalk game..."); startActivity(spawnGame); getApplicationContext().startActivity(spawnGame); currentQuestionIndex++; preferences.edit().putInt("ChalkQuestionIndex", currentQuestionIndex).apply(); if(currentQuestionIndex != 4) { loadNewQuestion(); } else { preferences.edit().putInt("lastChalkScore", chalkscore).apply(); } } } else { //choices button clicked // one of the choices buttons clicked selectedAnswer = clickedButton.getText().toString(); clickedButton.setBackgroundColor(Color.MAGENTA); } Loading @@ -185,7 +168,7 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList // log question numbers Log.d("QUESTION", "current question index: " + String.valueOf(currentQuestionIndex)); //Log.d("QUESTION", String.valueOf(currentQuestionIndex)); if(currentQuestionIndex == totalQuestion ){ if(currentQuestionIndex == totalQuestion ){ // If current question is the last question Log.d("QUESTION", "finishing quiz..."); finishQuiz(); return; Loading @@ -193,6 +176,7 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList questionTextView.setText(ChalkQuestionAnswer.question[currentQuestionIndex]); // Randomize orders of choices displayed int[] orders = new int[]{0, 1, 2, 3}; // Randomize orders[] Random rand = new Random(); Loading Loading @@ -227,9 +211,8 @@ public class ChalkActivity extends AppCompatActivity implements View.OnClickList SharedPreferences preferences = getSharedPreferences("MY_PREFS", 0); SharedPreferences.Editor editor = preferences.edit(); editor.putInt("lastChalkScore",chalkscore).apply(); editor.putInt("ChalkQuestionIndex",0).apply(); editor.putInt("lastChalkScore",chalkscore); editor.apply(); Intent intent = new Intent(getApplicationContext(),ChalkResultActivity.class);// startActivity(intent); Loading