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

2023/2/2

#
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

2023/2/2

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

2023/2/3

#
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);
Guy68 Guy68

2023/2/11

#
How can i save tha caracter in a static field?
danpost danpost

2023/2/11

#
Guy68 wrote...
How can i save tha caracter in a static field?
Some place in your selection world, when the player is chosen, you will use a line similar to this:
chosenPlayer = new PlayerA();
or, if already defined and set to a field:
chosenPlayer = playerA;
which would hold the chosen player in the static field declared in line 5 of my previous post.
Guy68 Guy68

2023/2/12

#
Yes but PlayerA is not working because i have two caracters, and i want to choose between them. I wan to play the other levels with the same caracter, without going back to choose the player again. I tryed my best to combine the code but it is not working. Maybe we can go with little steps. I have 2 caracters (Man and a Woman) And i have 3 worlds(MyWorld1,2,3) and I am choosing the caracters in the world Starting. I want to do if i clck on the man in the world satrting that i can play with the man at all 3 worlds and i am able to travell between them(the travelling between worlds works great, but i wan to play with the same caracter the whole game.
danpost danpost

2023/2/12

#
Guy68 wrote...
Yes but PlayerA is not working because i have two caracters, and i want to choose between them. I wan to play the other levels with the same caracter, without going back to choose the player again. I tryed my best to combine the code but it is not working. Maybe we can go with little steps. I have 2 caracters (Man and a Woman) And i have 3 worlds(MyWorld1,2,3) and I am choosing the caracters in the world Starting. I want to do if i clck on the man in the world satrting that i can play with the man at all 3 worlds and i am able to travell between them(the travelling between worlds works great, but i wan to play with the same caracter the whole game.
I had good doubt that you named your characters playerA and playerB. in your Starting world; hence, the words:
a line similar to
I do not know what you named them in the Starting world or whether you named them at all in that world. Still, the Man and Woman classes at some point extend the Actor class and any instance of them can be placed into my chosenPlayer field (which can be defined in your Starting world). Then, you can add Starting.chosenPlayer to your different game worlds. For more specific help, you will need to provide your Starting class codes.
You need to login to post a reply.