This site requires JavaScript, please enable it in your browser!
Greenfoot back
rn42v1r
rn42v1r wrote ...

2016/12/17

Help with switch statements

rn42v1r rn42v1r

2016/12/17

#
HI there, my code doesn't behave the way I want it to... I created 2 buttons which change the ScoreToWin like this: STW.STW = STW.STW + 1 and STW.STW = STW.STW -1 the class STW(score to win):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class STW extends Buttons
{   
    GreenfootImage stw1 = new GreenfootImage("stw1.png");
    GreenfootImage stw2 = new GreenfootImage("stw2.png");
    GreenfootImage stw3 = new GreenfootImage("stw3.png");
    GreenfootImage stw4 = new GreenfootImage("stw4.png");
    GreenfootImage stw5 = new GreenfootImage("stw5.png");
    GreenfootImage stw;
    static int STW = 3;
    
    public STW()
    {
        setImage(stw); 
    }
    
    /**
     * Act - do whatever the STW wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        changeSTW();
    }    
    
    public void changeSTW()
    {
        switch (STW)
        {
            case 1: stw = stw1;
                    break;
            case 2: stw = stw2;
                    break;
            case 3: stw = stw3;
                    break;
            case 4: stw = stw4;
                    break;
            case 5: stw = stw5;
                    break;
            default: stw = stw3;
                    break;
        }
    }
}
it doesn't change the Image of the class STW...
danpost danpost

2016/12/17

#
You are missing a setImage statement after the switch. Also, the value of STW never changes, so the same image will continually be set to the actor.
rn42v1r rn42v1r

2016/12/17

#
thank you... how could I miss that, guess it's too late again
You need to login to post a reply.