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

2011/12/23

Accessing an inherited World object.

iiRainzZ iiRainzZ

2011/12/23

#
How would you access an inherited object of the World class without making a new one? I have a method in one of the Actor objects that wants to call a method in one of the World objects, but I do not know how to supply the method with a World object. As you can see in the checkSplit() method, I have created a new console which I dont want to do. I tried using the getWorld(), but that returns the World and not the inhereted class, Console. Method in the Console class which inherits World: public void addPlayer(int numberPlayers, double xPos, double yPos){ if (playerPop < 1001){ for (int i = 0; i < numberPlayers ; i++){ player = new Player(box,map,hud,xPos, yPos); addObject(player,(int)xPos, (int)yPos); playerPop ++; } } } Method in the Player class that inherits Actor: public void checkSplit(){ Console w = new Console(); if (getSelected()){ //Take away 5 orbs if(hud.getSplits() == 1 && getSize() > 5){ setSize(getSize() -5); hud.setSplits(false,false,false); w.addPlayer(5,getExactX(), getExactY()); //Take away 10 orbs }else if (hud.getSplits() == 2 && getSize() >10){ setSize(getSize() -10); hud.setSplits(false,false,false); w.addPlayer(10,getExactX(), getExactY()); //Take away whole orb }else if (hud.getSplits() == 3 ){ w.addPlayer(getSize(),getExactX(), getExactY()); setSize(0); hud.setSplits(false,false,false); } } }
Duta Duta

2011/12/23

#
Console w = (Console) getWorld();
iiRainzZ iiRainzZ

2011/12/24

#
Haha, I actually thought of it after a few hours of hard thinking. Thanks though!
You need to login to post a reply.