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

2014/11/22

Need help with Color detection and Offsets.

Zwadouggie Zwadouggie

2014/11/22

#
I'm creating a maze game where the walls are 4 different colors that I choose when I make the object in the world. I also have a player class and I want it to work so that when the player is at an offset of 5 pixels to check what color of the wall it's close to and make it so that all of the walls in the maze of that color glow. What I have so far that isn't working: Navigator class public void act() { useOffsets(); examineKeysPressed(); } public void examineKeysPressed() { if ( Greenfoot.isKeyDown("left") && getOneObjectAtOffset( -5,0, Barrier.class ) == null ) { setLocation( getX() - 2, getY() ); } else if ( Greenfoot.isKeyDown("right") && getOneObjectAtOffset( 5,0, Barrier.class ) == null ) { setLocation( getX() + 2, getY() ); } if ( Greenfoot.isKeyDown("up") && getOneObjectAtOffset( 0,-5, Barrier.class ) == null ) { setLocation( getX() , getY() - 2 ); } else if ( Greenfoot.isKeyDown("down") && getOneObjectAtOffset( 0,5, Barrier.class ) == null ) { setLocation( getX() , getY() + 2 ); } } public void useOffsets() { Barrier barrier = (Barrier)getOneObjectAtOffset( -5,0, Barrier.class ); if ( barrier.getImage().getColor() == Color.RED ) { barrier.glow(); } /*if ( getOneObjectAtOffset( 5,0, Barrier.class ) != null ) { examineOffsets(); } if ( getOneObjectAtOffset( 0,-5, Barrier.class ) != null ) { examineOffsets(); } if ( getOneObjectAtOffset( 0,5, Barrier.class ) != null ) { examineOffsets(); }*/ } I'm getting a null pointer exception with the getColor() code. I think I need to use a list Any help please.
danpost danpost

2014/11/22

#
The GreenfootImage class, which is the type of object that 'getImage' returns, does not have a 'getColor' method. If you added a 'getColor' method to the Barrier class, then you could use just 'barrier.getColor(). If you still have difficulty with this class, post the code you are using in it (use the 'code' link below the reply box to insert codes into your posts) and explain the situation. You will need to use a list to set all of one color barrier visible; however, using the World class method 'getObjects' with the "for-each" type of 'for' loop, that should be fairly easy. Also, there may be a way to have the barriers set themselves (without using loops at all). That can be dealt with after you get the color issue resolved.
Zwadouggie Zwadouggie

2014/11/22

#
Before I used the "barrier.getColor()" method and the error is gone. So thank you. How does the declaring list work if you mind showing me an example.
danpost danpost

2014/11/22

#
Zwadouggie wrote...
How does the declaring list work if you mind showing me an example.
It would be much easier to have the barriers set their own transparency dependent on a static field in your World subclass. If you would prefer to use a list to set the transparencies of all walls of a specific color, it would be a two part process. The steps would include: * get and iterate through a list of all Wall objects in the world while iterating through the list * if wall is color and wall transparency is zero, set wall transparency to 255 * if wall is not color and wall transparency is not zero, set wall transparency to 0 You do not have to explicitly declare a list as you can just start iterating through the walls by starting with this:
for (Object obj : getWorld().getObjects(Wall.class))
{
    Wall wall = (Wall)obj;
    // perform checks on 'wall' here
}
The 'getWorld().getObjeccts(Wall.class)' creates the list and the 'for' loop iterates through the list, each time having the 'wall' variable reference the next Wall object in the list.
You need to login to post a reply.