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

2017/2/3

Help!!!!

KyrenD2 KyrenD2

2017/2/3

#
I am doing a game that you need a checkpoint on and I have no idea how to code it.Can someone help?
Super_Hippo Super_Hippo

2017/2/3

#
It's like that: If you have more than one checkpoint (if you only want one, you can simply use a boolean which makes it a little bit easier):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//in main actor class
private int checkpoint = 0;
 
public NameOfTheActor(int c)
{
    checkpoint = c;
}
 
//in act
if (--reached checkpoint-- && didn't already reach this checkpoint) //either save the coordinates or have a checkpoint actor to create a condition here
{
    checkpoint++;
}
 
if (--gameLost--)
{
    ((WorldName) getWorld()).restart(checkpoint);
    //or
    Greenfoot.setWorld(new WorldName(checkpoint));
}
Depending on your last choice: (if you understand the code, you will see which one you will need)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// in the world
 
public WorldName()
{
    this(0);
}
 
public WorldName(int checkpoint)
{
    super...
    createWorld(checkpoint);
}
 
public void restart(int checkpoint)
{
    removeObjects(getObjects);
    createWorld(checkpoint);
}
 
public void createWorld(int checkpoint)
{
    //add objects which are always there no matter if a checkpoint is reached
    switch (checkpoint)
    {
        case 0:
        //add objects which are there when no checkpoint was reached already, like:
        addObject(new NameOfActor(checkpoint), 100, 100);
        break;
         
        case 1:
        //add objects which are there when the first checkpoint was reached
        addObejct(new NameOfActor(checkpoint), 200, 100);
        break;
         
        //...
    }
}
I hope you get what I mean. You can also save the the checkpoint variable in the world.
You need to login to post a reply.