Colin
Colin

Reputation: 329

Android Developement - If-Then-Else Statement. Switching images

So What I would like to do here is that the starting image is assigned with Tag 0. When I run the method, I want the image to change to gallows1 and get a tag of "1". Then when its run again, I want it to keep gallows1 with a tag of 1, and then change it to gallows2, and reassign it.... Basically, I want 1 image to change to the next, and so forth as the method runs 6 times.

Here is what I have so far.

public void switchImage(View view) {
        ImageView img = (ImageView) findViewById(R.id.imageView1);
        img.setTag("0");
        if (img.getTag() == "0") {
            img.setImageResource(R.drawable.gallows1);
            img.setTag("1");
        } else if (img.getTag() == "1") {
            img.setImageResource(R.drawable.gallows2);
            img.setTag("2");
        } else if (img.getTag() == "2") {
            img.setImageResource(R.drawable.gallows3);
            img.setTag("3");
        } else if (img.getTag() == "3") {
            img.setImageResource(R.drawable.gallows4);
            img.setTag("4");
        } else if (img.getTag() == "4") {
            img.setImageResource(R.drawable.gallows5);
            img.setTag("5");;
        } else if (img.getTag() == "5") {
            img.setImageResource(R.drawable.gallows6);
            img.setTag("6");
        } else if (img.getTag() == "6") {
            return;
        }
    }

The Game I am making is hangman, but instead of using graphics, I wanted to have a twist (its for a challenge proj) and have progressing images show up instead.

Upvotes: 0

Views: 370

Answers (1)

Fraggle
Fraggle

Reputation: 8727

Put this outside the method:

ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setTag("0") 

and remove img.setTag("0") from inside the method

The way you have it now, img.setTag("0") gets called everytime so it will always just hit the first if block. If you move it outside, then the first time the method is called, if block 1 will match, then the next time, if block 2, etc.

Upvotes: 1

Related Questions