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

2014/1/12

Change world background

sanoy sanoy

2014/1/12

#
I want to change the worlds background by pressing left or right, but it doesnt work
public void auswahl()
    {
        if(auswahl == 0)
        {
            if(Greenfoot.isKeyDown("right"))
            {
                auswahl = 1;
            }
            if(Greenfoot.isKeyDown("left"))
            {
                auswahl = 1;
            }
        }
        if(auswahl == 1)
        {
            if(Greenfoot.isKeyDown("right"))
            {
                auswahl = 0;
            }
            if(Greenfoot.isKeyDown("left"))
            {
                auswahl = 0;
            }
        }
    }
    public boolean enter()
    {
        if(Greenfoot.isKeyDown("enter"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void act()
    {
        if(start == 0)
        {
              if(auswahl == 1)
              {
                  setBackground("mainmenu2.jpg");
                  if(enter() == true)
                  {
                      setBackground("sand2.png");
                      start = 1;
                      populate();
                  }
              }
              if(auswahl == 0)
              {
                  setBackground("mainmenu.jpg");
              }
              auswahl();
        }
What do i have to change
danpost danpost

2014/1/12

#
Probably nothing. Try just adding 'else' at the beginning of line 14 above. However, it appears you have more inherent problems than that. Mainly because of your usage of 'isKeyDown' and the lack of control that it provides (by itself -- or without a boolean backup to track their state of up or down). 'isKeyDown' will register true multiple times even with a quick push of the key where 'getKey' only registers once (when the key is released).
sanoy sanoy

2014/1/12

#
now it chnages randomly between the two when i press left or right
danpost danpost

2014/1/12

#
I was in the middle of editing my last post when you chimed in. Please note what I added there. The fact that 'now it chnages randomly between the two' is the result of how you are using the 'isKeyDown' method. What functionality are you trying to achieve with this class. Please explain what it is supposed to do and show the entire class code. That way we will have a better idea of how to proceed to correct your issues.
sanoy sanoy

2014/1/12

#
if the key is pressed it checks wich image it is by the intager auswahl. Then it should set the other image. All in once it should be a main menu, where you could select the game you want to play, but can“t hold down the button, to select faster
danpost danpost

2014/1/12

#
What I am seeing is this (and, btw, this is how you should explain things): before the enter key is pressed, you (I) want that if an arrow key (left or right) is pressed, then the background image should switch between mainmenu and mainmenu2, one change per arrow keypress; after the enter key is pressed the background should change to sands2 and the world is populated. If this is this correct, then:
public void act()
{
    if (start == 0)
    {
        String key = Greenfoot.getKey();
        if ("left".equals(key) || "right".equals(key))
        {
            auswahl = (auswahl+1)%2; // alternates: 0, 1, 0, 1...
            if (auswahl == 0) setBackground("mainmenu.jpg");
            else setBackground("mainmenu2.jpg");
        }
        if ("enter".equals(key))
        {
            start = 1;
            setBackground("sands2.png");
            populate();
        }
    }
}
Use this code to replace ALL the code in your initial post if I was correct in what you wanted.
sanoy sanoy

2014/1/12

#
thx
You need to login to post a reply.