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

2014/5/6

Suche Fehler!

Asoraso Asoraso

2014/5/6

#
private void runCounter() { counter++; if (counter == 500) getWorld().removeObject(this); } public void act() { if (getObjects(Roboter.class).isTouching()) spawnRoboter(); getWorld().removeObjects(getIntersectingObjects(null)); runCounter(); } private void spawnRoboter() { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject(new Roboter(), x, y); } Fehlermeldung: cannot find symbol - method getObjects (java.lang.Class<Roboter>)
AIMrOrange AIMrOrange

2014/5/6

#
you need to add "getWorld()." in front of "getObjects()" because it's a method of the world class.
danpost danpost

2014/5/6

#
How can every line in the code given be so right and this one line be so wrong:
if (getObjects(Roboter.class).isTouching()) spawnRoboter();
(1) everywhere in the code that a world method is used, it is prefixed with 'getWorld', (except this line) (2) everywhere in the code, the correct number and types of arguments are used in all method call ( except this line) (3) everywhere in the code, methods are called on objects of that methods class; (except this line) (1) and (3) above refer to similar type errors; however, it is not as evident with (1) because the object the method is executed on is 'hidden' -- when an object does not prefix a method call, it is by default whatever the keyword 'this' refers to. It is the object that prefixed the call for this method to execute. Since 'this' refers to an Actor object and 'getObjects' is a World class method, you cannot call the method on that object. With (3) you are creating a List class object (or at least trying to) with 'getObjects(Roboter.class)', but then trying to call an Actor class method on the List object ('isTouching' is an Actor class method). In short, a method is called like this:
instanceOfClass.methodOfClass();
where the classes MUST be the same. The location of the method may not, however, be in the same class; but instead, in a superclass of that class. If you do not give an object to execute the method on, like so:
methodOfClass();
The method is executed on the same object that the calling method is running for (or on). This call can be written this way:
this.methodOfClass();
to show the object that the method is running for (or on). If you just want to spawn a new Roboter object when 'this' (the instance of this class that the act method is currently running for) touches a Roboter object, then
if (isTouching(Roboter.class))
would be sufficient for the 'if' clause. Notice the argument in the 'isTouching' call. That is what (2) above referred to.
AIMrOrange AIMrOrange

2014/5/6

#
also your "addObject()" is also a method of World ;)
danpost danpost

2014/5/6

#
danpost wrote...
How can every line in the code given be so right and this one line be so wrong
Ok. I missed that one; so, not every line.
You need to login to post a reply.