Coding Horror, be scared

Simple Android App

The app crashes after switching to this activity. It isn't a problem with the manifest or any of that because it worked fine before I actually filled in the class with real code.

It is supposed to be a game that switches screen colors between red, yellow, and green. The player is awarded 2 points for tapping it when it is yellow, one for when it is green, and they lose if they tap on a red screen. Over time, the changes become shorter and less predictable. In the onCreate() method, there is an intro that plays a "Ready, set, go!" type of 'animation'. The previous activity was just the splash screen.

untitled.php
package com.nateolson.taptap; import android.graphics.Color; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; import java.util.concurrent.ThreadLocalRandom; public class GameActivity extends AppCompatActivity { final Handler introHandler = new Handler(); boolean gameActive = false; final View layout = findViewById(R.id.layout); //for main method only int playerPoints = 0; long timeMinMillis = 1000; long timeMaxMillis = 3000; long randTime; long switchReferenceTime = System.currentTimeMillis(); String colorStage = "green"; //for main method only @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); final TextView readyMessageView = (TextView) findViewById(R.id.readyTV); final TextView getSetMessageView = (TextView) findViewById(R.id.setTV); getSetMessageView.setVisibility(View.INVISIBLE); final TextView goMessageView = (TextView) findViewById(R.id.goTV); goMessageView.setVisibility(View.INVISIBLE); layout.setBackgroundColor(Color.RED); Runnable switchToYellow = new Runnable() { @Override public void run() { readyMessageView.setVisibility(View.GONE); layout.setBackgroundColor(Color.YELLOW); getSetMessageView.setVisibility(View.VISIBLE); } }; Runnable switchToGreen = new Runnable() { @Override public void run() { getSetMessageView.setVisibility(View.GONE); layout.setBackgroundColor(Color.GREEN); goMessageView.setVisibility(View.VISIBLE); } }; Runnable gameRunnable = new Runnable() { public void run() { gameActive = true; } }; introHandler.postDelayed(switchToYellow, 1500); introHandler.postDelayed(switchToGreen, 3000); introHandler.postDelayed(gameRunnable, 4500); main(); } public void main() { while(gameActive) { randTime = ThreadLocalRandom.current().nextLong(timeMinMillis, timeMaxMillis); if (System.currentTimeMillis() - switchReferenceTime >= randTime) { switch (colorStage) { case "green": colorStage = "yellow"; break; case "yellow": colorStage = "red"; break; case "red": colorStage = "green"; break; } switchReferenceTime = System.currentTimeMillis(); } switch (colorStage) { case "green": layout.setBackgroundColor(Color.GREEN); case "yellow": layout.setBackgroundColor(Color.YELLOW); case "red": layout.setBackgroundColor(Color.RED); } timeMinMillis -= .03 * timeMinMillis; timeMaxMillis -= .02 * timeMaxMillis; layout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (colorStage) { case "green": playerPoints++; case "yellow": playerPoints += 2; case "red": playerPoints = 0; gameActive = false; } return false; } }); } } }

Reactions 0 reactions

No reactions yet, be the first: comment or tweak the code

Comment on this code:

Please Login or Register to leave a comment.