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

2022/3/4

How to flip an image when border is reached?

Yaren Yaren

2022/3/4

#
I'm making a capybara game and the enemy of the capybara is a crocodile. But how do I make the crocodile flip 180 degrees and move to the left side when it reaches the border?
RcCookie RcCookie

2022/3/4

#
You can just mirror the current image:
getImage().mirrorHorizontally();
If you also want to invert the movement direction, you should make a variable that stores the current movement direction:
public class Crocodile extends Actor {
    // ...
    private boolean moveRight = true;
    // ...
}
Then, whenever you move, you test in which direction you should move:
int speed = ...;
if(moveRight) move(speed);
else move(-speed);
Finally, when you reach a wall you just flip the value of the moveRight variable:
moveRight = !moveRight;
An alternate aproach to the boolean variable could also be an int variable. May sound weird at first, but it's actually quite nice to work with:
// The variable
int direction = 1; // 1 = right, -1 = left

// moving
int speed = ...;
move(speed * direction);

// flipping direction
direction *= -1;
Yaren Yaren

2022/3/5

#
In which field do I need to put int speed = ...; and if(moveRight) move(speed); and else move(-speed);?
Yaren Yaren

2022/3/5

#
Because I keep getting the error ''illegal start of type''
RcCookie RcCookie

2022/3/5

#
You mean which method? If your crocodile moves, you must be calling the "move()" method somewhere. This call should be replaced with the three lines, and, obviously, the "..." needs to be replaced with a value or expression of the actual speed of the crocodile.
RcCookie RcCookie

2022/3/5

#
Example of the full class:
public class Crocodile extends Actor {

    private boolean moveRight = true;

    public void act() {
        int speed = 1;
        if(moveRight) move(speed);
        else move(-speed);

        // Flip direction when on the edge
        if(isAtEdge())
            moveRight = !moveRight;
    }
}
Yaren Yaren

2022/3/5

#
I don't know what I've done wrong, the system tells me that there is an error found in the class but it doesn't show me where. Could you help me find it? This is the code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Crocodile here. * * @author (your name) * @version (a version number or a date) */ public class Crocodile extends Animal { /** * Act - do whatever the Crocodile wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private boolean moveRight = true; public void act() { doCrocodileThings(); int speed = 1; if(moveRight) move(speed); else move(-speed); //Flip direction when on the edge if(isAtEdge()) moveRight = !moveRight; } }
Yaren Yaren

2022/3/5

#
I also don't know where to put the getImage().mirrorHorizontally(); . I'm a beginner still and it's quite difficult for me.
Yaren Yaren

2022/3/5

#
Oh! it works now but the crocodile keeps flipping from left to right even when it doesnt touch the world edge. Is there a solution for this?
Yaren Yaren

2022/3/5

#
Okay so I made the image flip when it touches the edge, but then it doesnt stay flipped and continues to move with the crocodileright.png image. Here is the code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Crocodile here. * * @author (your name) * @version (a version number or a date) */ public class Crocodile extends Animal { /** * Act - do whatever the Crocodile wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private boolean moveRight = true; public void act() { int speed = 2; if(moveRight) move(speed); else move(-speed); setImage("crocodileright.png"); //Flip direction when on the edge if(isAtEdge()) moveRight = !moveRight; if(isAtEdge()) getImage().mirrorHorizontally(); } private void doCrocodileThings() { if(isTouching(Capy.class)) { gameOver(); } } /** * We have won the game. */ public void gameOver() { Greenfoot.stop(); } }
danpost danpost

2022/3/6

#
Best is probably to retain both images (non-mirrored and mirrored) in fields:
import greenfoot.*;

public class Crocodile extends Actor
{
    GreenfootImage[] images = new GreenfootImage[2];
    int dir = 1; // -1 for left; 1 for right
    int speed = 2;
    
    public Crocodile()
    {
        images[0] = new GreenfootImage("crocodileright.png");
        images[1] = new GreenfootImage(images[0]);
        images[1].mirrorHorizontally();
    }
    
    public void act()
    {
        move(dir*speed);
        if (isAtEdge())
        {
            dir = -dir;
            setImage(images[(1-dir)/2];
        }
        if (isTouching(Capy.class)) Greenfoot.stop();
    }
}
You need to login to post a reply.