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

2016/5/10

Unable to Call Actor Method From World.

soveda31 soveda31

2016/5/10

#
Hello, for a final project, I am making a "Frogger" clone. I basically have a fly on top of the map, and when you put the frog over the fly, the fly creates another fly at the bottom of the map. I created a Counter so that you would be able to know how many flies you ate. However in its current state, when the frog tries to eat the fly, it throws out an Exception. The Following is for the TopFly (it has the same code as the fly on the bottom):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class TopFly extends Actor
{
    int x;
    int y;
    int gen;
    
    public void act() 
    {
        collected();
    }    
    
    public void collected()
    {
        Actor fly = getOneObjectAtOffset(0, 0, Frog.class);
        if(fly != null) 
        {
            newFlyX();
            StageOne world = (StageOne) getWorld();
            world.score();
            getWorld().addObject(new BottomFly(), x , 110);
            getWorld().removeObject(this);
        }
    }
    
    public void newFlyX()
    {
        gen = Greenfoot.getRandomNumber(10);
        
        if (gen > 4)
        {
            x = 27 + (Greenfoot.getRandomNumber(2)*22);
        }
        
        else
        {
            x = 110 + (Greenfoot.getRandomNumber(2)*22);
        }
    }
}
The Following is for the World:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class StageOne extends World
{
    int key;
    int s;
    private Counter counter;
    public StageOne()
    {    
        
        super(160, 116, 5); 
        prepare();
        gameStart();
    }

    private void prepare()
    {
        Greenfoot.start();
    }
    
    private void gameStart()
    {
        addObject( new Frog(), getWidth() / 2, 110);
        addObject( new TopFly() , getWidth() / 2 , 5);
        addObject(new Counter(), getWidth() / 2 , getHeight() / 2);
    }
    
    public void score()
    {
        System.out.println("161");
        
        counter.point();
    }
The following is for the Counter:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class Counter extends Actor
{
    int count;
    
    public Counter()
    {
        count = 0;
        
        GreenfootImage theImage = new GreenfootImage( 80, 58 );
        
        setImage(theImage);
        
        updateImage();
    } 
    
    public void point()
    {
        System.out.println("162");
        count = count + 1;
    }
    
    public void updateImage()
    {
        GreenfootImage myImage = getImage();
        myImage.clear();
        myImage.setColor(Color.BLACK);
        String message = "Count: " + count;
        myImage.drawString( message, 10, 30);
    }
danpost danpost

2016/5/11

#
You never assign a Counter object to the Counter field declared on line 7 for your StageOne world object -- it should be the same one you add into the world at line 25.
soveda31 soveda31

2016/5/12

#
OH! Yeah I did what you said and it started to work! Thank you very much!
You need to login to post a reply.