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

2015/10/29

Question about getting an actor to traverse entire grid

dasav dasav

2015/10/29

#
I have a question about getting an actor to traverse the entire grid, going from one square to the next. Here is the code for my actor class. I can get it to traverse the grid until the second row when it goes up instead of down.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.util.List;
import java.util.ArrayList;

public class Pacman extends Actor
{
    public static int cellSize;

    private GreenfootImage pacmanRight;
    private GreenfootImage pacmanLeft;
    private GreenfootImage pacmanEat;
    private GreenfootImage image;

    private int foodEaten;
    private boolean stopped;

    public Pacman()
    {
        pacmanRight = PacmanWorld.loadImage("pacman1.gif");
        pacmanLeft = new GreenfootImage(pacmanRight);
        pacmanLeft.rotate(0);
        pacmanEat = PacmanWorld.loadImage("pacman3.gif");
        setImage(pacmanRight);

		cellSize = PacmanWorld.CELL_SIZE;
        foodEaten = 0;
    }

    public void act()
    {
        if(foundFood())
        {
            eatFood();
        }
        else if(!isAtEdge())
        {
            move(1);
        }
        else
        {
            turn(90);
            move(1);
            if(!isAtEdge()){
                turn(90);
            }
        }
    }

    public boolean isAtEdge()
    {
		int rotation;
		boolean result;

		rotation = getRotation();

		result = false;
		if (rotation == 0)
		{
			result = (getX() >= (getWorld().getWidth()-1));
		}
		else if (rotation == 90)
		{
			result = (getY() >= (getWorld().getHeight()-1));
		}
		else if (rotation == 180)
		{
			result = (getX() <= 0);
		}
		else if (rotation == 270)
		{
			result = (getY() <= 0);
		}
		return result;
	}

    public boolean foundFood()
    {
		Actor food;
		boolean result;

        food = getOneObjectAtOffset(0, 0, Food.class);
        if(food != null)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public void eatFood()
    {
		Actor food;

        food = getOneObjectAtOffset(0, 0, Food.class);
        if(food != null)
        {
            getWorld().removeObject(food);
            foodEaten++;
        }
    }

    public int getFoodEaten()
    {
        return foodEaten;
    }

    public boolean isStopped()
    {
        return stopped;
    }

    public GreenfootImage loadImage(String string)
	{
		GreenfootImage image;

		image = new GreenfootImage(string);
        if ((image.getWidth() > cellSize) || (image.getHeight() > cellSize))
        {
			image.scale(cellSize, cellSize);
		}
		return image;
	}
}
Any help is much appreciated. David
danpost danpost

2015/10/29

#
The 'else' code block starting at line 40 does not look right. Recheck your logic there.
dasav dasav

2015/10/29

#
@danpost thank you for the response I have been trying to tweak the code to get the proper turning once an edge is hit. Here is my revised code:
danpost danpost

2015/10/29

#
I do not think that you want to move (in the 'else' part) before checking to see if it can move first. That is part of what I was referring to.
dasav dasav

2015/10/29

#
danpost wrote...
I do not think that you want to move (in the 'else' part) before checking to see if it can move first. That is part of what I was referring to.
ok so it would be better if I made the movements outside of the else statement?
danpost danpost

2015/10/29

#
dasav wrote...
ok so it would be better if I made the movements outside of the else statement?
No. I think you should not move within the 'else' part before checking to see if it can. In other words, within the 'else' part, check to see if it can move before moving.
dasav dasav

2015/10/29

#
danpost wrote...
dasav wrote...
ok so it would be better if I made the movements outside of the else statement?
No. I think you should not move within the 'else' part before checking to see if it can. In other words, within the 'else' part, check to see if it can move before moving.
ok thanks that makes more sense. I will try to fix that and post my results. Thanks for all your help
You need to login to post a reply.