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

2023/1/25

Saving the caracter

Guy68 Guy68

2023/1/25

#
i have two characters and i have levels games. after playing a certain level, I would like to continue the game with the same character, but it does not save the character. What can be done? anyone have an idea?
danpost danpost

2023/1/26

#
Guy68 wrote...
i have two characters and i have levels games. after playing a certain level, I would like to continue the game with the same character, but it does not save the character. What can be done? anyone have an idea?
Just move the character into the new level. Do not create a new one. For example: Create next level world with (I am using Level2 as the new level world and Player as the character in question);
Level2 level2 = new Level2(player);
with constructor in Level2 class starting
public Level2(Player player)
{
    super(600, 400, 1);
    addObject(player, 300, 200);
    // etc.
Guy68 Guy68

2023/2/1

#
We have two players and we want to chose between them but after we completed one level we want to play with the same charater again. It is possible to save the player we chose in the beggining of the game?
danpost danpost

4 days ago

#
Guy68 wrote...
We have two players and we want to chose between them but after we completed one level we want to play with the same charater again. It is possible to save the player we chose in the beggining of the game?
You could save the character in a static field (in your selection world). Then, the Level2 constructor might look simply like the following:
public Level2()
{
    super(600, 400, 1);
    addObject(SelectionWorldNameHere.player, 300, 200);
    // etc.
}
Guy68 Guy68

4 days ago

#
What is static field? How can i save the player in that?
danpost danpost

3 days ago

#
Guy68 wrote...
What is static field? How can i save the player in that?
A static field is a field that is declared from the onset. It does not belong to any object created from the class it is in. It belongs to the class itself. All objects created from the class (when cast as that type object) can access it. If set public (as opposed to private), any class can access the field. For example:
import greenfoot.*;

public class SelectionWorld extends World
{
    public static Actor chosenPlayer;
    
    public SelectionWorld()
    {
        super(600, 400, 1);
       // etc.
    }
    
    // etc.
}
Line 5 declares a field called chosenPlayer which can hold an Actor object. When the scenario is initially loaded (even before any world is created), the field is created and will have an initial value of null. Its value continues to be in effect even when resetting the scenario. Only reloading the scenario will reset the field. When this world is set active, it should not be left (to go to any game level world) until the selection is verified and the field contains an actual Actor object and no longer has a null value. With public access, the field can be access from anywhere as like in the following lines of code:
if (SelectionWorld.chosenPlayer == null) Greenfoot.setWorld(new SelectionWorld());

// or
addObject(SelectionWorld.chosenPlayer, 300, 200);
You need to login to post a reply.