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

2012/11/16

How to get a return statement from another class?

swapon swapon

2012/11/16

#
Hello, first of all I'm sorry for my English, it's far from being perfect. So, we've started programming with Java & Greenfoot a few weeks ago and I never used Java before. Our task is do create some kind of RPG with different levels. I want my hero (Robot) to switch levels as soon as a door in World 1 is removed which happens when Robot has a key in his inventory. This function is placed into the Robot class. So I tried to implement a bool return statement as soon as this happens. Switching levels has to be controlled by a subclass of World, "Weltenverwalter" (World-Administration). But I always get an error message that there is no such variable. These parts of my sourcecode are here; From my robot class;
     /**
     * Checks if a door can be removed
     */
    public boolean unlockDoor()
    {
        Actor object = getOneIntersectingObject(Door.class);
        if (object != null && !inventory.isEmpty() 
                && inventory.get() instanceof Key) { // Bonusaufgabe
            inventory.clear();
            getWorld().removeObject(object);
            return true;
        } else {
            return false;
        }
    }
and the part from my World-Administration;
public void checkWorld()
    {
        if(Robot.unlockDoor);
        { 
            if(Greenfoot.getWorld == Background);
            {
                Greenfoot.setWorld(Welt2);
            }
        
            if(Greenfoot.getWorld == Welt2);
            {
                Greenfoot.setWorld(Background);
            }
        }
    }
groengeel groengeel

2012/11/16

#
Try adding "()" in your if statement in the checkWorld() method.
groengeel groengeel

2012/11/16

#
To be clear the line should look like this:
public void checkWorld()  
    {  
        if(Robot.unlockDoor())
        {   
            // This doesn't work like this:    
            if(Greenfoot.getWorld() == Background)
            {  
                 // Same here (You are now calling the variable "Welt2")
                Greenfoot.setWorld(Welt2);  
            }  

            // This doesn't work like this:         
            if(Greenfoot.getWorld() == Welt2)
            {  
                 // Same here (You are now calling the variable "Background")
                Greenfoot.setWorld(Background);  
            }  
        }  
    }  
Try this. I: - removed ";" at the end of your if statements. - added "()" where you meant to use methods. If you are calling a method make sure you use () at the end of the name. Otherwise Java thinks you're calling a variable.
swapon swapon

2012/11/16

#
Thank you, but it still doesn't really work. In the same line;
        if(Robot.unlockDoor())  
I now get an error "non-static method unlockDoor() cannot be referenced from a static context" when compiling, although there is nothing static?
davmac davmac

2012/11/16

#
"Robot" is a class, not an object; that's what's meant by "static context". You can only call static methods on classes. You need a reference to a particular robot. You have a similar problem with your setWorld(...) commands: Greenfoot.setWorld(Background); // set world to a class, doesn't make sense Should probably be: Greenfoot.setWorld(new Background()); // set world to a world object
groengeel groengeel

2012/11/16

#
And as I pointed out in the code, I don't think this would work: if (Greenfoot.getWorld() == Background) I'd use: if (Greenfoot.getWorld() instanceof Background)
danpost danpost

2012/11/16

#
groengeel wrote...
I'd use: if (Greenfoot.getWorld() instanceof Background)
I do not this this would work either. If the method 'checkWorld' is in a sub-class of World, then you should use
if (this instanceof Background)
'getWorld()' is an Actor class method and can only be used on an actor object.
groengeel groengeel

2012/11/16

#
@danpost I stand corrected ;)
You need to login to post a reply.