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

2013/3/29

moving and turning objects

3
4
5
6
7
8
9
danpost danpost

2013/3/30

#
I removed everything that dealt with the worm power-up from the Crab class and altered the following method in that class:
public void ifCanSeeWP()
{
    if (getOneObjectAtOffset(0, 0, WormPower.class) != null)
    {
        getWorld().removeObjects(getWorld().getObjects(WormPower.class));  
        Worm.startPowerUp();
    }
}[code]
Then, I re-wrote the Worm class to the following:
[code]import greenfoot.*;

public class Worm extends Animal
{
    private static int puTimer;

    public void act() 
    {
        if (!isPoweredUp()) return;
        Crab crab = (Crab)getWorld().getObjects(Crab.class).get(0);  
        if (crab == null) return;
        turnTowards(crab.getX(), crab.getY());  
        move(3);
        setRotation(0);
    }
    
    public static void startPowerUp()
    {
        puTimer = 200;
    }
    
    public static boolean isPoweredUp()
    {
        return puTimer > 0;
    }
    
    public static void checkPuTimer()
    {
        if (puTimer > 0) puTimer--;
    }
}
And added this act method to the world class:
public void act()
{
    Worm.checkPuTimer();
}
Worked beautifully.
Gingervitis Gingervitis

2013/3/30

#
The error I spoke of before was from a different power-up... I put the code in a previous post
danpost danpost

2013/3/30

#
Insert the following line at line 30 of your Dominator class:
if (getWorld().getObjects(Lobster.class).isEmpty()) return;
Gingervitis Gingervitis

2013/3/30

#
That still didn't solve the problem..... I definitely messed up the code for this....
danpost danpost

2013/3/30

#
Comment out the problem area and upload to pass again to me.
Gingervitis Gingervitis

2013/3/30

#
the error is the same as before and I'm not sure where exactly the error is... do you want the source code?
danpost danpost

2013/3/30

#
Copy/paste the error message in your post.
Gingervitis Gingervitis

2013/3/30

#
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.RangeCheck(ArrayList.java:547) at java.util.ArrayList.get(ArrayList.java:322) at Dominator.lobsterPowerUp(Dominator.java:30) at Dominator.act(Dominator.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
danpost danpost

2013/3/30

#
Post your Dominator class code again.
Gingervitis Gingervitis

2013/3/30

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

/**
 * Write a description of class Dominator here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Dominator extends Animal
{
    /**
     * Act - do whatever the Dominator wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int lobster = 0;
    public boolean lobsterPowerUp;
    public void act() 
    {
        move(5);
        lookForLobster();
        turnAtEdge();
        randomTurn();
        lobstersEaten();
        lobsterPowerUp();
    }  
    
    public void lobsterPowerUp()
    {
        Crab crab = (Crab) getWorld().getObjects(Crab.class).get(0);
        Lobster lobster = (Lobster) getWorld().getObjects(Lobster.class).get(0);
        if (getWorld().getObjects(Lobster.class).isEmpty()) return;
        if (crab != null && crab.dp2Powered())  
        {  
            turnTowards(lobster.getX(), lobster.getY());  
            move(3);  
            //if (getWorld().getObjects(Lobster.class).isEmpty()) return;
        }  
    }

    
    public void turnAtEdge()
    {
        if ( atWorldEdge())
        {
            turn(17);
        }
    }

    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) < 10)
        {
            turn( Greenfoot.getRandomNumber(45) );
        }  
    }

    public void lobstersEaten()
    {
        if (canSee(Lobster.class))
        {
            eat(Lobster.class);
        }

        if(getWorld().getObjects(Lobster.class).isEmpty())
        {
            if (canSee(Crab.class))
            {
                //eat(Crab.class);
            }
        }

    }

    /**
     * Check whether we have stumbled upon a lobster.
     * If we have, eat it. If not, do nothing. If we have
     * eaten x lobsters, we win.
     */
    public void lookForLobster()
    { 
        if ( canSee(Lobster.class))
        {
            eat(Lobster.class);
            Greenfoot.playSound("slurp.wav");
            lobster = lobster + 1;
        }

        if(getWorld().getObjects(Lobster.class).isEmpty())
        {
            if (canSee(Crab.class))
            {
                eat(Crab.class);
            }
        }

    }
}
danpost danpost

2013/3/30

#
You did not yet insert the line I said to insert a few posts ago.
Gingervitis Gingervitis

2013/3/30

#
I did didn't I? Line 31?
danpost danpost

2013/3/30

#
Oh, switch lines 30 and 31 around.
Gingervitis Gingervitis

2013/3/30

#
That made the power-up active when I hit play..... and the error message continues to come....
Gingervitis Gingervitis

2013/3/30

#
oh wait... I think I switched the wrong lines lemme try again
There are more replies on the next page.
3
4
5
6
7
8
9