-1

Before I start, I'd like to mention that I'm new to programming and I'm really bad. If you can think of ANY reason as to what may be causing my issue, it's probably worth checking.

I'm making a game where the user controls a shark and has to move around, eating the fish and dodging the trash. The user gets a point whenever they eat a fish. I've been having an issue where the program says it can't find a method and I can't seem to figure it out.

Here is where the error is. It's in the Food class (the fish, what the shark eats):

Actor foodHit = getOneIntersectingObject(Shark.class);
  if(foodHit!=null)
  {
    World MyWorld = (World)MyWorld;
    Counter counter = World.getCounter(); 
    // The error is apparently in this line above, here's the error code: "cannot find symbol -  method getCounter()".
    counter.addScore();
    MyWorld.removeObject(this);
  }

This is the method in question, it is in the MyWorld class:

public Counter getCounter()
{
  return counter;
}

I can answer any questions and show more lines if you request. I know it's frustrating to deal with new people sometimes so I'm sorry in advance. Any help would be amazing, thanks for reading!

JanS
  • 2,065
  • 3
  • 27
  • 29
  • You are working in Java, not JavaScript btw :) – Jordan Soltman Dec 14 '18 at 18:36
  • Since you are new to programming, it's possible you are expecting the Food class to be able to reference the shark's world globally. What are you doing here: `World MyWorld = (World)MyWorld;` What type is MyWorld before you cast it to World? – geneSummons Dec 14 '18 at 20:10
  • I was following an instructional video where they typed that. I do not understand what that line is trying to do. – Josh Gould Dec 14 '18 at 20:17

2 Answers2

0

If you have a World base class and a MyWorld class that extends World and the getCounter() method is in the derived MyWorld class, then getCounter() will not be "visible" after you cast a MyWorld type object to a World type object. getCounter() only exists "in" MyWorld type objects.
(and casting in that direction isn't required anyway. Instances of MyWorld are also instances of World, no cast required. But instances of World may or may not also be instances of MyWorld, cast to MyWorld required in order to call methods defined in MyWorld)

Also, it's standard in Java to start variable names with lower case and class names with upper case. So a reader of your code would typically expect MyWorld to be a class name (an object type name) and myWorld to be a variable name (an object instance name).

I guess since you are copying code from a video you should do what they do so you can follow along with the video more easily. But if I were coding this, I would want to put an eatFood() method on the Shark class so that I could write

if (foodHit != null) {
    aShark.eatFood()
}

that way the world casting and counter updating code is encapsulated inside the Shark class rather than scattered around everywhere you check for food hits.

geneSummons
  • 907
  • 5
  • 15
-1

I believe if you change the code.

From

World MyWorld = (World)MyWorld;
Counter counter = World.getCounter(); 

To

World MyWorld = (World)MyWorld;
Counter counter = MyWorld.getCounter(); 

That should fix it. I had to post an answer since I don't have enough rep to comment. Let me know if that works.

JoshG
  • 39
  • 7
  • Unfortunately I'm getting the same error, as if the method is not in either of the 2 actors. Would it help if i sent the whole Greenfoot project to you? – Josh Gould Dec 14 '18 at 18:54
  • Yeah, that would make it easier for me to help you. – JoshG Dec 14 '18 at 22:29