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

2021/10/31

Help with code

Gabe1098 Gabe1098

2021/10/31

#
Help... I'm trying to make an ant mania like game but when the spiders move they move all at once and you can't even see it moving This is in the myWorld class where it adds the spider
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    int spiderX;
    int spiderY;
    /**
     * Constructor for objects of class MyWorld.
     * 
      */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        Ant ant = new Ant();
        addObject(ant,getWidth()/2,getHeight()/2);
        Food food = new Food();
        addObject(food,Greenfoot.getRandomNumber(600),Greenfoot.getRandomNumber(400));
        
    }
    public void addSpider() {
        if (Greenfoot.getRandomNumber(2) == 1) {
            spiderX = 0;
            spiderY = Greenfoot.getRandomNumber(400);
        }
        else {
            spiderX = Greenfoot.getRandomNumber(600);
            spiderY = 0;
        }
        Spider spider = new Spider();
        addObject(new Spider(),spiderX,spiderY);
    }
}
and here is the spider code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Spider here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spider extends Actor
{
    /**
     * Act - do whatever the Spider wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (getX() == 0) {
                 setRotation(0);
            for (int i = 0; i < 600; i++) {
                move(1);
            }
            setRotation(180);
            for (int i = 0; i < 600; i++) {
                move(1);
            }
        }
        else {
            if (getY() == 0) {
                setRotation(-90);
            for (int i = 0; i < 400; i++) {
                move(1);
            }
            setRotation(90);
            for (int i = 0; i < 400; i++) {
                move(1);
            }
            }
        }
    }
}
danpost danpost

2021/11/1

#
The name of the method is act -- not acts. It is called repeatedly by greenfoot while the actor is in the active world. You program it for what must be done each frame -- not what it should do in total. Remove the for loops (the repeated calling does the stepping). Ask what the spider should do at each instance of time:
if (getX() == 0 || getX() == getWorld().getWidth()-1) turn(180);
if (getY() == 0 || getY() == getWorld().getHeight()-1) turn(180);
move(1);
Gabe1098 Gabe1098

2021/11/1

#
danpost wrote...
The name of the method is act -- not acts. It is called repeatedly by greenfoot while the actor is in the active world. You program it for what must be done each frame -- not what it should do in total. Remove the for loops (the repeated calling does the stepping). Ask what the spider should do at each instance of time:
if (getX() == 0 || getX() == getWorld().getWidth()-1) turn(180);
if (getY() == 0 || getY() == getWorld().getHeight()-1) turn(180);
move(1);
Thank You!!! It worked!!!
Gabe1098 Gabe1098

2021/11/1

#
danpost wrote...
The name of the method is act -- not acts. It is called repeatedly by greenfoot while the actor is in the active world. You program it for what must be done each frame -- not what it should do in total. Remove the for loops (the repeated calling does the stepping). Ask what the spider should do at each instance of time:
if (getX() == 0 || getX() == getWorld().getWidth()-1) turn(180);
if (getY() == 0 || getY() == getWorld().getHeight()-1) turn(180);
move(1);
Also I had a bug, the ones on the top wouldn't work. But I fixed it. Instead of using
if (getY() == 0 || getY() == getWorld().getHeight()-1) turn(180);
I did
if (getY() == 0 || getY() == getWorld().getHeight()-1) turn(90);
danpost danpost

2021/11/2

#
Gabe1098 wrote...
I had a bug, the ones on the top wouldn't work.
I would have thought the rotation was previously set. This could be done with the following:
protected void addedToWorld(World world)
{
    if (getY() == 0 || getY() == world.getHeight()-1) turn(90);
}
This should initialize those actors placed along the top of the world to move vertically (using the code I previously gave above). Note that the rotation could otherwise be set from the world:
Actor mover = new << Actor class name here >>();
if (Greenfoot.getRandomNumber(2) == 0)
{
    addObject(mover, 0, Greenfoot.getRandomNumber(getHeight());
}
else
{
    addObject(mover, Greenfoot.getRandomNumber(getWidth(), 0);
    mover.turn(90); // adjusting rotation here to move vertically
}
You need to login to post a reply.