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

2014/7/26

Need Help With MouseClicks and Setlocation

thatsnice1 thatsnice1

2014/7/26

#
Hello, I am trying to create a game where when a tile (Actor Dirt) is clicked, my main actor (Wizard) will move to the clicked tile. However, when I type the following code in my Dirt class, I get an error that says "non-static method setLocation(int,int) cannot be referenced from a static context" and I was looking for a possible way to get around this. Thanks!! if (Greenfoot.mouseClicked(this)) { HeroWizard.setLocation(this.getX(), this.getY()); }
erdelf erdelf

2014/7/26

#
why is the code u provided in a static method ? maybe u should think about that. I would suggest you place this in the act method i will assume that u only have one object of the HeroWizard class placed at a time
if (Greenfoot.mouseClicked(this)) 
{ 
        ((Actor)getWorld().getObjects(HeroWizard.class).get(0)).setLocation(this.getX(), this.getY()); 
}
danpost danpost

2014/7/26

#
erdelf wrote...
why is the code u provided in a static method ?
The error message does not suggest that the code provided was in a static method. It is saying that the call to 'setLocation' was written as if 'setLocation' was a static method when, in fact, it is not. The use of a class name (and not an object of that class), 'HeroWizard', as the object of the method call is what threw the exception. You can set the location of an instance of an Actor class; but, you cannot set the location of the Class Name or Type. Because you can create multiple instances of an Actor subclass, you must be able to specify which instance you want to set the location of (even if you only created one).
You need to login to post a reply.