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

2018/2/27

I need help calling methods from another subclass

erchainis erchainis

2018/2/27

#
Hey, I'm having troubles trying to call some of the methods in "Scoreboard subclass" to my "Person subclass". I manage to do it passing a parameter in the constructor of "Person (<parameter>)" and a couple of other things, but I need to call those methods without having a parameter in the constructor (Person). So, What I'm going after is having no parameter in "Public Person()" but to use the methods inside the subclass Scoreboard.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MyWorld extends World
{
private Scoreboard tab;
 
public MyWorld()
{   
     
    // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
    super(800, 600, 1);
     
     
    tab = new Scoreboard(); // creates the Scoreboard
    addObject(tab, 399, 10);           // add the Scoreboard to the World
     
    Person player = new Person(tab) ; // creates a new player
    addObject(player, 2,580);     // add the player to the World
     
    Spider Enemy = new Spider(); // create a new Spider object
    addObject(Enemy,Greenfoot.getRandomNumber(getWidth()),54);  // add the object to the world
     
    
     
}
----------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Person extends Actor
{
private Scoreboard tablero;
 
public Person (Scoreboard tab)
 {
   
  tablero = tab;
   
  keyBoard = "space";
   
  if (Greenfoot.isKeyDown("space"))
  {
   getWorld().addObject(new Trap(), locatX, locatY);
    
  }
   
     
 }
Vercility Vercility

2018/2/27

#
and why exactly cant you just let the parameter in the constructor lol? Try using a static scoreboard in your world class and calling it by using getWorld().getScoreboard()
1
2
3
4
5
private static Scoreboard tab = new Scoreboard();
 
public Scoreboard getScoreboard() {
return tab;
}
Im not sure if this will work tho
danpost danpost

2018/2/28

#
Line 10 in the Person class does not make any sense (remove it). Also, lines 12 through 16 should be in the act method -- not in the constructor.
You need to login to post a reply.