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

2023/10/16

I need help with changing sprits

Lolan Lolan

2023/10/16

#
I am trying to make a start screen where you are able to change the image of the Actor. But the code for the image change is in the start screen world and the code for the actor is in the actor itself. Is there a way to change the image of the actor through the start screen world? Code from the world \/ if (Greenfoot.isKeyDown("1")) Greenfoot.setWorld(new MyWorld()); if (Greenfoot.isKeyDown("2")) Greenfoot.setWorld(new MyWorld()); if (Greenfoot.isKeyDown("3")) Greenfoot.setWorld(new MyWorld()); if (Greenfoot.isKeyDown("4")) Greenfoot.setWorld(new MyWorld());
danpost danpost

2023/10/16

#
Lolan wrote...
I am trying to make a start screen where you are able to change the image of the Actor. But the code for the image change is in the start screen world and the code for the actor is in the actor itself. Is there a way to change the image of the actor through the start screen world? << Code Omitted >>
It would be better to use getKey instead of isKeyDown for non-game play input. Also, you need to retain what that input was so the actor can tell which image to use:
import greenfoot.*;

public class StartScreen extends World
{
    public static int imageChoice;
    
    public StartScreen() {
        // add numbered images and instructions
    }
    
    public void act() {
        String key = Greenfoot.getKey();
        if (key == null || key.length() != 1) return;
        imageChoice = "01234".indexOf(key);
        if (imageChoice < 1) return;
        Greenfoot.setWorld(new MyWorld());
    }
}
Then, in actor class, ActorName, with images named playerImage1.png,p layerImage2.png, etc., in "images" folder
public ActorName() {
    setImage("playerImage"+StartScreen.imageChoice+".png");
}
Lolan Lolan

2023/10/17

#
Thank you very much. You are a true life savor! (:
Lolan Lolan

2023/10/17

#
I do have an error when I try to run the code. It has something to do with the actor itself saying it cant find the image, even though all the images are in the sprit folder. Idk why its happening??
Lolan Lolan

2023/10/17

#
never mind, managed to fix it! but thanks anyways for the help!
You need to login to post a reply.