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

2017/4/21

How to link to a boolean from an actor, in the world?

AndreasAagaard AndreasAagaard

2017/4/21

#
I need to link to a Loose variable i have in my Actor, in my World. But i can't figure out how to link it, it doesn't recognize it in my World. Do i need some special code? This is the one i need to link
 public boolean getLoose()
    {
        Actor Ghost1 = getOneObjectAtOffset( 0, 0, Ghost1.class);
        Actor Ghost2 = getOneObjectAtOffset( 0, 0, Ghost2.class);
        Actor Ghost3 = getOneObjectAtOffset( 0, 0, Ghost3.class);
        Actor Ghost4 = getOneObjectAtOffset( 0, 0, Ghost4.class);
        if(Ghost1 != null){
            return true;
        }
            else if (Ghost2 != null){
                return true;
            }
                else if (Ghost3 != null){
                    return true;
                }
                   else if (Ghost4 != null){
                    return true;
                    
                    }
                    else{
                        return false;
                    }
           }
public void Loose()
    {
        
            if(getLoose()){
            
             addObject(new ScoreBoard(800, 600), getWidth() / 2, getHeight() / 2); 
             
        }
    }
davmac davmac

2017/4/21

#
You have no variable called Loose. I think you mean that you want to call the getLoose() method, defined in your actor, from your world. To do that you need to have stored a reference to the actor in a variable in your world. That is, when you create the actor, save it to an instance variable.
   MyActor myActor; // this should be an instance variable


   myActor = new MyActor(); // this should be in your constructor or initialisation method (such as "prepare")
Then you can call the method via:
   if (myActor.getLoose()) {
However, if I understand correctly you are trying to see if the game is over and "getLoose" should really be called "getLose". "Lose" is the opposite of "Win". "Loose" is the opposite of "Tight".
AndreasAagaard AndreasAagaard

2017/4/24

#
Im not sure i understand this fully. So what i've done is that i have made that reference you said, but im not sure its right. What i have now is this
public void Lose() {
        PacMan PacMan; // this should be an instance variable
 
 
        PacMan = new PacMan(); // this should be in your constructor or initialisation method (such as "prepare")
       
        if (PacMan.getLose()) {
            Greenfoot.setWorld(new Startscreen());
        }
    }
Is this right, or did i get it wrong?
danpost danpost

2017/4/24

#
You got it wrong. Line 2 should be before line 1 and line 5 should be after the 'super' call in the constructor of the class. You will also need to call the given method from the act method of the class. You may run into problem because of naming fields with the same names you named your classes. Instead of
PacMan PacMan;
better, and to standard convention, would be:
PacMan pacMan;
and the call to 'getLose' would be:
pacMan.getLose();
and the constructor line would be:
pacMan = new PacMan();
AndreasAagaard AndreasAagaard

2017/4/24

#
Ok, i changed that, but what i have to use it for is this code
addObject(new ScoreBoard(800, 600), getWidth() / 2, getHeight() / 2); 
This does not happen when the game ends. Why?
danpost danpost

2017/4/24

#
AndreasAagaard wrote...
Ok, i changed that, but what i have to use it for is this code
addObject(new ScoreBoard(800, 600), getWidth() / 2, getHeight() / 2); 
This does not happen when the game ends. Why?
Show the act method within the class that line is in.
You need to login to post a reply.