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

2016/3/16

Level 2 World Confusion

GreenMan15 GreenMan15

2016/3/16

#
Hello, I have a question. I have a world and its my level 2 world, and everytime I get hit I am supposed to lose a life which works perfectly fine in my level 1 world. I am getting an error saying java.lang.ClassCastAException. Level2 cannot be cast to MyWorld. Then it highlights two lines in red which I will show on the next line. I put my level2 source code on the bottom along with my life counter just in case. See anything I can add? at Man.checkHit(Man.java:64) at Man.act(man.java:20)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//man class, fyi its a plane
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Man here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Man extends Actor
{
    public Man()
    {
       GreenfootImage myImage = getImage();
       int myNewHeight = (int)myImage.getHeight()/3;
       int myNewWidth = (int)myImage.getWidth()/3;
       myImage.scale(myNewWidth, myNewHeight);  
    }
    public void act()
    {
      checkHit();
      checkKeys();
    }   
     
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-8);
        }
         
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+8);
        }
         
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+4, getY());
        }
         
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-4, getY());
        }
        if ("space".equals(Greenfoot.getKey()))
        {
           shoot();
        }
         
    }
    private void shoot()
    {
        Ammo ammo = new Ammo();
        getWorld().addObject(ammo, getX(), getY());
         
     }
    public void checkHit()
    {
      Actor a = this.getOneIntersectingObject(Rocket.class);
      if (a != null)
      {
        this.getWorld().removeObject(a); 
        World myWorld = getWorld();
        MyWorld world = (MyWorld)(myWorld);
        LifeCounter count = world.getCounter();
        count.countLives();
      }
      Actor b= this.getOneIntersectingObject(Enemy.class);
      if (b != null)
      {
        this.getWorld().removeObject(b); 
        World myWorld = getWorld();
        MyWorld world = (MyWorld)(myWorld);
        LifeCounter count = world.getCounter();
        count.countLives();
      
         
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Level2 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level2 extends World
{
    boolean startMusic = true;
    private int time;
    private int span = 3;
    GreenfootSound backGroundMusic = new GreenfootSound("MySounds.mp3");
    public Level2()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1024,768,1);
        time = 1500;
        Music();
        prepare();
         
    }
    
    public void Music()
    {
        if (startMusic)
        {
            backGroundMusic.playLoop();
            startMusic= false;
        }
    }
    private void prepare()
    {
        Man man = new Man();
        addObject(man,702,241);
        Level level = new Level();
        addObject(new Level(), 512, 260);
        LifeCounter life = new LifeCounter();
        addObject(life, 880, 20);
    }
 
    public void act()
    {
        showTheTime();
        Music();
         
        if (Greenfoot.getRandomNumber(100) <3 )
        {
            addObject(new Enemy(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
        }
        if (Greenfoot.getRandomNumber(100) <3)
        {
            addObject(new Rocket(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
        }
        
    }
 
    public void showTime()
    {
        showText("Time: " + time, 50, 20);
    }
 
    public void showTheTime()
    {
        time--;
        showTime();
        if(time==0)
        {
            Greenfoot.stop();
            showText("You Won! ", 450, 275);
            backGroundMusic.stop();
        }
 
    }
}
[code]import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class LifeCounter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class LifeCounter extends Actor
{
    int lives = 2;
    boolean startMusic = true;
    GreenfootSound backGrounddMusic = new GreenfootSound("Sound2.mp3");
    public void act()
    {
        setImage(new GreenfootImage("Lives: " + lives, 24, Color.GREEN, Color.BLACK));
        gameOver();
        Music();
    }   
 
    public void countLives()
    {
        lives--;
    }
 
    public void gameOver()
    {
        if (lives == 0)
        {
            Greenfoot.stop();
            World myWorld = getWorld();
            GameOver game = new GameOver();
            myWorld.addObject(game, 450,216);
            backGrounddMusic.stop();
        }
 
    }
 
    public void endMusic()
    {
         
    }
 
    public void Music()
    {
        if (startMusic)
        {
            backGrounddMusic.playLoop();
            startMusic= false;
 
        }
    }
Super_Hippo Super_Hippo

2016/3/16

#
First, don't add a comment here which isn't in your program code if the error message wants to lead to the right line. (Line 1) That is the line which isn't working:
1
MyWorld world = (MyWorld)(myWorld);
This can't work if the actor is in Level2 which is not a MyWorld. You could do something like this:
1
2
3
4
5
6
7
8
9
10
if (getWorld() instanceof MyWorld)
{
    MyWorld world = (MyWorld) getWorld();
    //...
}
else if (getWorld() instanceof Level2)
{
    Level2 world = (Level2) getWorld();
    //...
}
As an alternative, you could subclass your worlds like this: -World --Level ---Level1 (or MyWorld) ---Level2 Then you could add the 'getCounter' method in the Level world, so you can get the counter after casting any world returned by 'getWorld' to 'Level'.
GreenMan15 GreenMan15

2016/3/16

#
The subclass sounds really useful and much easier to understand for me. I never thought about that. Appreciate it
GreenMan15 GreenMan15

2016/3/16

#
But how am I supposed to adjust my background if its in actor. I cannot use the super(object, x ,y ) method. I may have misunderstood what you said, what exactly did you mean?
GreenMan15 GreenMan15

2016/3/16

#
Oh I understand now I didnt know you could have a subclass of a world.
GreenMan15 GreenMan15

2016/3/16

#
Would it be possible to just make two life counters then implement each one into a different world?
danpost danpost

2016/3/16

#
GreenMan15 wrote...
Would it be possible to just make two life counters then implement each one into a different world?
It is possible to do that -- but, it may not be the best way, as it depends on whether you do not mind having any lives lost in the first world to be restored in the second world. If you want the lives value to be retained from one world to the next, creating a separate counter would just complicate things. If the number of lives is not specific to the individual worlds and more specific to the game in general, then the counter object can be held in a class field instead of being held by any object created from a class.
GreenMan15 GreenMan15

2016/3/16

#
Well good thing is in my game, I start off with 3 lives and if I survive for an certain amount of time, then I go to the next level and start with 2 lives and try to survive until time runs out and so on. Does that make it easier for me to try to do that?
danpost danpost

2016/3/16

#
That would be quite simple if a set number of lives were given to complete each world level. You could always try to change things up later (if you decide to).
GreenMan15 GreenMan15

2016/3/17

#
I tried doing the way I said, good news I didnt get any errors, bad news its not counting down my lives when I get hit lol. What did I do wrong? I did the exact same thing that I did for my first level. Here are the 3 classes I put in order. airship: when it gets hit, it loses 1 life, level2: the world, lifecounter actor class: method implemented.
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
import greenfoot.*;
public class Level2 extends World
{
    boolean startMusic = true;
    private int time;
    private int span = 3;
    GreenfootSound backGroundMusic = new GreenfootSound("MySounds.mp3");
    LifeCounterr countt = new LifeCounterr();
    public Level2()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1024,768,1);
        time = 1500;
        Music();
        prepare();
    }
     
    public void Music()
    {
        if (startMusic)
        {
            backGroundMusic.playLoop();
            startMusic= false;
        }
    }
    public void prepare()
    {
        AirShip ship = new AirShip();
        addObject(ship,702,241);
        Level level = new Level();
        addObject(new Level(), 512, 260);
        LifeCounterr lifee = new LifeCounterr();
        addObject(lifee, 880, 20);
    }
  
    public void act()
    {
        showTheTime();
        Music();
         if (Greenfoot.getRandomNumber(100) <3 )
        {
            addObject(new Enemylvl2(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
        }
    }
  
    public void showTime()
    {
        showText("Time: " + time, 50, 20);
    }
  
    public void showTheTime()
    {
        time--;
        showTime();
        if(time==0)
        {
            Greenfoot.stop();
            showText("You Won! ", 450, 275);
            backGroundMusic.stop();
        }
  
    }
    public LifeCounterr getCounterr()
    {
        return countt;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class AirShip here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class AirShip extends Actor
{
    public AirShip()
    {
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/2;
        int myNewWidth = (int)myImage.getWidth()/2;
        myImage.scale(myNewWidth, myNewHeight);
    }
 
    public void act()
    {
        checkKeyss();
        checkHitt();
    }   
 
    private void checkKeyss()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-8);
        }
 
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+8);
        }
 
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+4, getY());
        }
 
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-4, getY());
        }
        if ("space".equals(Greenfoot.getKey()))
        {
            shoot();
        }
 
    }
 
    private void shoot()
    {
        Ammoo ammoo = new Ammoo();
        getWorld().addObject(ammoo, getX(), getY());
 
    }
 
    public void checkHitt()
    {
        Actor c = this.getOneIntersectingObject(Enemylvl2.class);
        if (c != null)
        {
            this.getWorld().removeObject(c); 
            World myWorldd = getWorld();
            Level2 woorld = (Level2)(myWorldd);
            LifeCounterr countt = woorld.getCounterr();
            countt.countTheLives();
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class LifeCounter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class LifeCounterr extends Actor
{
    int livess = 2;
    boolean startMusic = true;
    //GreenfootSound backGrounddMusic = new GreenfootSound("Sound2.mp3");
    public void act()
    {
        setImage(new GreenfootImage("Lives: " + livess, 24, Color.GREEN, Color.BLACK));
        gameOverr();
        //Music();
    }   
 
    public void countTheLives()
    {
        livess--;
    }
 
    public void gameOverr()
    {
        if (livess == 0)
        {
            Greenfoot.stop();
            World myWoorld = getWorld();
            //GameOver game = new GameOver();
            //myWorld.addObject(game, 450,216);
            //backGrounddMusic.stop();
        }
 
    }
danpost danpost

2016/3/17

#
Remove everything dealing with lifecounters from your subclasses of Level and put it only in your subclass of World (Level). Then all references to the lifeounter in your actor class(es) can have your world cast to Level and be able to access the methods there regardless of which type subclass of Level is active.
GreenMan15 GreenMan15

2016/3/17

#
Thanks! I will try that!
GreenMan15 GreenMan15

2016/3/17

#
Dan I just want to personally thank you for your help, it finally works! I couldn't believe I just had to put my method in just one World subclass.
danpost danpost

2016/3/17

#
GreenMan15 wrote...
Dan I just want to personally thank you for your help, it finally works! I couldn't believe I just had to put my method in just one World subclass.
Why not? You can create multiple subclasses of Actor and all of them can use any method within the Actor class. This is what is called inheritance. All non-private methods (and fields) in any class are a part of any of its subclasses (and subclasses of those subclasses, etc.).
You need to login to post a reply.