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

2015/1/11

Actor changes color when mirrorHorizontally is called

Tokinator420 Tokinator420

2015/1/11

#
I am making a simple program to familiarize myself with GreenFoot where a person throws a toy a random direction for a random distance, and you control a dog and pick it up. One problem I found strange is that when I use the mirrorHorizontally() method the dog (which is represented by the wombat image that came with the program) changes from brown to orange permanently. I want to know why this is happening. Here is the code for the movement of the dog (I called the class Raider because that is the name of my dog):
public class Raider extends Actor
{
    
    private int raiderSpeed = 2;
    private boolean facingRight = true;
    
    public void act() 
    {  
        if( Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w" ) )
        {
            setLocation( getX(), getY() - raiderSpeed ) ;
        }
  
        if( Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("s") )
        {
            setLocation( getX(), getY() + raiderSpeed ) ;
        }
        
        if( Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a") )
        {
            if(facingRight)
            {
                GreenfootImage image = getImage();
                image.mirrorHorizontally();
            }
            
            facingRight = false;
           
            setLocation( getX() - raiderSpeed, getY() ) ;
        }
        
        if( Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d" ) )
        {
            if(!facingRight)
            {
                GreenfootImage image = getImage();
                image.mirrorHorizontally();
            }
            
            facingRight = true;
            
            setLocation( getX() + raiderSpeed, getY() ) ;
        }
    }    
}
danpost danpost

2015/1/11

#
That is a known issue and is believed to be a bug in JDK 7. As an alternative, you can use the following method which I created because of this issue:
public GreenfootImage getReverseImage(GreenfootImage img)
{
    int w = img.getWidth(), h = img.getHeight();
    GreenfootImage image = new GreenfootImage(w, h);
    for (int y=0; y<h; y++) for (int x=1; x<=w; x++)
        image.setColorAt(w-x, y, img.getColorAt(x-1, y));
    return image;
}
You need to login to post a reply.