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

2023/3/8

How would I go about initializing an actor in a world without using "new"?

FJacobs FJacobs

2023/3/8

#
I'm creating a FNAF minigame where the player (PurpleGuy/purple) has to find his Springtrap suit (IdleSpring/spring). The suit is in a different world than the player initially. Once PurpleGuy makes his way to the room and changes to the suit, it does not carry over into the other worlds; however, I change the worlds whenever the purple guy is in a certain X or Y location in them:
public void act()
    {
        
        
        
        
        if(purple.getX() > 983)
        {
            Greenfoot.setWorld(fan);
            
        }
        else if(purple.getY() < 40)
        {
            Greenfoot.setWorld(suit);
        }
    }
for instance above, where in my world "Turn" (it's just a corner) if he's at the top he goes into the suit up room, or if he's on the right he goes to the fan room. How would I go about "carrying" PurpleGuy's information over between worlds. Sorry if this is confusing how I word it. Also shoutout to danpost for helping me on my last two (the only one's I've made) posts!
Super_Hippo Super_Hippo

2023/3/8

#
You could pass the object itself into the new world. You have a reference to the world (fan or suit) and you have a reference to the player (this). You can use this information to “move” the object to the other world.
danpost danpost

2023/3/8

#
FJacobs wrote...
How would I go about "carrying" PurpleGuy's information over between worlds.
Just add the guy when setting the world. For example, line 10 could be:
fan.addObject(purple, 100, 100); // or wherever
FJacobs FJacobs

2023/3/9

#
I used this method, but I have 5 worlds, so it appears to only work in the world in the "center" and the two adjacent worlds. Am I doing this wrong?
FJacobs FJacobs

2023/3/9

#
Wouldn't I have to initialize each world and object like:
FanRoom fan = new FanRoom();

//or in the case of purple guy

PurpleGuy purple = new PurpleGuy();
Or am I understanding this incorrectly?
danpost danpost

2023/3/9

#
FJacobs wrote...
Wouldn't I have to initialize each world and object like: << Code Omitted
You do not have to initialize the player again (only initialized in the starting world). That is, your line 5 is not needed to put a player in a different world. The line I provide previously will move the player from one world to the next. The new world does not have to create another instance of a PurpleGuy object. If it would make things easier, you could add a method to the different worlds to help move the player into each world:
public void addPurpleGuy(PurpleGuy purpleGuy)
{
    purple = purpleGuy; // saving a reference to the player in this world
    addObject(purple, 100, 100); // or wherever
}
Then, instead of the line I gave previously, you would use:
fan.addPurpleGuy(purple);
Out of curiosity, what class does your FanRoom class extend?
You need to login to post a reply.