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

2013/1/4

How do i make one screen before another?

Oxy Oxy

2013/1/4

#
Im trying to make a Title screen for when my game runs, im using two classes extending world, one being the real world, the other being my title screen. I'm going to have a button you select to play game and at that point it will do Greenfoot.setWorld(new MYWORLD()); and it will begin. but i dont know how to make my title screen first, then the games world
vonmeth vonmeth

2013/1/4

#
Right click on the world's class and on the popup menu that pops up select "new WorldName()". Greenfoot will remember that is the default one to start with. There is no code you can add, or change (as far as I know) to do this.
Oxy Oxy

2013/1/4

#
oh OK thanks
Oxy Oxy

2013/1/4

#
OK new Question if anyone happens to open this and read it, i got my button set up and did the Greenfoot setWorld to set the world... but when i click the play button in my game the new Frame does not show up... anyone know why? or how i should do it? am i missing a command like some sort of method to remove my current Frame?
danpost danpost

2013/1/4

#
We will need to see what code you have for the play button and the code for checking it being clicked on.
Oxy Oxy

2013/1/4

#
Ok now it's working that when i click on either single player / two player the image changes, and i can click play then the new world shows up. but then i need to click on Run again. My question is what do i need to change so that 1) if i select two player, then 1 play that the two player button unselects itself, and vice versa. and 2) how do i make it so when you click Play i don't need to click Run again it will just start? Heres the Code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
import java.awt.Color;
import java.util.Calendar;
/**
 * Write a description of class UnderWater here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UnderWater extends World
{
    Points Dpoints = new Points();
    Points Spoints = new Points();
    
    private boolean Players;
    
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;
    
    
    private int sharkNumber = 0;
    
    /**
     * Constructor for objects of class UnderWater.
     * 
     */
    public UnderWater(boolean Players)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1);
        this.Players = Players;
    }
    
   
    public void act()
    {
       int fishNumber; //To hold a value for a random fish number
       
       fishNumber = Greenfoot.getRandomNumber(1500);
       //Blue Fish
        if ( fishNumber <= 20)
         addObject(new BlueFish(),Greenfoot.getRandomNumber((getWidth() / 2) + 400),Greenfoot.getRandomNumber((getHeight() / 2) + 200));   
        //GreenFish 
        else if (fishNumber >= 21 && fishNumber <= 25)
            addObject(new GreenFish(),Greenfoot.getRandomNumber((getWidth() / 2) + 400),Greenfoot.getRandomNumber((getHeight() / 2) + 200));
        //OrangeFish    
        else if (fishNumber >= 26 && fishNumber <= 29)
            addObject(new OrangeFish(),Greenfoot.getRandomNumber((getWidth() / 2) + 400),Greenfoot.getRandomNumber((getHeight() / 2) + 200));
        //PurpleFish    
        else if (fishNumber >= 30 && fishNumber <= 31)
            addObject(new PurpleFish(),Greenfoot.getRandomNumber((getWidth() / 2) + 400),Greenfoot.getRandomNumber((getHeight() / 2) + 200));
        //RedFish    
        else if (fishNumber >= 34 && fishNumber <= 36)
            addObject(new RedFish(),Greenfoot.getRandomNumber((getWidth() / 2) + 400),Greenfoot.getRandomNumber((getHeight() / 2) + 200));
        //Shark    
        else if (fishNumber == 666 && sharkNumber < 3)
        {
            addObject(new Shark(Spoints),Greenfoot.getRandomNumber((getWidth() / 2) + 400),Greenfoot.getRandomNumber((getHeight() / 2) + 200));
            sharkNumber++;
        }
            
    }
    
    
        private void prepare()
    {
       
       addObject(Dpoints, 100, 550);
       if (Players == true){
           addObject(Spoints, 900, 550);
        }
       Dolphin d = new Dolphin(Dpoints);
       
       addObject(d, getWidth() / 2, getHeight() / 2); 
       
    }
}
Title Screen:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TitleScreen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TitleScreen extends World
{
    private boolean TwoPlayer = false;
    
    PlayButton play = new PlayButton();
    PlayersONE One = new PlayersONE();
    PlayersTWO two = new PlayersTWO();
    /**
     * Constructor for objects of class TitleScreen.
     * 
     */
    public TitleScreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(450, 300, 1);
        prepare();
        
    }
    
    
    public void act()
    {
       if (One.getOnePlayer()== true){
            two.setPlayers(false);
            TwoPlayer = false;
        }
        
       if (two.getTwoPlayers() == true){
            One.setPlayers(false);
            TwoPlayer = true;
        } 
        
       if (play.getStart() == true && One.getOnePlayer() == true || two.getTwoPlayers() == true){
            
            Greenfoot.setWorld(new UnderWater(TwoPlayer));
            Greenfoot.stop();
        }
    }
   
    
    private void prepare()
    {
        //PLayers one
        addObject(One, 54, 30);
        //Player 2
        addObject(two, 192, 30);
        //Play button
        addObject(play, 123, 72);
    }
    
    
    
}
3 Buttons
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PlayButton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayButton extends Actor
{
    private boolean Startgame = false; 
    private boolean clicked = false;
    private GreenfootImage background;
    
    public PlayButton(){
        background = getImage();
        updateImage();
    }
    
    /**
     * Act - do whatever the PlayButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this)){
        Startgame = true;
        updateImage();
        }
    }    
    
    
    public boolean getStart(){
        return Startgame;
    }
    
    private void updateImage(){
        
        GreenfootImage image = new GreenfootImage(background);
        
        setImage(image);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PlayersONE here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayersONE extends Actor
{
    private boolean OnePlayer = false;
    private boolean clicked = false;
    private GreenfootImage background;
    
    
   
    public PlayersONE(){
        background = getImage();
        updateImage();
    
    }
    
    
    /**
     * Act - do whatever the PlayersONE wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
         if (Greenfoot.mouseClicked(this)){
           OnePlayer = true;
           clicked = true;
           
         updateImage();  
        }
          
    }    
    
    
    public void setPlayers(boolean players){
        OnePlayer = players;
    }
    
    public boolean getOnePlayer(){
        return OnePlayer;
    }
    
    
    private void updateImage(){
        
        if (clicked == false){
         this.setImage("1playerbutton1.jpg");
        }
        
        if (clicked == true){
         this.setImage("OnePlayerButtonTwo.jpg");
        }
        
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PlayersTWO here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayersTWO extends Actor
{
   private boolean TwoPlayers = false;
   private boolean clicked = false;
   private GreenfootImage background;
   
   
   public PlayersTWO(){
        background = getImage();
        updateImage();
    }
    
    /**
     * Act - do whatever the PlayersTWO wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
         if (Greenfoot.mouseClicked(this)){
            TwoPlayers = true;
            clicked = true;
            
            updateImage();
        }
    }   
    
    public void setPlayers(boolean players){
        TwoPlayers = players;
    }
    
    
   public boolean getTwoPlayers(){
        return TwoPlayers;
    } 
    
    
    
     private void updateImage(){
        
        if (clicked == false){
         this.setImage("2playerbutton1.jpg");
        }
        
        if (clicked == true){
         this.setImage("2playerbutton2.jpg");
        }
        
        
    }
}
danpost danpost

2013/1/4

#
Since one of the player number buttons needs to be 'true' before the play button can work, it might be best to start one with 'clicked = true;' and call 'updateImage' from the constructor. Then when one is clicked on, set it the 'true' and the other to 'false'. You can use something like the following in both players button classes:
// before updateImage() call in the act method
Players### p# =  (Players###) getWorld().getObjects(Players###.class).get(0);
p#.cancelClicked();
// add the following method in both classes
public void cancelClicked()
{
    clicked = false;
    updateImage();
}
Oxy Oxy

2013/1/4

#
should i keep the updateImage() i have in my Act() method? along with the updateImage() in the cancelClicked() method?
Oxy Oxy

2013/1/4

#
ok yeah i did and its working great on the image aspect THANK YOU! I wasnt really sure how to send Actors across other actors without making a new one, this helps me out alot now, and will definitly help in the future! Any ideas on how i can make the game actually start when you click play without having to click run? or is that just a factor of GreenFoot that you need to click run every time you switch worlds?
danpost danpost

2013/1/4

#
Absolutely. Any time you change the value of 'clicked' you will need to update the image of the button. And since it can be changed two different ways (needing two different codes) you need it in both places.
danpost danpost

2013/1/4

#
You shouldn't have to click 'Play' when your world changes. Do you have a 'Greenfoot.stop();' command in your code that is stopping the execution between worlds? Remove line 44 of the TitleScreen world.
Oxy Oxy

2013/1/4

#
oh excitement! Thank you Danpost. I had that before because I thought i needed it to stop one world so the other could appear. Much appreciated.
You need to login to post a reply.