I want to create a mirror of the image

Hi, I want to create a mirror image effect: when the character is moving left it face left, when is moving right it face right.

The character can move also up and down, but I want only this kind of effect: left and right.
So, if my character have face left and it move up/down, it continues with face left
if the character hava face right and it move up/down, it continues with face right

this is the code:


public void draw(Graphics2D g) {
         AffineTransform transform = new AffineTransform();        

            // translate the sprite
            transform.setToTranslation(x,y);

            // if the sprite is moving left, flip the image
            if (dir == dir.WEST) {
                transform.scale(-1, 1);
                transform.translate(-sprite.getWidth(), 0);
            }             
          g.drawImage(sprite.image,transform,null);           
	}

The problem is: 'cause my default image have face right, if it move left, it change to face left, but if it move up/down/right, it change to face right.

for example: character is moving left, it change to face left… then, character is moving up/down, it change to face right.

How I can resolve this problem??

Fix the logic in your game? ;D

tbh unless you plan on posting all your games code (please don’t!), I can’t see that we can give you much help.

You appear to understand (or have found some reference materal that explains) how to use AffineTransform.
You simply need to adapt the rendering code so it works with your game logic.

the problem isn’t AffineTransform!

if my character moving left, then it face left correctly!!

I’ve a problem when my character moving up or down, it always has “right face” 'cause my default image have “right face”.
What I want is:
if my character move up or down, it keep the last “direction face” (if it was left, then left face … if it was right, then right face)

I hope you understand me (english isn’t my mother language :P)

why don’t you use multiple images - as far as I know AffineTransform isn’t hardware accalerated every time

Two images ghostleft.png and ghostright.png ??

Yes, why not?

hell… ghost.png is all you need. you can mirror it at runtime and keep the result in memory :wink:

oh, this silly 4K logic!

I did that once… I used g.drawImage(200,200,-100,-100); :smiley:

damn, that would’ve saved me some bytes in Goomba4K

I was doing…


g.scale(-1,1);
g.translate(-sprite_width,0);
g.drawImage(...);

just keep the last direction as property in your character object and only change it when left or right is pressed.