Question

I have some codes that I need help with. My plan for this code is simple. I have 3 buttons which are the yellowBar, greenBar and redBar. I also have 3 images that is colored yellow, green and red. What I wanted to do is when I start my app, the images I have will randomly pop out and whichever color comes out, I should be able to click the button with the corresponding color. I also have a time, so basically I want to click correctly as much as possible, but my code only runs it one time. my time continues to go down but this code only runs one time. How can I fix this so that it will continue to pop random color images until the time runs out?

while(gameTime > 0){
                colorNum = new Random();
                colorNumber = colorNum.nextInt(2);

                switch (colorNumber){
                    case 0: colors.setDisplayedChild(0);
                            break;

                    case 1: colors.setDisplayedChild(1);
                            break;

                    case 2: colors.setDisplayedChild(2);
                            break;
                }
                colors.setVisibility(View.VISIBLE);
                yellowBar.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v){
                        if(colorNumber == 0){
                            points ++;
                            score.setText(String.valueOf(points));
                        }
                    }
                });
                greenBar.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v){
                        if(colorNumber == 1){
                            points ++;
                            score.setText(String.valueOf(points));
                        }
                    }
                });
                redBar.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v){
                        if(colorNumber == 2){
                            points++;
                            score.setText(String.valueOf(points));
                        }
                    }
                });

            }
Was it helpful?

Solution

The better solution would be to render your graphics onClick event. When you press a button, you check if the answer was right, then you render the graphics.

using while loop like you do it now is wrong, since your graphics will be rendered all the time (whether button is pressed or not)

Basically (this is not a solution, rather a guideline based on your code):

         public void initialize(){
            yellowBar.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v){
                    if(colorNumber == 0){
                        points ++;
                        score.setText(String.valueOf(points));
                        updateColors();
                    }
                }
            });
            greenBar.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v){
                    if(colorNumber == 1){
                        points ++;
                        score.setText(String.valueOf(points));
                        updateColors();
                    }
                }
            });
            redBar.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v){
                    if(colorNumber == 2){
                        points++;
                        score.setText(String.valueOf(points));
                        updateColors();
                    }
                }
            });

        }
     }

     public void updateColors(){
     colorNum = new Random();
            colorNumber = colorNum.nextInt(2);

            switch (colorNumber){
                case 0: colors.setDisplayedChild(0);
                        break;

                case 1: colors.setDisplayedChild(1);
                        break;

                case 2: colors.setDisplayedChild(2);
                        break;
            }
            colors.setVisibility(View.VISIBLE);
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top