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

2016/5/27

Using getImage().mirrorHorizontally()

moretinshop moretinshop

2016/5/27

#
So I have a run animation that I'd like to be able to mirror depending on which direction the player is moving within Greenfoot, rather than creating separate mirrored images for moving left and right. Currently, the way I'm using mirrorHorizontally() is constantly flipping the image between left and right with every run of the act method. What am I doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public void act()
{
    if(Greenfoot.isKeyDown("left")){
        facingRight = false;
        checkCollision();
        if(!isCollidingWall)
            setLocation(getX() - speed, getY());
        isMoving = true;
    }
    else if(Greenfoot.isKeyDown("right")){
        facingRight = true;
        checkCollision();
        if(!isCollidingWall)
            setLocation(getX() + speed, getY());
        isMoving = true;
    }
    if(canMove && Greenfoot.isKeyDown("up") && !isJumping){
        velocityY = 14.0f;
        isJumping = true;
    }
    
    if(!Greenfoot.isKeyDown("left") && !Greenfoot.isKeyDown("right")){
        animationTimer = 0;
        isMoving = false;
    }
    gravity();
    checkCollision();
     
    //image changing code
    if(isMoving && !isJumping){
        if(animationTimer == 0){
            setImage(playerRunningRight1);
        }
        else if (animationTimer == 6){
            setImage(playerRunningRight2);
        }
        else if (animationTimer == 12){
            setImage(playerRunningRight3);
        }
        else if (animationTimer == 18){
            setImage(playerRunningRight2);
        }
        else if (animationTimer == 24){
            animationTimer = -1;
        }
        animationTimer++;
    }
    else if(!isJumping)
        setImage(playerStandingRight);
    else if(isJumping)
        setImage(playerJumpingRight);
     
    if(!facingRight){
        getImage().mirrorHorizontally();
    }
}
Super_Hippo Super_Hippo

2016/5/27

#
You only have to mirror the image when either you are moving left and a new animation image was selected or when the direction was changed, not all the time 'facingRight' is false. But even this won't work. The problem is, that you if you mirror images, the original image is changed. So next time you use the image, the image is still mirrored. I think it would be more easy to save every image and select the one which is needed right now. It could look a bit like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private GreenfootImage[][] images =
{
    {//facing right
        new GreenfootImage("playerRunningRight1"),
        new GreenfootImage("playerRunningRight2"),
        new GreenfootImage("playerRunningRight3"),
        new GreenfootImage("playerStandingRight"),
        new GreenfootImage("playerJumpingRight")
    },
    { //facing left -- I am not sure if it works this way... if not, you can mirror them in a method
        new GreenfootImage("playerRunningRight1").mirrorHorizontally(),
        new GreenfootImage("playerRunningRight2").mirrorHorizontally(),
        new GreenfootImage("playerRunningRight3").mirrorHorizontally(),
        new GreenfootImage("playerStandingRight").mirrorHorizontally(),
        new GreenfootImage("playerJumpingRight").mirrorHorizontally()
    }
};
 
//when changing directions, adapt this variable
private int direction = 0; //0=right, 1=left
//when changing the state (from animation or when stop/start moving/jumping, adapt this variable
private int state = 3; //0..2=running, 3=standing, 4=jumping
 
//and then, at the end of the act-method:
setImage(images[direction][state]);
//or (but I don't know if it's worth it)
if (getImage() != images[direction][state]) setImage(images[direction][state]);
moretinshop moretinshop

2016/5/27

#
Thanks for your help, I ended up using a solution similar to the one found in this thread: http://www.greenfoot.org/topics/200 BTW, lines 11-15 do not work without a method, the linked thread uses a for loop to do something similar.
You need to login to post a reply.