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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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