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

2016/2/21

Change the Background

eve19 eve19

2016/2/21

#
Here I am again! Now I want to change Backgrounds by clicking the left or right arrow.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Home here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Home extends World
{

    /**
     * Constructor for objects of class Home.
     * 
     */
    public Home()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        setBackground("a.png");
        Greenfoot.setSpeed(30); 

}
    public World inventory = new Inventory(this);
    public void changeBackground()
    {

        GreenfootImage a = new GreenfootImage("a.png");
        GreenfootImage b = new GreenfootImage("b.png");
        GreenfootImage c = new GreenfootImage("c.png");

        key key=new key();
        if (Greenfoot.isKeyDown("right")&&  inventory.getObjects(key.class).size()==0) {
        setBackground(b);
        addObject(key,300,300);

        }
        else if( Greenfoot.isKeyDown("right") && inventory.getObjects(key.class).size()!=0 )
        {setBackground(b);}
   if ( Greenfoot.isKeyDown("right") 
       setBackground(c);

        if(Greenfoot.isKeyDown("left")){
        setBackground(a);
        }
    
        if (Greenfoot.isKeyDown("i")){
            Greenfoot.setWorld(inventory);
        }

       }
    }
    public void act() {
        changeBackground();
    }
}

That's what I've done so far, but I want to change the background to "c" if the current background is "b" and this doesn't happen and I've tried this with so many ways to the whole code as an example:
 if ( Greenfoot.isKeyDown("right")&& this.getBackground()==b ){ 
       setBackground(boy);

       }
Please I need some help.
Super_Hippo Super_Hippo

2016/2/21

#
Move lines 28 through 30 outside methods.
danpost danpost

2016/2/22

#
Super_Hippo wrote...
Move lines 28 through 30 outside methods.
You should also change line 20 to this:
setBackground(a);
so that any later comparisons to that background with 'a' will return a 'true' value.
eve19 eve19

2016/2/22

#
Well, I changed all the things you both said, but again it doesn't work :/
eve19 eve19

2016/2/22

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class Home here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Home extends World
{

    /**
     * Constructor for objects of class Home.
     * 
     */
    public Home()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

       setBackground(a);
        Greenfoot.setSpeed(30); 

}
    public World inventory = new Inventory(this);
    GreenfootImage a = new GreenfootImage("a.png");
        GreenfootImage b = new GreenfootImage("b.png");
        GreenfootImage c = new GreenfootImage("c.png");

    public void changeBackground()
    {      
        key key=new key();
        if (Greenfoot.isKeyDown("right")&& this.getBackground()==a && inventory.getObjects(key.class).size()==0) {
        setBackground(b);
        addObject(key,300,300);

        }
        else if( Greenfoot.isKeyDown("right") && this.getBackground()==a && inventory.getObjects(key.class).size()!=0 )
        {setBackground(b);}

        if(Greenfoot.isKeyDown("left") && this.getBackground()==b){
        setBackground(a);
        }
if(Greenfoot.isKeyDown("left") && this.getBackground()==c){
        setBackground(b);
        }
    
        if (Greenfoot.isKeyDown("i")){
            Greenfoot.setWorld(inventory);
        }

         if ( Greenfoot.isKeyDown("right")&& this.getBackground()==b ){ 
       setBackground(c);

       }
    }
    public void act() {
        changeBackground();
    }
}
the problem is that from a sometimes goes to b and sometimes to c,but I want a to b and b to c and backwards.
Super_Hippo Super_Hippo

2016/2/22

#
If you press 'right', line 34 sets the background to 'b' and line 53 sets it to 'c' in the same act cycle. I think you could avoid that by adding an 'else' before every if (except the first one of course).
danpost danpost

2016/2/22

#
None of these key actions are ones that require multiple act cycles to process. I think the main issue is due to using the 'isKeyDown' method, which will continue to return a 'true' value for every act cycle the key is down. This is causing the image to change repeatedly until you release the key. For this, you may want to use the 'getKey' method instead, which returns 'true' upon the release of the key (which only happens once for every time the key is pressed). Then, it might look like this:
String key = Greenfoot.getKey();
if ("right".equals(key))
{
    if (getBackground() == a) setBackground(b);
    else if (getBackground() == b) setBackground(c);
}
if ("left".equals(key))
{
    if (getBackground() == c) setBackground(b);
    else if (getBackground() == b) setBackground(a);
}
if ("i".equals(key)) Greenfoot.setWorld(inventory);
After this change, you can probably remove the 'setSpeed' call on line 22.
You need to login to post a reply.