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

2017/4/10

Reseting the world using an actor

1
2
AustinProkopishin AustinProkopishin

2017/4/10

#
I am making a game for a school project and I want the world to automatically reset when the player character dies. I don't know how to go about doing this and would appreciate any help you could offer.
danpost danpost

2017/4/10

#
AustinProkopishin wrote...
I want the world to automatically reset when the player character dies.
Create a new world instance and use the Greenfoot.setWorld method to set it active..
AustinProkopishin AustinProkopishin

2017/4/10

#
I tried that but nothing happens, I put it in my player class
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * The Player charecter
 *
 * @author Austin Prokopishin
 * @version 1.0
 */
public class Player extends Actor
{
    private int gemsCollected = 0;
    private int counter = 0;
    private int deaths = 0;
    GifImage gifImage = new GifImage("spaceshipV2.gif");
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage(gifImage.getCurrentImage());
        //resetting the world
         if (deaths == 1) {
            Greenfoot.setWorld(new MyWorld());
        }
 
        //interacting with gems/crystals
        if(foundGem()) {
            collectGem();
        }
 
        //movement
        if (Greenfoot.isKeyDown("left")) {
            setLocation( getX() - 3, getY() ); 
        }   
        if (Greenfoot.isKeyDown("right")) {
            setLocation( getX() + 3, getY() ); 
        
        if (Greenfoot.isKeyDown("up")) {
            setLocation( getX() , getY() - 3 ); 
        }   
        if (Greenfoot.isKeyDown("down")) {
            setLocation( getX() , getY() + 3 ); 
        }
 
        //rotate
        if (Greenfoot.isKeyDown("d")) {
            setRotation (getRotation() + 3); 
        
        if (Greenfoot.isKeyDown("a")) {
            setRotation (getRotation() - 3); 
        }
 
        //fire lasers
        if (Greenfoot.isKeyDown("space")) {
            counter++;
        
        if(counter > 10) {
            counter = 0;
        }
        if (counter == 5) {
            Greenfoot.playSound("lasersound.wav");
            fire();
        }
 
        if (isTouching(alien.class)) {
            removeTouching(alien.class);
            death();
            deaths++;
        }
 
        
    }
 
    public boolean foundGem()
    {
        Actor gem = getOneObjectAtOffset(0, 0, Crystal.class);
        if(gem != null) {
            return true;
        }
        else {
            return false;
        }
    }
 
    public void collectGem()
    {
        Actor Gem = getOneObjectAtOffset(0, 0, Crystal.class);
        if(Gem != null) {
            
            getWorld().removeObject(Gem);
            gemsCollected = gemsCollected + 1;
 
        }
    }
 
    private void fire()
    {
        LazerBolt bolt = new LazerBolt();
        World myWorld = getWorld();
        myWorld.addObject(bolt, 0, 0);
        bolt.setLocation(getX(), getY());
        bolt.setRotation(getRotation());
 
    }
 
    public void death()
    {
        
        getWorld().addObject (new explosion2(), getX(), getY());
        getWorld().removeObject(this);
    }
}
Nosson1459 Nosson1459

2017/4/10

#
Try removing line 111. You don't need to remove the actor if you're starting a new world. Since you remove it, the if in the beginning of the act never executes with deaths being 1. If the only values that deaths will hold are 0 and 1 then you should use a boolean.
AustinProkopishin AustinProkopishin

2017/4/10

#
It works! thanks for your help!
danpost danpost

2017/4/10

#
Remove lines 23 through 25 and add the following code to your world act method:
1
2
3
4
if (getObjects(Player.class).isEmpty() && getObjects(explosion2.class).isEmpty())
{
    Greenfoot.setWorld(new MyWorld());
}
You can also remove lines 13 and 69 from the Player class. This way will allow the explosion animation to complete before changing worlds.
AustinProkopishin AustinProkopishin

2017/4/11

#
danpost wrote...
Remove lines 23 through 25 and add the following code to your world act method:
1
2
3
4
if (getObjects(Player.class).isEmpty() && getObjects(explosion2.class).isEmpty())
{
    Greenfoot.setWorld(new MyWorld());
}
You can also remove lines 13 and 69 from the Player class. This way will allow the explosion animation to complete before changing worlds.
I am getting an error that says that it cannot find the method "getObjects"
danpost danpost

2017/4/11

#
AustinProkopishin wrote...
I am getting an error that says that it cannot find the method "getObjects"
Read carefully:
danpost wrote...
Remove lines 23 through 25 and add the following code to your world act method
AustinProkopishin AustinProkopishin

2017/4/11

#
Thanks! unfortunately I am still having one error, when the player is removed the aliens have nowhere to go and the game crashes, is there a way to remove them when the spaceship gets removed?
danpost danpost

2017/4/11

#
You could do that by adding the following code with the creation of the 'explosion' object when hit:
1
getWorld().removeObjects(getWorld().getObjects(alien.class));
Or you could just have the aliens roam around when the player is not in the world. If you would prefer this way, you will need to show the codes contained in your alien class.
AustinProkopishin AustinProkopishin

2017/4/11

#
It worked but now I have a new problem in MyWorld class I made it so that the aliens respawn when killed but, now it crashes again. I tried putting a Boolean to prevent this issue but it still does nothing.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * The game world.
 *
 * @author Austin Prokopishin
 * @version 1.0
 */
public class MyWorld extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("8-Bit Dubstep IV.mp3");
    private int lives = 3;
    private boolean dead;
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 1600x770 cells with a cell size of 1x1 pixels.
        super(1600, 770, 1);
        populate();
        backgroundMusic.playLoop();
    }
 
    /**
     * Method act
     *
     */
    public void act()
    {
         
        //check for game over
        if (lives == 0) {
            Greenfoot.setWorld(new GameOverScreen());
        }
 
                if (getObjects(Player.class).isEmpty() && getObjects(explosion2.class).isEmpty())
        {
            Greenfoot.setWorld(new MyWorld());
            lives--;
            dead = true;
        
         
        if (getObjects(alien.class).isEmpty() && dead != true)
        {
            addObject(new alien(), 1551, 35);
            addObject(new alien(), 1556, 77);
            addObject(new alien(), 1488, 45);
            addObject(new alien(), 30, 732);
            addObject(new alien(), 42, 663);
            addObject(new alien(), 94, 726); 
        
    }
    public void populate(){
               int x;
        int y;
        int x2;
        int y2;
        for(int i = 0; i < 5; i++) {
            x = (int)(Math.random() * 1600);
            y = (int)(Math.random() * 770);
            x2 = (int)(Math.random() * 1600);
            y2 = (int)(Math.random() * 770);
            addObject(new Asteroid1(), x, y);
            addObject(new Asteroid2(), x2, y2);
 
        }
        addObject(new Player(), 800, 385);
 
        addObject(new alien(), 1551, 35);
        addObject(new alien(), 1556, 77);
        addObject(new alien(), 1488, 45);
        addObject(new alien(), 30, 732);
        addObject(new alien(), 42, 663);
        addObject(new alien(), 94, 726);
    }
 
}
danpost danpost

2017/4/12

#
There does not appear to be anything in the MyWorld class that would cause a crash. Are you getting any error meessages? iff so, what (copy/paste)?
AustinProkopishin AustinProkopishin

2017/4/12

#
I put a Boolean on lines 13, 42 and 45 to prevent the aliens from respawning after the player dies. if there are any aliens in the world when the player is not there the world crashes because the aliens have nowhere to go.
danpost danpost

2017/4/12

#
AustinProkopishin wrote...
I put a Boolean on lines 13, 42 and 45 to prevent the aliens from respawning after the player dies. if there are any aliens in the world when the player is not there the world crashes because the aliens have nowhere to go.
You could just make the aliens move around randomly if no player is in the world (like I suggested previously).
AustinProkopishin AustinProkopishin

2017/4/12

#
ok I think that may be a better way to do it, here is my code for the alien:
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
38
39
40
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * The enemy ship that chases the player.
 *
 * @author Austin Prokopishin
 * @version 1.0
 */
public class alien extends Actor
{
    GifImage gifImage = new GifImage("AlienShip.gif");
    
    /**
     * Act - do whatever the alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         setImage(gifImage.getCurrentImage());
         goTo();
         if (isTouching(LazerBolt.class)) {
            removeTouching(LazerBolt.class);
            death();
        }
    }
        public void goTo()
    {
               Player player = (Player)getWorld().getObjects(Player.class).get(0);
        turnTowards(player.getX(),player.getY());
        move(2);
         
    }
     
     public void death()
    {
       getWorld().addObject (new explosion3(), getX(), getY());
        getWorld().removeObject(this);
    }
     
}
There are more replies on the next page.
1
2