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

2023/4/1

Make image only flip once

TanishStIves TanishStIves

2023/4/1

#
I am trying to code a game and I want my png (character) to face the direction of the movement so I tried to make it flip horizontally everytime I clicked "s" (move left) but it would happen repeatedly and would just keep happening. I only want it to flip once when I click "s".
public void act()
    {
        
        
        if(Greenfoot.isKeyDown("d"))
        {
            
            setLocation(getX()+5, getY());
            setRotation(360);
            
            
        }
        
        
        
        if(Greenfoot.isKeyDown("a"))
        {
            
            getImage().mirrorHorizontally();
            
            setLocation(getX()-5, getY());
            
        }
        
        
       
        
    }
danpost danpost

2023/4/1

#
TanishStIves wrote...
I am trying to code a game and I want my png (character) to face the direction of the movement so I tried to make it flip horizontally everytime I clicked "s" (move left) but it would happen repeatedly and would just keep happening. I only want it to flip once when I click "s". << Code Omitted >>
Best would be to make a copy of the image, flip it and retain it in an Object reference field: along with the default image:
GreenfootImage leftImage, rightImage;

// in constructor of class
rightImage = getImage();
leftImage = new GreenfootImage(rightImage);
leftImage.mirrorHorizontally();

// then you can set the image as needed:
setImage(leftImage); // for example
// or
setImage(rightImage); // for another example

// always check for leftImage (if needed):
if (getImage() == leftImage) // for example
TanishStIves TanishStIves

2023/4/1

#
I'm sorry but I don't know how to link my left and right images to the code, could you please help me?
danpost danpost

2023/4/1

#
TanishStIves wrote...
I'm sorry but I don't know how to link my left and right images to the code, could you please help me?
Here it is:
public class /** class name here */ extends actor
{
    GreenfootImage leftImage, rightImage;
    
    public /** class name here */()
    {
        rightImage = getImage();
        leftImage = new GreenfootImage(rightImage);
        leftImage.mirrorHorizontally();
    }
    
    public void act()
    {
        if (Greenfoot.isKeyDown("d")) {
            move(5);
            if (getImage() == leftImage) {
                setImage(rightImage);
            }
        }
        if (Greenfoot.isKeyDown("a")) {
            move(-5);
            if (getImage() != leftImage) {
                setImage(leftImage);
            }
        }
    }
TanishStIves TanishStIves

2023/4/3

#
Okay thank you so much, with some other help I was able to fix this. I had to create 2 new actors(left and right) and linked them to the main character's code.
 if(Greenfoot.isKeyDown("d"))
        {
            GreenfootImage b = new GreenfootImage("CharacterR.png");  
            b.scale(106,137);
            setImage(b);
            setLocation(getX()+5, getY());
            
            
            
            
            
        }
        if(Greenfoot.isKeyDown("a"))
        {
            
            GreenfootImage c = new GreenfootImage("CharacterL.png");  
            c.scale(106,137);
            setImage(c);
            
            setLocation(getX()-5, getY());
            
            
            
        }
danpost danpost

2023/4/3

#
TanishStIves wrote...
Okay thank you so much, with some other help I was able to fix this. I had to create 2 new actors(left and right) and linked them to the main character's code. << Code Omitted >>
It might work, but it is more taxing on the system than need be. Your code will have both a directory file operation plus an image operation every act step during which a key is being held. Totally unnecessary. Best would be to retain the two images in Object reference fields. They can be static fields at no cost:
private static final GreenfootImage leftImage, rightImage;
Set the values and scale them in the constructor of the class:
if (leftImage == null) {
    leftImage = new GreenfootImage("CharacterL.png");
    leftImage.scale(106, 137);
    rightImage = new GreenfootImage("CharacterR.png");
    rightImage.scale(106, 137);
}
setImage(rightImage);
Then, your code above would just be this:
if(Greenfoot.isKeyDown("d"))
{           
    setImage(rightImage);
    setLocation(getX()+5, getY());   
}
if(Greenfoot.isKeyDown("a"))
{
    setImage(leftImage);
    setLocation(getX()-5, getY());
}
AMasterStIves AMasterStIves

2023/4/5

#
Bro this code is so obvious why you asking for help
TanishStIves TanishStIves

2023/4/5

#
Umm, I'm sorry but I am new to this and BTW thanks danpost, it makes way more sense and looks cleaner.
TheGoatFighter TheGoatFighter

2023/4/5

#
AMasterStIves is right this code is so obvious I dont know why u asking for help, dampost isnt even that good at coding. 1v1 me damposit
TanishStIves TanishStIves

2023/4/5

#
Geez, what? I am confused. Check your spelling.
TheGoatFighter TheGoatFighter

2023/4/5

#
Naaaa Yototrth
You need to login to post a reply.