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

2018/3/27

Help with flipping an image

emystery emystery

2018/3/27

#
So, im having a problem with my code. I have it so that the actor by default faces right, but i need it o face left when i press "left", but it keeps looking side to side
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void move()
    {
        int y = getY();
        int x = getX();
         
        if(Greenfoot.isKeyDown("Left") && canMoveLeft())
        x-=STEP;
        turn (180);
        getImage().mirrorVertically();
         
        if(Greenfoot.isKeyDown("Right")&& canMoveRight())
        x+=STEP;
         
        setLocation(x, y);
    }
danpost danpost

2018/3/27

#
Use setRotation instead of turn. Also, you will need to set the rotation in the "Right" key detected code also (line 13). As a side note, I do not believe that "Right" and "Left" should be capitalized.
emystery emystery

2018/3/27

#
Ok, thank you Lord Danpost. But now the actor stays upside down and only looking left, after looking left for the first time. What could i have done wrong?
emystery emystery

2018/3/27

#
nevermind i ended up fixing it
1
2
3
4
5
6
7
8
9
10
11
12
13
public void move()
    {
        int y = getY();
        int x = getX();
         
        if(Greenfoot.isKeyDown("left") && canMoveLeft())
         { x-=STEP ; setImage("0.png"); setRotation (180); getImage().mirrorVertically();}
       
        if(Greenfoot.isKeyDown("right")&& canMoveRight())
        {x+=STEP; setImage("0.png"); setRotation(0);}
         
        setLocation(x, y);
    }
You need to login to post a reply.