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

2013/1/17

How to change the background if an actor hits the edge of the world?

1
2
bbwf bbwf

2013/1/17

#
Hey I was wondering what would the code be if when an actor hits the right or lefts edge of the world it will change the background?
Gevater_Tod4711 Gevater_Tod4711

2013/1/17

#
First you need to know if the actor hits the edge of the world using a mehtod like this:
    public boolean atWorldEdge() {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20) {
            return true;
        }
        if(getY() < 20 || getY() > getWorld().getHeight() - 20) {
            return true;
        }
        else {
            return false;
        }
    }
If this method return true you have to change the worldbackground like this:
((YourWorld) getWorld()).setBackground("filename.png"); //YourWorld has to be the classname of your world class.
Im not sure if the cast is necessary but it should work this way.
bbwf bbwf

2013/1/17

#
Where would I put the setbackground?
bbwf bbwf

2013/1/17

#
My world is named Background, I want to set the file to test.png I have replaced everything and tried it out and it won't compile. Says unreachable statement.
danpost danpost

2013/1/17

#
'unreachable statement' error will only show when you place code somewhere where the logical execution of the method results in never being able to get to the statement indicated; possibly due to a 'return' statement before it. Check the logic of the method the error occurs in.
bbwf bbwf

2013/1/17

#
So than where do I put the setbackground?
vonmeth vonmeth

2013/1/17

#
Where you check if the edge of the world has been hit ...
danpost danpost

2013/1/17

#
Did you fix your 'unreachable statement' issue?
bbwf bbwf

2013/1/18

#
Sorry, I'm so confused.
bbwf bbwf

2013/1/18

#
And danpost I have not.
danpost danpost

2013/1/18

#
Please show what code you have.
bbwf bbwf

2013/1/18

#
I do not have access to it right now, I can give it to you tomorrow when I get access.
danpost danpost

2013/1/18

#
I'll be happy to help you out, if I can. Until then.
bbwf bbwf

2013/1/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bob here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bob extends Actor
{
    /**
     * Act - do whatever the bob wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkForArrowKeys(4);
       
       
    }
    public void checkForArrowKeys (int adjust)
    {
        if ( Greenfoot.isKeyDown("a") )

        {
            setLocation(getX() - adjust, getY());
        }
        if ( Greenfoot.isKeyDown("d") )
 

        {
            setLocation(getX() + adjust, getY() );
        }
        //if ( Greenfoot.isKeyDown("w") )

       // {
          //  s//etLocation(getX() , getY() - adjust );
       // }
        //if ( Greenfoot.isKeyDown("s") )

      //  {
            //setLocation(getX() , getY() + adjust );
        //}

    }
    public boolean atWorldEdge() {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20) {
            return true;
        }
        if(getY() < 20 || getY() > getWorld().getHeight() - 20) {
            return true;
         ((Background) getWorld()).setBackground("lol.png"); //YourWorld has to be the classname of your world class.
        }
        else {
            return false;
        }
    }
    
}
danpost danpost

2013/1/18

#
Line 52 is not reachable because you have a 'return' statement immediately before it.
There are more replies on the next page.
1
2