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

2016/3/10

Method to other class?

Weetis Weetis

2016/3/10

#
I thought i got the thing right by searching on this forum i still don't get it... So i want to Have my method "setCharacter" to another class. Here is the code for it. Can someone help me please?
    public void setCharacter(String ch)
    {
    setImage(ch);
    }
    public void enterGame()
    {
        MouseInfo info = Greenfoot.getMouseInfo();
        SpelCharacter main = new SpelCharacter(); // there is something wrong in this line?
        if(info!=null) {
            int clicked = info.getButton();
            if(Greenfoot.mouseClicked(this) && clicked==1) {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new Spel());
                SpelCharacter.setCharacter("hedgehog.pjg"); // here i get an error too
            }
        }
    }  
    
}
danpost danpost

2016/3/10

#
There are two possible errors that you could be getting on line 10 -- it would have helped if you explained what error message you were getting there. The first is the image filename -- image filenames usually end with 'png' or 'jpg' ( 'pjg' is not a normal image filename suffix ). The other is that a non-static method cannot be called from a static context. This means you need an object of the class to call the method on (you cannot call the method directly on the class name). Finally, there does not appear to be anything wrong with line 4 -- again without you being more specific, one cannot help there. The only thing I see is that later on in the method, the reference variable 'main' is ignored (maybe you meant to call 'setCharacter' on it; but still, it will be lost as soon as the method completes as it is not placed into any world or saved anywhere (like in a field or array).
You need to login to post a reply.