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

2014/11/12

Simulation just pauses

KevinWelch2001 KevinWelch2001

2014/11/12

#
I'm trying to make a ant simulation. I have some code that add's leaves for the ants too eat. but it just pauses the program. How can I add leaves were the mouse is? Here is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ant_1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ant_1 extends Actor
{
    /**
     * Act - do whatever the Ant_1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(3);
        int Food = 100;
        World world;
        world = getWorld();
        Actor ant_food;
        ant_food = getOneObjectAtOffset(0, 0, ant_food.class);
        int mouseX = getX();
        int mouseY = getY();
        if (Greenfoot.getRandomNumber(100) < 10)
        {
            turn(Greenfoot.getRandomNumber(90) - 45);
        }
        if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
        {
            turn(180);
        }
        if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
        {
            turn(180);
        }
        if (Greenfoot.mouseClicked(world))
        {
            world.addObject(ant_food, mouseX, mouseY );
        }
        
        
        if ( ant_food != null)
        {
            
            world.removeObject(ant_food);
            Food ++;
        }
    }    
}
danpost danpost

2014/11/13

#
'ant_food', which is assigned a value at line 22, could be 'null'. So, when you click on the world and line 39 is executed, you get an error because you cannot add 'null' into a world. You need to create a 'new' ant_food object to add into the world. Also, 'mouseX' and 'mouseY' are set to the location coordinates of the Ant_1 object (lines 23 and 24), not to the coordinates of where the mouse is. To get the mouse coordinates, you need to get a MouseInfo object that is not null and use 'getX' and 'getY' on it. Finally, each act cycle you declare a variable called 'Food' which is initialized to 100 (not sure why 100). This variable is lost at the end of each cycle because its scope is limited to the method. You need to move line 18 to outside the act method for its scope to be limited to the life of the Ant_1 actor instead.
KevinWelch2001 KevinWelch2001

2014/11/13

#
Thanks a lot, I just cant get the mouse info thing to work, other then that it works.
KevinWelch2001 KevinWelch2001

2014/11/13

#
if you can, can you give me code for the mouse function
danpost danpost

2014/11/13

#
The greenfoot API shows the MouseInfo class (and how to acquire a MouseInfo object). It also contains all the methods you can use on a MouseInfo object. Please review the documentation.
You need to login to post a reply.