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

2019/6/17

call another class's method

1
2
Fargesia Fargesia

2019/6/17

#
Hi, it's a pretty dumb question I guess, but how do I call a method from another class?
public void bouger()
    {
        if(etat==1){
            posX=plateau.centrer(mouse.getX());
            posY=plateau.centrer(mouse.getY());
            
            setLocation(posX, posY);
            
            getWorld().removeObjects(getWorld().getObjects(carreVert.class));
            
            if(isTouching(pionNoir.class)||isTouching(chevalNoir.class)||isTouching(roiNoir.class)||isTouching(tourNoir.class)||isTouching(fouNoir.class)||isTouching(reineNoir.class)){
                removeTouching(pionNoir.class);
                removeTouching(chevalNoir.class);
                removeTouching(tourNoir.class);
                removeTouching(roiNoir.class);
                removeTouching(reineNoir.class);
                removeTouching(fouNoir.class);
            }
            etat=0;
            plateau.changeTour();
            plateau.changerJoue();
        }
        
    }
I got this method and I want to call it right here:
if(Greenfoot.mouseClicked(this)){
            getWorld().removeObjects(getWorld().getObjects(carreVert.class));
            chevalBlanc.bouger();
        }
But as it is in the act method, I can't because it's a non-static method. And if I understood it right, I can't use it because I call on a whole class instead of an actor, but how do I call this method just for one actor? Thanks guys!
danpost danpost

2019/6/17

#
Provided the actor clicked on is not a carreVert object and exactly one chavelBlanc object is in the world, you can use:
((chevalBlanc)getWorld().getObjects(chevalBlanc.class).get(0)).bouger();
Fargesia Fargesia

2019/6/17

#
Well actually I click on the carreVert object and this should call the bouger method from the chevalBlanc class. And there's two of them (chevalBlanc class): one called chevalB1 and the other chevalB2
danpost danpost

2019/6/17

#
Fargesia wrote...
Well actually I click on the carreVert object and this should call the bouger method from the chevalBlanc class. And there's two of them (chevalBlanc class): one called chevalB1 and the other chevalB2
How called? -- by class names or field names in some class? And are you wanting to call the method on a specific one (and which)?
Fargesia Fargesia

2019/6/17

#
Like I said the answer's probably simple, but like if I click on a carreVert object, then the chevalB1 object do the bouger method.
danpost danpost

2019/6/17

#
danpost wrote...
How called? -- by class names or field names in some class?
Fargesia Fargesia

2019/6/17

#
Sorry I'm a beginner I don't really get what you mean by that...
danpost danpost

2019/6/17

#
Fargesia wrote...
Sorry I'm a beginner I don't really get what you mean by that...
Are chevalB1 and chevalB2 class names? are they subclasses of chevalBlanc?
Fargesia Fargesia

2019/6/17

#
No chevalB1 and chevalB2 are just objects of the chevalBlanc class
danpost danpost

2019/6/17

#
Fargesia wrote...
No chevalB1 and chevalB2 are just objects of the chevalBlanc class
How can you distinguish between the two? Is there anything different about them that will tell you which is chevalB1 and which is chevalB2? or does it even matter?
Fargesia Fargesia

2019/6/17

#
No just the name, I just precised there were two of them but it doesn't really matter. So let's just forget the chevalB2 and say that I want chevalB1 to do the bouger method when I click on a carreVert
danpost danpost

2019/6/17

#
Fargesia wrote...
No just the name, I just precised there were two of them but it doesn't really matter. So let's just forget the chevalB2 and say that I want chevalB1 to do the bouger method when I click on a carreVert
The line I provided above will call the method on one of the two objects. If it does not matter which one, then it should suffice as is.
Fargesia Fargesia

2019/6/17

#
I tried using it but I get this error: java.lang.NullPointerException at chevalBlanc.act(chevalBlanc.java:29)
danpost danpost

2019/6/17

#
Fargesia wrote...
I tried using it but I get this error: java.lang.NullPointerException at chevalBlanc.act(chevalBlanc.java:29)
Switch the line with the line above it. The carreVert object needs to be in the world for the line to execute properly.
Fargesia Fargesia

2019/6/17

#
Already thought so and I did put it above the delete command (after posting it, should have corrected it before posting)
There are more replies on the next page.
1
2