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

2017/5/5

how to keep health between worlds

1
2
FutureCoder FutureCoder

2017/5/5

#
When i swap between worlds, i just have it swap to another world with the character already placed in it, how do i get the character to swap to another world, and still have the same health and same value for all the other variables.
danpost danpost

2017/5/5

#
FutureCoder wrote...
When i swap between worlds, i just have it swap to another world with the character already placed in it, how do i get the character to swap to another world, and still have the same health and same value for all the other variables.
If all the variables are in the class of the character, just pass the character to the new world and have it add that character into it instead of a new character.
FutureCoder FutureCoder

2017/5/5

#
how would i do this?
Yehuda Yehuda

2017/5/5

#
You can have it as a parameter in the second world.
private Character character;

public World2() {
    this(new Character());
}

public World2(Character charctr) {
    character = charctr;
    // here/now you can use charctr/character however you wanted and it will have the values specified
    // in this case the ones from the first world
}
When you switch to World2 you'll have to put the instance of your character as the argument for the parameter. If in the first world you created your Character by doing this in the beginning of your class:
Character character = new Character();

// then when you switch worlds to World2 you would do:
Greenfoot.setWorld(new World2(character));
FutureCoder FutureCoder

2017/5/5

#
it is saying "this must be first statement in conductor" about this(new Character());, and when i put it first, it says the same thing about the code for the size of the world, what do i do to fix this.
danpost danpost

2017/5/5

#
FutureCoder wrote...
it is saying "this must be first statement in conductor" about this(new Character());, and when i put it first, it says the same thing about the code for the size of the world, what do i do to fix this.
You need to insert a 'super' call at line 8 with the dimensions of the world.
FutureCoder FutureCoder

2017/5/5

#
i tried putting the super line at line 8, but it came up with an error, what code should i use?
danpost danpost

2017/5/5

#
FutureCoder wrote...
i tried putting the super line at line 8, but it came up with an error, what code should i use?
What code did you try? What error did you get?
FutureCoder FutureCoder

2017/5/5

#
wasn't sure exactly what to do so i put super(1000, 618, 1); under character = charctr; and it gave me this error "no suitable constructor found for World(no arguments)
danpost danpost

2017/5/5

#
FutureCoder wrote...
wasn't sure exactly what to do so i just put super(1000, 618, 1); under character = charctr; and it gave me this error "no suitable constructor found for World(no arguments)
Switch the lines. Inserting at would mean what was on line 8 should be shifted down.
FutureCoder FutureCoder

2017/5/6

#
i've used this code, it compiles but doesn't do anything, what else could i do?
danpost danpost

2017/5/6

#
FutureCoder wrote...
i've used this code, it compiles but doesn't do anything, what else could i do?
Make sure you do not have any other 'new Character()' in the World2 class except for in 'this(.. here ...)'.
FutureCoder FutureCoder

2017/5/6

#
i've got
private Character character;
 
public World2() {
    this(new Character());
}
 
public World2(Character charctr) {
    character = charctr;
   
}
in the world2 class, and
 Character character = new character();
     
       if (getWorld() instanceof world1)

       {
     if(isTouching(Character.class))
     {
       
         Greenfoot.setWorld(new world2(character));
        }
    }
in another object that changes to world2 when the character touches it. Is there any code i need to add or take away to get this to work?
danpost danpost

2017/5/6

#
In the last snippet given, you are passing a different Character object to the new world2 object -- you are passing the one created in the first line of that snippet, not the one that is already in your world1 object. Remove line 1. Place the following at line 8:
Character character = (Character) getOneIntersectingObject(Character.class);
FutureCoder FutureCoder

2017/5/6

#
i tried this but it didn't change anything, it swaps between the worlds, but the character isn't placed in it.
There are more replies on the next page.
1
2