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

2016/5/23

Actor out of bounds

benbec1 benbec1

2016/5/23

#
This is the code for my actor in my game. whenever I try to move is says the actor is out of bounds. for example: Y = 6 should have been less than 5 any help would be appreciated thanks
import greenfoot.*;
import java.awt.Color;

/**
 * Write a description of class Mazer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mazer extends Actor
{
    /**
     * Act - do whatever the Mazer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        World world = getWorld();
        Color color = Color.WHITE;
        GreenfootImage background = world.getBackground();
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
            if (background.getColorAt(getX() - 7, getY()).equals(Color.WHITE))
            {
                move(3);
            }
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            if (background.getColorAt(getX(), getY() + 7).equals(Color.WHITE))
            {
                move(3);
            }
        } 
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            if (background.getColorAt(getX() + 7, getY()).equals(Color.WHITE))
            {
                move(3);
            }
        }  
        if (Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            if (background.getColorAt(getX(), getY() - 7).equals(Color.WHITE))
            {
                move(3);
            }

        }  
        

    }
}
danpost danpost

2016/5/23

#
It is not saying the actor is out of bounds, it is saying that the place you are checking for a color on the image of the background does not exist -- you are attempting to locate a color off the image itself. You can prevent out of bounds here by adding a condition to each inner 'if' statement to ensure that the location checked exists on the image. For example, line 48 could be:
if (getY()-7 >= 0 && background.getColorAt(getX(), getY()-7).equals(color))
The local variable 'color' was set to 'Color.WHITE' at line 19, but not used anywhere. Either change all future references to 'Color.WHITE' to 'color' beyond line 19 or remove line 19.
benbec1 benbec1

2016/5/23

#
Thank you
You need to login to post a reply.