Sim
Sim

Reputation: 590

Anyway to make an else if run a certain number of times?

e.g

else if (currentImage == nekoPics[4]) 
currentImage = nekoPics[5];

Need this to run twice, so my cat animation scratches twice, before moving to else, this is probably the worst way to do this, but im interested if you can have an else if execute twice without copy pasting

else if (currentImage == nekoPics[5]) 
currentImage = nekoPics[4];

and changing the indexes cause that just loops and he'll scratch all the time.

Any help is appreciated

edit: entire code

private void scratch(){
    for (int i = xPos; i <getWidth()/2; i+=0){
            xPos = getWidth()/2;
            // swap images

            if (currentImage == nekoPics[0]) 
                currentImage = nekoPics[1];
            else if (currentImage == nekoPics[1]) 
                    currentImage = nekoPics[2];
            else if (currentImage == nekoPics[2]) 
                    currentImage = nekoPics[4];
            else if (currentImage == nekoPics[4]) 
                currentImage = nekoPics[5];
            else 
                i+=10;

            repaint();
            pause(150)

Upvotes: 0

Views: 138

Answers (2)

paxdiablo
paxdiablo

Reputation: 882028

I gather you're moving through a sequence of images, so you could do something like this:

else if (currentImage == nekoPics[3]) {
    currentImage = nekoPics[4];
    countdown = 5;
} else if (currentImage == nekoPics[4]) {
    currentImage = nekoPics[5];
} else if (currentImage == nekoPics[5]) {
    if (countdown-- > 0)
        currentImage = nekoPics[4];
    else
        currentImage = nekoPics[6];
} else if ...

In other words, when transitioning from 3 to 4, set a countdown variable based on how often you want to repeat the 4,5 sequence.

Then transition from 5 to either 4 or 6 based on the countdown.

Upvotes: 0

Ingo
Ingo

Reputation: 36349

The whole if-else logic is superfluous. Put the images in an ordered collection (array, list) in the order thy should be shown, like this

 Pictures[] seq = new Pictures[] { pic1, pic2, pic3, pic3, pi2, pic4, pic5 };

Then go through the collection, or maintain an index that would point to the next picture.

Upvotes: 2

Related Questions