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

2020/3/21

How to

HarveyP30 HarveyP30

2020/3/21

#
I'm currently working on a dodgeball game. As usual, all players should be out for the game to be over. In the program of the Ball, it says there the following: public void act(){ move (2); if(isTouching(Player1.class)) { getWorld().removeObject(this); } if(isTouching(Player2.class)) { getWorld().removeObject(this); } if(Player1.class == null && Player2.class == null) { Greenfoot.setWorld(new Gameover()); } } However, if a player got hit, it errors into "Actor not in world." What can I do to fix this? And how can I make a program wherein the two players should be hit first then go to the Gameover screen?
danpost danpost

2020/3/21

#
In the last if, the classes will NEVER be null (the classes exist, otherwise you would not have objects being created from them. In other words, that is not how you check if no actors from those classes are in the world. Besides, it would be better to have the world perform that check with:
public void act()
{
    if (getObjects(Player1.class).isEmpty() && getObjects(Player2.class).isEmpty())
    {
        Greenfoot.setWorld(new GameOver());
    }
}
Your other if blocks would remove the ball, not the hit player, from the world. Maybe, the players should check for the ball and remove self when being hit (instead of ball checking for players), leaving the following act method in the ball class (at least, for now):
public void act()
{
    move(2);
}
HarveyP30 HarveyP30

2020/3/22

#
Oh. So I should put the isTouching inside the players' code? Not in the ball?
HarveyP30 HarveyP30

2020/3/22

#
I figured it out: however, I have this code inside the players for the second level of the game. if(isTouching(Shot3.class)||isTouching(Shot4.class)) { getWorld().removeObject(this); } If the player got hit in Level 1, that code would error which says "Actor not in World."
HarveyP30 HarveyP30

2020/3/22

#
Oh nevermind, I resolved it by having for conditions in an if using ||.
You need to login to post a reply.