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

2013/12/1

Passing Parameters Between Worlds

1
2
Adept_Red Adept_Red

2013/12/6

#
HE CAN BE TAUGHT!!! Just tested it out by adding a System.out.println in the HS_controller and it successfully printed the score. Thank you guys so much for your help! There is of course the issue that HS_controller wont draw anything on the screen like it should... but the hardest part for me has been resolved. Here is the code as it is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Play screen. 
 */
public class Field_Screen extends World
{
    //Time for controling Bacon spawns
    private int wait = 0;
    private final int spawnDelay = 500;
    private boolean spawnBacon = false;
    
    //create reference to scoreCounter object
    ScoreCounter counter = new ScoreCounter();

    public Field_Screen()
    {    
        // Create a new world with 800x500 cells with a cell size of 1x1 pixels.
        super(800, 500, 1); 
        addObject(new PlayerUI(),145,75);
        addObject(new Player(),600,400); 
        addObject(new Rocket(),740,300);
        addObject(new CageTear(),300,400);
        addObject(counter,700,50);
        addObject(new PlayerHealth(),205,58);
        addObject(new PlayerMagic(),205,88);
        
    }
    	
		public void act()
		{
			makinBacon();
			endGame();
		}

        public void makinBacon()
        {//periodically places Bacon randomly on the field.
            // Cheacks wait timer
            if (wait > 0)wait--;
            if (wait == 0)spawnBacon = false;
            
            // Spawn Bacon when timer count down is complete
            if (!spawnBacon)
            {
                spawnBacon = true;
                wait += spawnDelay;
                addObject(new Bacon(), 400, 400);
            }
            
        }
        
        public void endGame()
        {
            int finalScore = counter.giveScore();

			if (Greenfoot.isKeyDown("escape"))
            {
                Highscore_Screen hss = new Highscore_Screen(finalScore);  
                //hss.getConstructor(finalScore);  
                Greenfoot.setWorld(hss);
            }
        }
        
        
}        

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

/**
 * Displays current score and prior scores.
 */
public class Highscore_Screen extends World
{
    /**
     * Constructor for objects of class Highscore_Screen.
     */
    public Highscore_Screen(int finalScore)
    {    
        // Create a new world with 800x500 cells with a cell size of 1x1 pixels.
        super(800, 500, 1); 
                        
        addObject(new Cursor_HS(),270,460);
        addObject(new HS_controller(finalScore),300,150);

		
    }

}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; // Because black text isn't going to work. 
import java.awt.Font; //Because default font is lame.
/**
 * Two screens, first displaying current achievments, and then high score list.
 */
public class HS_controller extends Actor
{
    private int yourScore;

	//constructor reciving score from HighScore
	public HS_controller(int finalScore)
	{
		yourScore = finalScore;
		
	}


    public void act() 
    {
        String []ranks = setRanks();
		int []previousScores = setPreviousScores();

        displayCurrent(yourScore, ranks);
        continueToHOF(yourScore, ranks, previousScores);
        
    }    
    

    public String[] setRanks()
    {
        String rankArray[] = new String[5];
        
        rankArray[0] = "GG in Sixty Seconds";
        rankArray[1] = "National Failure";
        rankArray[2] = "Ghost Ridin";
        rankArray[3] = "Wicked Man";
        rankArray[4] = "Paulemon Master!";
        
        return rankArray;
    }
    public int[] setPreviousScores()
	{
		int scoreArray[] = new int[5];
        
        scoreArray[0] = 8001;
        scoreArray[1] = 4001;
        scoreArray[2] = 2001;
        scoreArray[3] = 1001;
        scoreArray[4] = 1;

        return scoreArray;
	}

    

	public void displayCurrent(int yourScore, String[] ranks)
    {
        int a = 0;
        if (yourScore <= 1000) a=0;
        if (yourScore > 1000 && yourScore <= 2000) a=1;
        if (yourScore > 2000 && yourScore <= 4000) a=2;
        if (yourScore > 4000 && yourScore <= 8000) a=3;
        if (yourScore > 8000) a=4;
        
        setImage (new GreenfootImage(400, 400));
        GreenfootImage imgtext = getImage(); //current image
         
        //customizes font
        float fontSize = 30.0f;
        Font font = imgtext.getFont().deriveFont(fontSize);
        imgtext.setFont(font);
         
        //draws the image
        imgtext.setColor(Color.WHITE); 
        imgtext.drawString("Experience Gained: " + yourScore + "\n\n" +
                           "Your Rank: " + ranks[a], 0, 0);
    }
    
	public void continueToHOF(int yourScore, String[] ranks, int[] previousScores)
	{
		if (Greenfoot.isKeyDown("enter"))
		{
			
		}
	}
	
}
You need to login to post a reply.
1
2