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

2014/1/9

Calling a method from another class.

HCWIZZ HCWIZZ

2014/1/9

#
Hello, I am making a tower defence game and have a score and health counter, these are just text with a variable, i have a mehod within the class that adds or subtracts the variable. I am trying to call this from another class but can't at the moment, Probably something really simple (am used to vb6 but completely new to java, a big change) Here is the Code: Score Counter:
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
public class ScoreCounter extends Actor
{
    private int score;
     
    public ScoreCounter()
    {
        score = 0;
        setImage(new GreenfootImage(200, 30));
        update();
    }
 
    public void addScore()
    {
        score++;
        update();
    }
     
    public void update()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.WHITE);
        img.drawString("Score: " + score, 4, 20);
    }
}
And I am trying to call minusHealth() in the health() method in the Enemy class;
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
public class Enemy extends Actor
{
    private int enemyDelayCount;
    private Weapon weapon;
     
    public World myWorld;
     
    public void act()
    {
        followPath();
        //if the enemy gets to the end, minus a life and remove the enemy.
        if (getX() >= getWorld().getWidth() - 10)
        {
            removeEnemy();
            health();
        }
    }   
     
    private void followPath()
    {
         /**
         * making the enemy follow the correct path
         */
        if (getX() < 93 )
        {  
            setRotation(50);
            move(1);
        }
        if (getX() >= 93 && getX() < 284)
        {
            setRotation(0);
            move (1);
        }
        if (getX() >= 284 && getX() < 368)
        {
            setRotation (325);
            move (1);
        }
        if (getX() >= 368 && getX() < 595)
        {
            setRotation (405);
            move(1);
        }
    }
     
    private void removeEnemy()
    {
        World world;
        world = getWorld();       
        world.removeObject(this);
    }
     
    public void health()
    {
         
    }
}
Thank you for any help!!!! :)
erdelf erdelf

2014/1/9

#
well, u need a reference to the counter if u dont have more then one counter, this should work
1
((ScoreCounter)getWorld().getObjects(ScoreCounter.class).get(0)).addScore();
HCWIZZ HCWIZZ

2014/1/9

#
One thing I forgot to mention, In my world class I have the code:
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
public class MyWorld extends World
{
 
    private int counter;
    private int enemySpaceCounter;
    private int enemyX;
    
    //change this to reference the range in the tower class.
    private int range = 150;
    private int radius = range/2;
    private int halfRadius = range/4;
    private int enemySpace = 100;
 
    public HealthCounter healthcounter = new HealthCounter();
    public ScoreCounter scorecounter = new ScoreCounter();
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
 
        //All text displayed on the screen
        addObject (new Instructions(), 460, 9);
        addObject (new Version() ,535, 25);
        addObject (scorecounter, 483, 33);
        addObject (healthcounter, 483, 57);
 
        //Prepare the world
        prepare();
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Enemy enemy = new Enemy();
        addObject(enemy, 11, 156);
    }
 
    /**
     * check for mouse clicks
     */
    public void act()
    {
        addTower();
        newEnemy();
    }
 
    public void addTower()
    {
        if(Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            Tower tower = new Tower();
            addObject(tower, mouse.getX(), mouse.getY());
 
            GreenfootImage img = getBackground();
            img.setColor(Color.WHITE); 
            img.drawOval(tower.getX() - halfRadius,tower.getY() - halfRadius,radius,radius); //just draw an outline
        }
    }
 
    public void newEnemy()
    {       
        if (enemySpaceCounter == enemySpace)
        {
            Enemy addEnemy;
            addEnemy = new Enemy();
            addObject(addEnemy, 11, 156);
            enemySpaceCounter = 0;
        }
        else if(enemySpaceCounter < enemySpace)
        {
            enemySpaceCounter ++;
        }
    }
 
    public void health()
    {
        healthcounter.minusHealth();
    }
}
And I can call the method in the Act() part with healthcounter.minusHealth(); , but I cant call it in the other classes, What do I need to put to be able to call it? Thank you
HCWIZZ HCWIZZ

2014/1/9

#
I have added that line of code and am getting the same problem I was getting before, it just stops the game, pressing play will run it till the next time it is used and the health still doesn't decrement.
erdelf erdelf

2014/1/9

#
oh.. i think u should create the counters in the constructor
JasonZhu JasonZhu

2014/1/9

#
When you create the counter, you should make it referencable. That is similar to the below code:
1
2
3
4
5
6
7
public Counter hpCounter;   
 
public void addHpCounter()
    {
        hpCounter = new Counter(this,2,"");
        getWorld().addObject(hpCounter,0,0);
    }
That way, you could refer to it inside the class you wish. I wrote this method within the class I wanted to use it from. Then I could write:
1
hpCounter.methodName();
To access a method within my Counter class.
JasonZhu JasonZhu

2014/1/9

#
If you don't understand this, I could clarify because I'm not too sure what you are asking.
HCWIZZ HCWIZZ

2014/1/9

#
I have tried what you have suggested and am struggling to implement it. I will try to re-phrase what I want to do: in the Enemy class there is "public void Health" when this is called, an enemy has got to the end of the world. I would like to call and use "addScore()", lines 12 to 16 of the first lot of code. This will add one too the score. How do I call "addScore()" from the Enemy class when "addScore()" is in the ScoreCounter class. if I try at the moment, it will work till it needs to be executed then stop the program, pressing play will cause act once then stop again. Hope this helps to clarify what I am trying to do,
danpost danpost

2014/1/10

#
It appears you already had them referenced in the MyWorld class. From an actor object that is in the world, you should be able to use this:
1
((MyWorld)getWorld()).healthCounter.minusHealth();
HCWIZZ HCWIZZ

2014/1/10

#
Got It Working! Hello, Managed to get it sorted, the code that you said worked perfectly, I was getting a null pointer exception error error, this was due to me calling health() after removeEnemy() in lines 14 and 15 of the second lot of code. Thank you for all your help! :)
You need to login to post a reply.