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

2019/1/7

calling methods

1
2
qwe qwe

2019/1/7

#
I am trying to call a method where the player would lose a life if the enemy object reached one side of the wall. I have my lives displayed at the top of the screen. However, there seems to be an error when calling the method.
1
2
3
4
5
6
//This is at the top of the page
public int lifeCounter = 3;
     
    private Lives lives1;
    private Lives lives2;
    private Lives lives3;
qwe qwe

2019/1/7

#
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
private void prepare()
    {
        addObject(new Rocket(), 100, 200);
        counter = new Counter();
        addObject(counter,300,30);
        GreenfootSound sound = new GreenfootSound("Opening.wav");
        Lives lives1 = new Lives();
        addObject(lives1,35,40);
        Lives lives2 = new Lives();
        addObject(lives2,95,40);
        Lives lives3 = new Lives();
        addObject(lives3,155,40);
    }
     
    public void checkLivesLeft()
    {
       if (lifeCounter == 2)
        {
            removeObject(lives3);
        }
         
       if(lifeCounter == 1)
        {
            removeObject(lives2);
        }
         
       if(lifeCounter == 0)
        {
            removeObject(lives1);
            Greenfoot.setWorld(new GameOverScreen());
        }
    }
     
    public void invasion()
    {
        if(getY(Enemy.class)<10)
        {
            lifeCounter--;
        }
         
    }   
}
Zamoht Zamoht

2019/1/7

#
Change your code to
1
2
3
4
5
6
7
8
9
10
11
12
13
private void prepare()
{
    addObject(new Rocket(), 100, 200);
    counter = new Counter();
    addObject(counter,300,30);
    GreenfootSound sound = new GreenfootSound("Opening.wav");
    lives1 = new Lives();
    addObject(lives1,35,40);
    lives2 = new Lives();
    addObject(lives2,95,40);
    lives3 = new Lives();
    addObject(lives3,155,40);
}
qwe qwe

2019/1/7

#
Thanks Zamhot, however what can i do about the method for invasion()?
danpost danpost

2019/1/7

#
qwe wrote...
Thanks Zamhot, however what can i do about the method for invasion()?
I am not Zamoht, however, here we go. The invasion method should be in the class of the enemy. At least, the enemy location check should be there.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**   in enemy act method   */
if (getY() < 10)
{
    MyWorld world = (MyWorld)getWorld();
    world.removeObject(this);
    world.adjustLives(-1);
}
 
/**   in world class   */
public void adjustLives(int amt)
{
    livesCounter += amt;
    if (livesCounter < 3) removeObject(lives3);
    if (livesCounter < 2) removeObject(lives2);
    if (livesCounter < 1)
    {
        removeObject(lives1);
        Greenfoot.delay(60);
        Greenfoot.setWorld(new GameOverScreen());
    }
}
Remove both the invasion and the checkLivesLeft methods.
qwe qwe

2019/1/8

#
Thanks dan! however, I receieve an error from Greenfoot java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:714) at greenfoot.Actor.getY(Actor.java:178) at Enemy.act(Enemy.java:70) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:714) at greenfoot.Actor.getY(Actor.java:178) at Enemy.act(Enemy.java:70) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Enemy here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    private int vx=0;
    private boolean toRemove = false;
    public Enemy()
    {
        
    }
     
    public Enemy(int v)
    {
        vx=v;
    }
     
    public void move()
    {
        setLocation(getX()+vx,getY());
        Actor actor = getOneIntersectingObject(Rocket.class);
        if(actor!=null)
        {
            destroyed();
        }
        if(getX()<-200)
        {
            toRemove=true;
        }
    }
     
    public void destroyed()
    {
        for(int i=0; i<10;i++)
        {
            int px =-20+Greenfoot.getRandomNumber(40);
            int py =-20+Greenfoot.getRandomNumber(40);
             
            getWorld().addObject(new ExplosionRemains(getImage()),getX()+px,getY()+py);
        }
        getWorld().addObject(new ExplosionEffect(),getX(),getY());
        toRemove=true;
    }
     
    public void addedToWorld(World GameScreen)
    {
        setRotation(180);
    }
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if(!toRemove)
        {
            move();
        }
        else
        {
            getWorld().removeObject(this);
        }
         
         
        if (getY() < 10)
        {
            GameScreen world = (GameScreen)getWorld();
            world.removeObject(this);
            world.adjustLives(-1);
        }
    }
}
"
danpost danpost

2019/1/8

#
qwe wrote...
Thanks dan! however, I receieve an error from Greenfoot << Error Omitted >> << Code Omitted >>
Insert it after line 62. (move -- then check location)
qwe qwe

2019/1/8

#
Thanks danpost for the help! However, there seems to be a 45 sec delay before the the life icon is removed from the screen. sometimes after 20 enemies have passed by, no icons have been removed , and the finally world changes but, the world should change right after 3 enemies have past by.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class GameScreen here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class GameScreen extends World
{
    private int delay = 0;
    Counter counter = new Counter();
    public int lifeCounter = 3;
      
    private Lives lives1;
    private Lives lives2;
    private Lives lives3;
    /**
     * Constructor for objects of class GameScreen.
     *
     */
    public GameScreen()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);
         
        prepare();
    }
     
    public Counter getCounter()
    {
      return counter; 
    }
 
    public void act()
    {
        if(delay>0)
        {
            delay--;
        }
        else
        {
            delay=20;
        }
        if(delay==1)
        {
            int py=Greenfoot.getRandomNumber(getHeight());
            addObject(new Enemy(-(2+Greenfoot.getRandomNumber(3))),getWidth()+200,py);
        }
        adjustLives();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(new Rocket(), 100, 200);
        counter = new Counter();
        addObject(counter,300,30);
        GreenfootSound sound = new GreenfootSound("Opening.wav");
        sound.stop();
        Lives lives = new Lives();
        addObject(lives,35,40);
        Lives lives2 = new Lives();
        addObject(lives2,95,40);
        Lives lives3 = new Lives();
        addObject(lives3,155,40);
    }
     
    public void adjustLives(int amt)
    {
        lifeCounter += amt;
        if (lifeCounter < 3)
        {
            removeObject(lives3);
        }
        if (lifeCounter < 2)
        {
            removeObject(lives2);
        }
        if (lifeCounter < 1)
        {
            removeObject(lives1);
             
            Greenfoot.setWorld(new GameOverScreen());
        }
    }
}
    
danpost danpost

2019/1/8

#
qwe wrote...
Thanks danpost for the help! However, there seems to be a 45 sec delay before the the life icon is removed from the screen. sometimes after 20 enemies have passed by, no icons have been removed , and the finally world changes but, the world should change right after 3 enemies have past by. << Code Omitted >>
Please show your revised Enemy class codes. Also, remove the first word, 'Lives', from lines 64, 66 and 68 in your GameScreen class code and remove line 60 altogether.
qwe qwe

2019/1/8

#
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Enemy here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    private int vx=0;
    private boolean toRemove = false;
    public Enemy()
    {
        
    }
     
    public Enemy(int v)
    {
        vx=v;
    }
     
    public void move()
    {
        setLocation(getX()+vx,getY());
        Actor actor = getOneIntersectingObject(Rocket.class);
        if(actor!=null)
        {
            destroyed();
        }
        if(getX()<-200)
        {
            toRemove=true;
        }
    }
     
    public void destroyed()
    {
        for(int i=0; i<10;i++)
        {
            int px =-20+Greenfoot.getRandomNumber(40);
            int py =-20+Greenfoot.getRandomNumber(40);
             
            getWorld().addObject(new ExplosionRemains(getImage()),getX()+px,getY()+py);
        }
        getWorld().addObject(new ExplosionEffect(),getX(),getY());
        toRemove=true;
    }
     
    public void addedToWorld(World GameScreen)
    {
        setRotation(180);
    }
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if(!toRemove)
        {
            move();
            if (getY() < 10)
            {
                GameScreen world = (GameScreen)getWorld();
                world.removeObject(this);
                world.adjustLives(-1);
            }
        }
        else
        {
            getWorld().removeObject(this);
        }
    }   
}
danpost danpost

2019/1/8

#
qwe wrote...
<< Code Omitted >>
Okay -- looks like you did alright in the Enemy class. Did you make the other changes I suggested in my last post (edited in)?
qwe qwe

2019/1/8

#
Yes, i still have the same issue though. There is a long delay and the lives don't get removed off the screen immediately. It seems that after 20+ objects have past by, the Gameover screen appears.
danpost danpost

2019/1/8

#
qwe wrote...
Yes, i still have the same issue though. There is a long delay and the lives don't get removed off the screen immediately. It seems that after 20+ objects have past by, the Gameover screen appears.
Which way have they "past by"?
qwe qwe

2019/1/8

#
the main actor is displayed on the left hand side of the world. The enemy object are coming from the right side of the screen. Once the enemy objects hit the opposite end, without being shot, a life is supposed to be lost.
danpost danpost

2019/1/8

#
qwe wrote...
the main actor is displayed on the left hand side of the world. The enemy object are coming from the right side of the screen. Once the enemy objects hit the opposite end, without being shot, a life is supposed to be lost.
I had a feeling. Your original code was using getY which led me to believe the enemies were going upward (see line 36 of your second post -- in the invasion method). It seems a bit odd, but I went with it. Okay, change line 63 in your last code post to:
1
if (getX() < 10)
You can probably remove lines 31 through 34 also.
There are more replies on the next page.
1
2