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

2013/5/26

Problem with code

Nsalhan01 Nsalhan01

2013/5/26

#
public class Boy extends Actor
{
    /**
     * Act - do whatever the Boy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act()
    {
        checkKeys();
        tryToEat();
    }
    
    public void checkKeys() 
    {
        if( Greenfoot.isKeyDown ( "left" ))
        {
            move (-6);
        }
        
        if( Greenfoot.isKeyDown ( "right" ))
        {
            move (6);
        }
        
    }  
  
    public void tryToEat()
    {
        if (canSee(Cake.class) )
        {
            eat(Cake.class);
        }
    }
    
    public void createNewCake()
   {
       Cake newCake;
       
       newCake = new Cake();
       
       World world;
       world = getWorld();
       
       world.addObject(newCake, 100, 100);
    }
}


It won't let me compile, it keeps on saying "cannot find symbol - method canSee(java.lang.Class<Cake>)".
danpost danpost

2013/5/26

#
The 'canSee' and the 'eat' methods are part of the Animal class, but this class extends Actor, not Animal.
Nsalhan01 Nsalhan01

2013/5/28

#
Yea I realized after I posted my question, sorry I'm a beginner. Would you happen to know how to add a functional welcome screen that allows the player to click for instruction and to start the game?
danpost danpost

2013/5/28

#
Yeah. Create a new World class and name it 'IntroWorld' or 'WelcomeWorld' or 'MenuWorld' (or whatever). Create an Actor class, called 'Text' or 'Button' that expects a String caption that is displayed on its image. Save the String caption in an instance field and write a public 'get' method to return that String value. In the MenuWorld class, in the constructor, create and add the button objects into the world. In the MenuWorld act method, check for any mouse-click. When detected, get the MouseInfo object and use the 'getActor' method to see if a button was clicked on using 'instanceof'. If a button was clicked, cast the actor to Button and get its caption value to determine what action to perform.
public void act()
{
    if (Greenfoot.mouseClicked(null))
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        Actor actor = mouse.getActor();
        if (actor != null && actor instanceof Button)
        {
            Button button = (Button)actor;
            String caption = button.getCaption();
            if ("Instructions".equals(caption))
            {
                /* display instructions */
            }
            if ("Start".equals(caption))
            {
                /* start game */
            }
        }
    }
}
Nsalhan01 Nsalhan01

2013/5/29

#
Thank you very much. Would you happen to know how to play a sound file when the game ends or include pop ups in the game?
You need to login to post a reply.