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

2017/11/6

Another problem! Help!

twistedninja42 twistedninja42

2017/11/6

#
I've created an image for each sub-class of actor, for when they eat all of their prey. I'd like to display said image over top of the world once a team eats all of their prey (Crabs eat worms, worms eat lobsters, lobsters eat crabs), but it doesn't seem to work. Have I gone wrong somewhere?
 private void determineWinner()
    {
       if (player == 0 && amountOfCrabs == 0)
       {
      addObject (new GameOverLobsters(), 0,0);    
    }
    
danpost danpost

2017/11/6

#
Not enough code was supplied. How are the 'players' and 'amountOfCrabs' fields initialized and how are their values controlled?
twistedninja42 twistedninja42

2017/11/7

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.String;

/**
 * Write a description of class CrabWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CrabWorld extends World
{
    int amountOfCrabs;
    int player;
    int amountOfLobsters;
    int amountOfWorms;
    
    /**
     * Constructor for objects of class CrabWorld.
     * 
     */
    public CrabWorld()
    {    
        super(560, 560, 1); 
        prepare();
        determineWinner();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        amountOfCrabs = getObjects(Crab.class).size();
        player = getObjects(CrabPlayer.class).size();
        amountOfLobsters = getObjects(Lobster.class).size();
        amountOfWorms = getObjects(Worm.class).size();
        Crab crab = new Crab();
        addObject(crab,291,90);
        Crab crab2 = new Crab();
        addObject(crab2,90,274);
        Crab crab3 = new Crab();
        addObject(crab3,420,461);
        Lobster lobster = new Lobster();
        addObject(lobster,115,85);
        Lobster lobster2 = new Lobster();
        addObject(lobster2,302,255);
        Lobster lobster3 = new Lobster();
        addObject(lobster3,472,82);
        lobster.setLocation(95,456);
        Worm worm = new Worm();
        addObject(worm,91,80);
        Worm worm2 = new Worm();
        addObject(worm2,477,269);
        Worm worm3 = new Worm();
        addObject(worm3,292,443);
        crab3.setLocation(475,450);
        lobster2.setLocation(291,267);
        crab3.setLocation(463,452);
    }

    private void determineWinner()
    {
       if (player == 0 && amountOfCrabs == 0)
       {
        addObject (new GameOverLobsters(), 0,0);    
    }
    if (amountOfWorms == 0)
    {
        addObject(new GameOver(), 0,0);
    }
}
}
just added the whole of my code. It's not finished, but whenever the program is reset or the world is reset, A red square appears in the top left corner.
danpost danpost

2017/11/7

#
Remove line 2. You never have to import anything from java.lang. Remove lines 12, 14 and 15. The values for these should be changing throughout the game. Best to get the current value when needed. Remove lines 34, 36 and 37. The value set at the time these lines are executed will always be zero. Create and 'act' method for the class and move line 25 into it. Trying to determine a winner before the game starts does not make any sense. In the 'determineWinner' method, replace all variables beginning with 'amountOf...' with the appropriate 'getObjects/size' call removed from lines 34, 36 and 37.
You need to login to post a reply.