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
danpost danpost

2016/4/8

#
Where is the code that moves the missile? It should be checking that 'chased' is not 'null' and that it is in the world before moving toward it each act cycle. If not, it should call 'findPlayer' again to acquire a new target. You may want the missile to be removed if no player is in the world; but that is something you would need to decide upon.
Bassusour Bassusour

2016/4/8

#
Here's the code:
public void no_players()
    {
        if (getWorld().getObjects(Player_2.class).isEmpty() && getWorld().getObjects(Player_1.class).isEmpty())
        {
            getWorld().removeObject(this);
        }
    }

    private void findPlayer()
    {
        chased = null;
        int choice = Greenfoot.getRandomNumber(2);
        if (choice == 0 &&!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);
    }

    public void søgende()
    {
        if (getWorld().getObjects(Player_1.class).size() == 1 || getWorld().getObjects(Player_2.class).size() == 1)
        {
            if (chased == null || chased.getWorld() == null) findPlayer();
            if (chased == null) return;
            turnTowards(chased.getX(), chased.getY());
        }
    }
As you can see, it will be removed if there are no players left in the game My act method:
public void act()
    {
        if (getWorld() == null) return;
        tid_metode();
        if (getWorld() == null) return;
        tid++;
        no_players();
        if (getWorld() == null) return;
        move(6);
        søgende();
        lookForPlayer_1();
        if (getWorld() == null) return;
        lookForPlayer_2();
        if (getWorld() == null) return;
    }
danpost danpost

2016/4/8

#
Remove lines 21, 22 and 26 from your last post. The 'findPlayer' method performs an equivalent check.
danpost danpost

2016/4/8

#
Now that I see the 'no_player' method, I suggest you remove it and any call to it and replace line 24 (in the 'søgende' method above) with this:
if (chased == null)
{
    getWorld().removeObject(this);
    return;
}
Bassusour Bassusour

2016/4/8

#
What do you mean by from my my last post? I would be nice if you were more specific about what code you want me to remove :) And again, I am really grateful for your help.
danpost danpost

2016/4/8

#
Bassusour wrote...
What do you mean by from my my last post? I would be nice if you were more specific about what code you want me to remove :)
Look at the code you most recently posted in this discussion thread. Find your 'søgende' method and go to line 24. Replace that line with the line I just gave above. I do not know how to any clearer (I thought I was clear enough before).
Bassusour Bassusour

2016/4/9

#
I'm sorry for being such an inconvinience, but this doesn't work either. Here's the change I did
public void søgende()
    {
            if (chased == null || chased.getWorld() == null) findPlayer();
            if (chased == null)
            {
                getWorld().removeObject(this);
                return;
            }
            turnTowards(chased.getX(), chased.getY());
    }
And i removed any calls to the no_player method, and removed it.
danpost danpost

2016/4/9

#
Again, just saying 'this doesn't work' is vague. What is it doing that it should not do? Are you getting any errors, and if so, what error are you getting and where is it occurring?
Bassusour Bassusour

2016/4/12

#
I get an error when a missile is targeting Player 1, when player 2 is not in the game. It says there's an error in the findPlayer. It is this part
chased = (Actor)getWorld().getObjects(Player_2.class).get(0);
I also get this error in the søgende method, in this part
if (chased == null || chased.getWorld() == null) findPlayer();
danpost danpost

2016/4/12

#
You will need to show the entire class code for the missile. Chances are that you are having the missile removed from the world before executing those lines.
Bassusour Bassusour

2016/4/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)  

/**  
 * This class defines a missile
 */
public class ND extends NPC
{
    private int tid = 0;
    private Actor chased;
    public void act()
    {
        søgende();
        if (getWorld() == null) return;
        tid_metode();
        if (getWorld() == null) return;
        tid++;
        move(6);
        lookForPlayer_1();
        if (getWorld() == null) return;
        lookForPlayer_2();
        if (getWorld() == null) return;
    }

    public void tid_metode()
    {
        if (tid == 60)
        {
            getWorld().removeObject(this);
        }
    }

    private void findPlayer()
    {
        chased = null;
        int choice = Greenfoot.getRandomNumber(2);
        if (choice == 0 &&!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);
    }

    public void søgende()
    {
            if (chased == null || chased.getWorld() == null) findPlayer();
            if (chased == null)
            {
                getWorld().removeObject(this);
                return;
            }
            turnTowards(chased.getX(), chased.getY());
    }

    /**
     * Check whether we have stumbled upon a crab.
     * If we have, eat it. If not, do nothing.
     */
    public void lookForPlayer_2()
    {
        if ( !getWorld().getObjects(Player_2.class).isEmpty() && isTouching(Player_2.class) && !getWorld().getObjects(ND.class).isEmpty())
        { 
            removeTouching(Player_2.class);
            getWorld().showText("Player 2 LOST!",80,310);
            Greenfoot.playSound("slurp.wav");
            getWorld().removeObject(this);
        }  
    }

    /**
     * Check whether we have stumbled upon player_1
     * If we have, eat it. If not, do nothing.
     */
    public void lookForPlayer_1()
    {
        if ( !getWorld().getObjects(Player_1.class).isEmpty() && isTouching(Player_1.class))
        {
            removeTouching(Player_1.class);
            getWorld().showText("Player 1 LOST!",80,280);
            Greenfoot.playSound("slurp.wav");
            getWorld().removeObject(this);
        }
    }  
}
danpost danpost

2016/4/12

#
I do not know if this is what is causing the problem (though, it very well may be), but the 'findPlayer' method you are showing is not exactly what I provided earlier. You modified it for some reason. Change it back to what I gave when I first created it. Please post the error trace (copy/pasting it) from the terminal window so that the specific error can be tracked down. It would be best to clear the terminal and re-create the error before copying the trace lines created so the specific error is captured from the terminal.
Bassusour Bassusour

2016/4/12

#
I really don't know why I would modify your code, but it helped quite a lot :) Now the only problem is the game crashing (The same error codes as I mentioned from my last post) when a missile is in the air, and both players are gone.
danpost danpost

2016/4/12

#
Bassusour wrote...
Now the only problem is the game crashing (The same error codes as I mentioned from my last post) when a missile is in the air, and both players are gone.
Again, you need to post the error trace. Do what I mentioned in my last post.
You need to login to post a reply.
1
2