开发者

Switching images with delay

I'm currently making my first ever Android application and I need some help. The app I'm making is the memory game Simon.

So I kinda have 2 questions:

1) When the user presses one of the color buttons, the background image of the button switches to a glowing version. But how can I get the button to have the original background image again after a small delay? I tried it with timer task (only for the red button), but the app crashes when I try it like that.

2) This should also happen automatically when the computer goes through the color combination. Is there a way to put it in a seperate function that can be called upon?

Here's the code: package android.Simon;

import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.TextView; import java.util.Random; import java.util.Timer; import java.util.TimerTask;

public class Simon extends Activity { Chronometer mChronometer; int[] ComArray = new int[100]; int gebruikergetal; int i = 0; int j = 0; int aantalcomgetallen = 1; //variable to determine the amount of colors the computer has to show int gebruikerteller; //variable to determine if it's the user's turn int startstop = 1; int delay = 1000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button;
    final TextView textView = (TextView) findViewById(R.id.textView1);
    final Button button1;
    final Button button2;
    final Button button3;
    final Button button4;

    for(i=0;i<100;i++){
        for(j=0;j<4;j++){
            ComArray[i] = RandomCreator.getRandomInt(1, 4);
        }
    }

    i=0;

    mChronometer = (Chronometer) findViewById(R.id.chronometer1);

    button = (Button) findViewById(R.id.button5);       
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);

    final View.OnClickListener Blauw = new OnClickListener() {
        public void onClick(View v){
            //int GebruikerArray[] = new int[i];
            gebruikergetal = 2;
            button1.setBackgroundDrawable(getResources().getDrawable(0x7f020001));
               开发者_如何学Go     //here i'm switching the background image of the button
            textView.setText(String.valueOf(gebruikergetal));
            if(gebruikergetal!=ComArray[i]){
                textView.setText("Game Over");
            }
            i++;
        }
    };

    final View.OnClickListener Groen = new OnClickListener() {
        public void onClick(View v){
            //int GebruikerArray[] = new int[i];
            gebruikergetal = 3;
            button2.setBackgroundDrawable(getResources().getDrawable(0x7f020003));
            textView.setText(String.valueOf(gebruikergetal));
            if(gebruikergetal!=ComArray[i]){
                textView.setText("Game Over");
            }
            i++;
        }
    };

    final View.OnClickListener Rood = new OnClickListener() {
        public void onClick(View v){
            //int GebruikerArray[] = new int[i];
            Timer timer = new Timer();

            gebruikergetal = 1;
            button3.setBackgroundDrawable(getResources().getDrawable(0x7f020006));
            //this is where I try out the timer task, but the app crashes
                    TimerTask task = new TimerTask(){
                public void run(){
                        button3.setBackgroundDrawable(getResources().getDrawable(0x7f020005));
                }
            };
            timer.scheduleAtFixedRate(task, 0, 1000);
            textView.setText(String.valueOf(gebruikergetal));
            if(gebruikergetal!=ComArray[i]){
                textView.setText("Game Over");
            }
            i++;
        }
    };

    final View.OnClickListener Geel = new OnClickListener() {
        public void onClick(View v){
            //int GebruikerArray[] = new int[i];
            gebruikergetal = 4;
            button4.setBackgroundDrawable(getResources().getDrawable(0x7f020009));
            textView.setText(String.valueOf(gebruikergetal));
            if(gebruikergetal!=ComArray[i]){
                textView.setText("Game Over");
            }
            i++;
        }
    };
    View.OnClickListener mStartListener = new OnClickListener() {
        public void onClick(View v) {
            if(startstop == 1){
                mChronometer.start();
                textView.setText("Com Turn!");
                button1.setOnClickListener(Blauw);
                button2.setOnClickListener(Groen);
                button3.setOnClickListener(Rood);
                button4.setOnClickListener(Geel);
                startstop = 0;
            }
            else{
                startstop = 1;
                mChronometer.stop();
                textView.setText("");
            }
        }
    };
    button.setOnClickListener(mStartListener);
}

}

If someone could help, I'd really appreciate it. Thx


Throw whatever you have to do in a method and use what's called a handler and runnable.

private Handler mHandler;

mHandler = new Handler(); 
mHandler.postDelayed(switchImages, delay (in milliseconds); 

 private Runnable switchImages = new Runnable() {
        public void run() {
            //Whatever method you make call here
                mHandler.postDelayed(switchImages, delay); 
                //This will call it again therefore continuous updating;
        }
    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜