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

2016/4/1

How to go after one target

1
2
Bassusour Bassusour

2016/4/1

#
My problem is i have a searching missile. But i want it to target one player (It is a two player game), and my problem is that it stands still, and cannot decide which player to target.
public void søgende()
    {
      if (!getWorld().getObjects(Player_1.class).isEmpty() && !getWorld().getObjects(Player_2.class).isEmpty())
      {
          if (Greenfoot.getRandomNumber(100) < 50)
            {  
              Player_1 player = (Player_1)getWorld().getObjects(Player_1.class).get(0);  
              turnTowards(player.getX(), player.getY());  
            } 
            else
            {
              Player_2 player = (Player_2)getWorld().getObjects(Player_2.class).get(0);  
              turnTowards(player.getX(), player.getY());  
            }
        }
      if (!getWorld().getObjects(Player_1.class).isEmpty() && getWorld().getObjects(Player_2.class).isEmpty())
      {
         {
          Player_1 player = (Player_1)getWorld().getObjects(Player_1.class).get(0);  
          turnTowards(player.getX(), player.getY());  
         }
      }
      if (!getWorld().getObjects(Player_2.class).isEmpty() && getWorld().getObjects(Player_1.class).isEmpty())
      {
          Player_2 player = (Player_2)getWorld().getObjects(Player_2.class).get(0);  
          turnTowards(player.getX(), player.getY());
      }
    }
danpost danpost

2016/4/1

#
What code do you have, and where is it located, creating and adding missiles into the world? Also, how do you want the missiles to decide which player to go after?
Bassusour Bassusour

2016/4/4

#
Thanks for the reply. This code is in the class 'ND' (the missile), and an object of the class ND is created when one of the players eat the class 'Risk'. And it should be a 50/50 in which player to target. Code for player eating the risk class (located in player)
public boolean spørgsmåltegn()
    {
           if ( canSee(Risk.class) )
           {
               eat(Risk.class);
               Greenfoot.playSound("slurp.wav");
               if (Greenfoot.getRandomNumber(100)<50)
               {
               fd_spist = fd_spist + 2;
               return false;
          } 
          else
           {
              fd_spist = fd_spist - 1;
              return true;
           } 
         }
         return false;
    }
Code if above is true (located in ND_dum class. This is the object running randomly around in the map)
public void nd_missil()
    {
        if (getWorld().getObjects(Player_1.class).size() >0 ) {
           Player_1 a = (Player_1) getWorld().getObjects(Player_1.class).get(0);
           if (a.spørgsmåltegn() ) 
           {
               getWorld().addObject (new ND(), getX(), getY());
            }
        }
        if (getWorld().getObjects(Player_2.class).size() >0 )
         {
           Player_2 b = (Player_2) getWorld().getObjects(Player_2.class).get(0);
           if (b.spørgsmåltegn_2() )
           {
               getWorld().addObject (new ND(), getX(), getY());
            }
        }      
    }
danpost danpost

2016/4/4

#
Your missiles need to decide on one player and stick with that player. It cannot be changing its choice of players every time it acts. It might be a good idea to have a reference field for the player that the missile has decided to go after, which we will deal with here:
/** in ND class */

// add instance field
private Actor chased;

// add 'findPlayer' method
private void findPlayer()
{
    chased = null;
    int choice = Greenfoot.getRandomNumber(2);
    if (!getWorld().getObjects(Player_1.class).isEmpty()) chased = (Actor)getWorld().getObjects(Player_1.class).get(0);
    if (chased == null || (choice == 1 && !getWorld().getObjects(Player_2.class).isEmpty())) chased = (Actor)getWorld().getObjects(Player_2.class).get(0);
}

// the 'søgende' method
public void søgende()
{
    if (chased == null || chased.getWorld() == null) findPlayer();
    if (chased == null) return;
    turnTowards(chased.getX(), chased.getY());
}
Now, I cannot for the life of me understand why you have the 'nd_missil' method in the ND_dum class. It would seem easier to add the ND object into the world from within the 'spørgsmåltegn' method in the player classes. The method would be called from the act method of the player class and it would not have to return any boolean value. To make things even easier, you could have a subclass of Actor called Player which Player_1 and Player_2 can extend and place all code used in both classes in it instead of having like code in two different places (for example, you could place a 'spørgsmåltegn' method in the Player class and eliminate the methods with that name from its subclasses).
Bassusour Bassusour

2016/4/6

#
Thanks for your reply, danpost. Adn you're right about setting things up in a much easier way, and not make it so complicated for no apparent reason. But keep ind mind, that this is my first game in the programming world, and I am not that experienced yet. However, when I want to relocate the nd_missil method from the ND_dum class to the player classes, I want the missil to spawn at one of the ND_dum objects, which already is in the map.
danpost danpost

2016/4/6

#
Bassusour wrote...
when I want to relocate the nd_missil method from the ND_dum class to the player classes, I want the missil to spawn at one of the ND_dum objects, which already is in the map.
But, if you place the code in the ND_dum class, all ND_dum objects will spawn a missile simultaneously -- which is not what you want. You will need to randomly pick one of the ND_dum objects in the world and spawn one at its location:
// in player class(es), when eating Risk class object
int dumSize = getWorld().getObjects(ND_dum.class).size();
if (dumSize > 0)
{
    int rand = Greenfoot.getRandomNumber(dumSize);
    Actor dum = (Actor)getWorld().getObjects(ND_dum.class).get(rand);
    getWorld().addObject(new ND(), dum.getX(), dum.getY());
}
Bassusour Bassusour

2016/4/6

#
Thank you for your help danpost. It really helps me a lot. By now, I'd actually be done with my game, but my game crashes, and I cannot find out why. It would be a huge help, if you'd help me. Here's the link for the game: http://www.greenfoot.org/scenarios/16370
danpost danpost

2016/4/6

#
Bassusour wrote...
my game crashes, and I cannot find out why.
You have three different methods in the ND class that could have an ND object remove itself from the world (specifically 'tid_metode', 'lookForPlayer_1' and 'lookForPlayer_2'). If any of these methods causes one to remove itself, then any future execution requiring the actor be in the world will fail. Every one of your methods except for the 'act' method has a call to 'getWorld' in them (or the method calls another method that calls 'getWorld'). It is not the calling of the method itself, but the attempt to execute a method of the World class on the reference returned by 'getWorld' which is 'null' -- and you cannot execute a method on a non-existent world. What this means is that you cannot allow the act method to call another method after one has the ND object remove itself. So, after each one of the method calls in the act method to any of these three methods, you need to insert the following line:
if (getWorld() == null) return;
Bassusour Bassusour

2016/4/6

#
Thank you so much! Now I'm really close to finish this game. However, there are still two unfixed bugs Bug nr. 1: For Player_1, when eating a question mark, and you are unlucky and gets removed, the game crashes, because of my movement method. This does not apply to Player_2, even thought they have the excact same method, except for the keys pressed. Bug nr. 2: It marks some of your code from the findPlayer method in th ND class. This is the code:
chased = (Actor)getWorld().getObjects(Player_2.class).get(0);
I have upfated the game, so you can try it out yourself if you want to.
Super_Hippo Super_Hippo

2016/4/6

#
Player 1 and Player 2 both get a Nullpointer Exception when eating a '?'. danpost explained you why this happened and showed you how to fix it. Line like this are useless:
if( !getWorld().getObjects(Player_1.class).isEmpty())
If the code is executed, the Player_1 class is either in the world or was removed this act-cycle. In this case, the line will give you an error. For bug number 2. I can't reproduce it, but you should remove the call to the findPlayer-method from the act-method. Some things for improvement: Press Control+Shift+I to intend code correctly. This makes it much easier to read. Change the image directly when moving. In the animation method, you check the keypress again which is redundant. You can keep these methods in both classes the same way with 'isKeyDown'. The reason for a superclass like Player is that you can put all methods in there which are same for Player 1 and Player 2. You don't need to superclass everything with the Animal class. No class you are using is an animal. To replace the 'canSee' and 'eat' methods, you can simply write 'isTouching' and 'removeTouching'.
Bassusour Bassusour

2016/4/6

#
Thanks for your help. However, bug nr. 1 still persists, even thought I removed the line you mentioned. I really appreciate your help, and thanks for the tips. I will keep them in mind :)
Super_Hippo Super_Hippo

2016/4/6

#
Add the line danpost gave you at the beginning of every method which is called after the actor could be removed from the world or after each method in the act-method.
if (getWorld() == null) return;
Bassusour Bassusour

2016/4/8

#
Than you very much for your help. However, there is one last problem. The missile doesn't target a player, if one of the players are removed from the game. I really don't know how to fix this, since I don't really understand danpost's code. (I have updated the game) I hope you can help me :)
Super_Hippo Super_Hippo

2016/4/8

#
Line 55 in your ND-class (the søgende-method) prevents the rest of the method to execute if one or both players are removed from the world.
if (getWorld().getObjects(Player_1.class).size() == 1 && getWorld().getObjects(Player_2.class).size() == 1)
I think you can remove this line and the following { and }. You could also change the '&&' to '||' so it still executes when one was removed.
Bassusour Bassusour

2016/4/8

#
Thanks for your answer, but I find your solution not to work for me. If only Player 2 is alive, the game works fine. However, if player 1 is alive, and a ND spawns, the game crashes, and marks the søgende method as the fault. Even more confusing is, that this will only happen when the second missile is created, and the first missile works fine.
There are more replies on the next page.
1
2