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

2018/1/26

I made a two player game with in CrabWorld and...

Timmy_11 Timmy_11

2018/1/26

#
I made a two player game with in CrabWorld and put name tags on the two crabs to avoid confussion but when the enemy (a lobster ate a crab i got an error code from the name tag saying java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 and froze the game what do i do to fix this issue
Timmy_11 Timmy_11

2018/1/26

#
my name tag code is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class P1_NAME here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class P1_NAME extends Animal
{
    /**
     * Act - do whatever the P1_NAME wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Player1 player1 = (Player1)getWorld().getObjects(Player1.class).get(0);
        int x1 = player1.getX();
        int y1 = player1.getY() - 25;
        setLocation(x1, y1);
        setImage(new GreenfootImage("Player1", 12, Color.GREEN, Color.BLACK));

    }    
}
and the world code conserning it is public void act() { if (getObjects(Worm.class).size() <3) addWorms(); if (getObjects(Player1.class).isEmpty()) { if (Player1out == 0) { DudesAlive = DudesAlive - 1; Player1out = 1; removeObjects(getObjects(P1_NAME.class)); } } if (getObjects(Player2.class).isEmpty()) { if (Player2out == 0) { DudesAlive = DudesAlive - 1; Player2out = 1; removeObjects(getObjects(P2_NAME.class)); } } if (DudesAlive==0) { END(); } } and private void setUp() { int DudesAlive = 2; int randX = Greenfoot.getRandomNumber(560); int randY = Greenfoot.getRandomNumber(560); Player1 crab = new Player1(); addObject(crab, randX, randY); int randX2 = Greenfoot.getRandomNumber(560); int randY2 = Greenfoot.getRandomNumber(560); Player2 crab2 = new Player2(); addObject(crab2, randX2, randY2); int randX3 = Greenfoot.getRandomNumber(560); int randY3 = Greenfoot.getRandomNumber(560); Lobster lobster = new Lobster(); addObject(lobster, randX3, randY3); theCounter1 = new Score1(); addObject(theCounter1, 90, 20); theCounter2 = new Score2(); addObject(theCounter2, 270, 20); P1_NAME p1 = new P1_NAME(); addObject(p1, randX, randY-25); P2_NAME p2 = new P2_NAME(); addObject(p2, randX2, randY2-25); } that is all the world code that mensions it
Timmy_11 Timmy_11

2018/1/26

#
Never Mind I figured it out on my own :)
Timmy_11 Timmy_11

2018/1/26

#
the Name tag code goes like this
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class P1_NAME here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class P1_NAME extends Animal
{
    /**
     * Act - do whatever the P1_NAME wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(!getWorld().getObjects(Player1.class).isEmpty())
        {
            Player1 player1 = (Player1)getWorld().getObjects(Player1.class).get(0);
            int x1 = player1.getX();
            int y1 = player1.getY() - 25;
            setLocation(x1, y1);
            setImage(new GreenfootImage("Player1", 12, Color.GREEN, Color.BLACK));
        }
        else 
        {
            getWorld().removeObject(this);
        }
    }    
}
You need to login to post a reply.