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

2015/3/10

A little Help with my project

Neco Neco

2015/3/10

#
Hi guys, I am programming a little game for school. I sucesfully solved every problem up to now, where I cant solve one that might have an answer so easy, I will just slamm my head on my desk once I read it. SO, I have various variables in there, for example poins. The player earns points for beating the first boss, then the world changes. How do I save the points (damage of the player, life, whatever, cause there will be upgrades and so on) from world 1 so that they also appear in world 2 and the combined once of 1 and 2 in 3 and so on? Thanks for your help ;)
Super_Hippo Super_Hippo

2015/3/10

#
You can either a) pass the current world to the next world and save it, so you can access the variable
//current world
private score = /*...*/;
//...
Greenfoot.setWorld(new Level2(this));
//...
public int getScore() {return score;}

//the other world
private World w = null;
Level2(World w)
{
    this.w = w;
}
//whenever you need the score
int score = w.getScore();
b) usually better: just pass the value of the variable to the next world
//current world
private score = /*...*/;
//...
Greenfoot.setWorld(new Level2(score));

//other world
private int score = 0;
Level2(int score)
{
    this.score = score;
}
By the way, the following is the best way in my opinion. It lets you create new levels very easily. Make all Levels extend one class named Level (for example) (→still a lot of world classes), or if possible in your game, make all Levels just be in one Level world class. This is especially then possible, if all levels are quite similar working. I have done it in my Bomberman game. The only difference in the levels are the number and type of the enemies, so I simply created an array which these numbers for each level in it. Then I only have to pass the number of the level, lives, number of bombs etc. to the next level like described in b).
CSBossmann CSBossmann

2015/3/10

#
Mirko, it isn´t ok to let others make the work, you have to make for school! :P
Neco Neco

2015/3/10

#
Oh CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARL... THanks Super_Hippo, thats what I needed ^^ Jup, already tryed the one World for every Level and every world extending one world class, but didnt get either one working good, but now I should get it ^^
You need to login to post a reply.