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

2018/12/14

Unable to find Method?

1
2
Legitimacy Legitimacy

2018/12/14

#
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. its 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(); //error is here, "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;
    }
Any help would be amazing, thanks for reading!
danpost danpost

2018/12/14

#
Legitimacy wrote...
this is the method in question, it is in the MyWorld class
That is one problem -- it is not in the World class and the world referenced is typed as a World object (see line 4). If typed as a MyWorld object, the method will be found. The other thing is that you are calling the method, which is not a class method, on a class (the World class) and not on the world created from the class, which you referenced as MyWorld. And line 4, I just noticed, is a problem also, in that MyWorld is a class and should not be able to be typed as a World object and set to a World type variable.
Legitimacy Legitimacy

2018/12/14

#
Hi, I want to get back to editting my code but I'm having a new issue - the buttons to Act, Run and Reset my program are greyed out and it tells me to Instantiate a new World object. Any ideas?
CoolSharkCody CoolSharkCody

2018/12/14

#
Is there any errors in MyWorld?
Legitimacy Legitimacy

2018/12/14

#
No errors showing up. here is the world code.
import greenfoot.*;
public class Ocean extends World
{
    Counter counter = new Counter();
    public Ocean()
    {    
        super(854, 480, 1); 
        prepare();
    }
    
    public void act()
    {
        if(Greenfoot.getRandomNumber(1000)<5)
            addTrash();
        if(Greenfoot.getRandomNumber(1000)<3)
            addFood();
    }

    public Counter getCounter()
    {
        return counter;
    }
    
    private void prepare()
    {
        Shark shark = new Shark();
        addObject(shark,getWidth()/2,getHeight()/4*3);
        addObject(counter,46,436);
    }

    private void addTrash()
    {
        if (getObjects(Trash.class).size() < 5)
        {
            Trash trash = new Trash();
            addObject(new Trash(),Greenfoot.getRandomNumber(getWidth()),0);
        }
    }
    
    private void addFood()
    {
        if (getObjects(Food.class).size() < 3)
        {
            Food food = new Food();
            addObject(new Food(),Greenfoot.getRandomNumber(getWidth()),0);
        }
    }
}
CoolSharkCody CoolSharkCody

2018/12/14

#
If that’s the case I would restart Greenfoot.
danpost danpost

2018/12/14

#
Legitimacy wrote...
No errors showing up. here is the world code. << Code Omitted >>
I did not see anything wrong going through that class. Show your other classes.
Legitimacy Legitimacy

2018/12/14

#
I restarted my PC, same issue. this is something I have run into before, last time it happened I just used a slightly older version of my project which still worked.
CoolSharkCody CoolSharkCody

2018/12/14

#
Are you currently using a old version of Greenfoot?
Legitimacy Legitimacy

2018/12/14

#
Counter Class:
import greenfoot.*;
public class Counter extends Actor
{
    int score = 0;
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + score, 24, Color.GREEN, Color.BLACK));
    }    
    
    public void addScore()
    {
        score++;
    }
}
Food Class:
import greenfoot.*;
public class Food extends Actor
{
    public void act() 
    {
        setLocation(getX(),getY()+1);
        checkCollision();
    }    
    
    void checkCollision()
    {
        Actor foodHit = getOneIntersectingObject(Shark.class);
        if(foodHit!=null)
        {
            World myWorld = getWorld();
            Ocean ocean = (Ocean)myWorld;
            Counter counter = ocean.getCounter(); 
            counter.addScore();
            myWorld.removeObject(this);
        }
        
        else
        {
            if(getY()==479)
            {
                getWorld().removeObject(this);
            }
        }
    }
}
Shark Class:
import greenfoot.*;
public class Shark extends Actor
{
    /**
     * Act - do whatever the Shark wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int lives = 3;
        sharkMovement();
    }    

    private void sharkMovement ()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+3,getY());
            if (Greenfoot.isKeyDown("up"))
            {
                setRotation(45);
            }
            else if (Greenfoot.isKeyDown("down"))
            {
                setRotation(135);
            }
            else
            {
                setRotation(90);
            }
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-3,getY());
            if (Greenfoot.isKeyDown("up"))
            {
                setRotation(315);
            }
            else if (Greenfoot.isKeyDown("down"))
            {
                setRotation(225);
            }
            else
            {
                setRotation(270);
            }
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(),getY()-2);
            if (Greenfoot.isKeyDown("left"))
            {
                setRotation(315);
            }
            else if (Greenfoot.isKeyDown("right"))
            {
                setRotation(45);
            }
            else
            {
                setRotation(0);
            }
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(),getY()+4);
            if (Greenfoot.isKeyDown("left"))
            {
                setRotation(225);
            }
            else if (Greenfoot.isKeyDown("right"))
            {
                setRotation(135);
            }
            else
            {
                setRotation(180);
            }
        }
    }
}
Trash Class:
import greenfoot.*;
 class Trash extends Actor
{
    public void act() 
    {
        int lives = 3;
        trashMovement();
        checkCollision(lives);
    }   
    
    public void trashMovement()
    {
        setLocation(getX(),getY()+2);
    }
    
    void checkCollision(int lives)
    {
        Actor trashHit = getOneIntersectingObject(Shark.class);
        if(trashHit!=null)
        {
            lives--;
            getWorld().removeObject(this);
        }
        
        else
        {
            if(getY()==479)
            {
                getWorld().removeObject(this);
            }
        }
    }
}
Legitimacy Legitimacy

2018/12/14

#
Currently using Greenfoot Version 3.5.0, java version 1.8.0_172. should be recent since I installed it less than 2 months ago
CoolSharkCody CoolSharkCody

2018/12/14

#
I would downgrade if it already worked for you earlier
Legitimacy Legitimacy

2018/12/14

#
Keep in mind there will be a lot of issues with the code that I posted but I only need it to run right now. there are unused variables (such as lives) but as long as it runs, i can fix it
danpost danpost

2018/12/14

#
I fail to spot anything amiss in any of the classes. I copied all classes, as given, into a new project and everything seemed to work fine (compiled and ran with no issues). Try creating a new project and copy over the classes.
Legitimacy Legitimacy

2018/12/14

#
I'm currently re-installing greenfoot. you say everything worked, did my score counter work? as in, did the score go up by one when the shark ate a fish?
There are more replies on the next page.
1
2