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

2014/12/3

need get a variable from one class to another heres what i got

Kickero Kickero

2014/12/3

#
the code I've given is only a fraction of everything else so just say something if you need more
SirB

private int SBHealth = 3;

public void HitSirB()
    {
        SBHealth --;
    }
Enemy

  public void SirBAlert()
    {
        if (canSee(SirB.class))
        {
            checkSirB();
        }
    }

    public void checkSirB()
    {
        if (SirB.IsAttacking = false)
        {
            Health -- ;
            Greenfoot.playSound("Hurt.wav");
        }
        else
        {
            SirB.HitSirB();
            Greenfoot.playSound("Hurt.wav");
            
        } 

    }
Kickero Kickero

2014/12/3

#
sorry that was really minimal i need the enemy to be able to access HitSirB(); and then affect the variable SBHealth, but it cant because it can't reference HitSirB because its from "static content" ... after trying to fix this I kinda understand what it means, but now i just need help i tryed to use this tutorial http://www.greenfoot.org/doc/howto-1 but didn't really help ...
danpost danpost

2014/12/3

#
What the tutorial tries to explain is that you cannot call an object method on a class (or, you cannot use the class name to call a method that requires an object of that class). For example, if you had a Bicycle class and created two bicycles from it then tried to call 'applyBrake' on the class name ... well, do you see the dilemma? The class does not have brakes to apply; and, should the method then just choose one of the bicycles to apply the brakes on? No. The method must know what bicycle it is to apply the brakes on. Somewhere in your code you used 'new SirB()' to create an object of that class. If you saved it in a field a added a 'get' method to return it, you would be halfway there. If not, and you only have one object of that class in the world, you can use method calls to get a reference to that object:
SirB sirb = (SirB)getWorld()'getObjects(SirB.class).get(0);
Then you can run the methods on 'sirb'.
Kickero Kickero

2014/12/5

#
Thanks!
You need to login to post a reply.