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

2016/10/21

Getting the coordinates of an object

1
2
danpost danpost

2016/11/9

#
LimeLava wrote...
if i wanted to remove the second and third catcher but only after the one before it was removed would i just replace all the places there it says catcher with the name of the second catcher (Catcher2) and then do it again for the third one (Catcher3)?
If I understand what you are saying and those are different class names -- then, yes. You could try first (before asking), you know. You may need to rearrange you code a little and use 'else' on 'catcher != null' to continue with the Catcher2 object; and then the same with that to continue with the Catcher3 object.
LimeLava LimeLava

2016/11/14

#
I did what I knew how to do and what I understood from what you said and ended up with this.
    public void removeCatcher2()
    {
        if (!getObjects(Catcher2.class).isEmpty())
        {
            Ball ball = (Ball)getObjects(Ball.class).get(0);
            Catcher2 catcher2 = (Catcher2)getObjects(Catcher2.class).get(0);
            if (ball.getY() >= 380)
            {
                removeObject(catcher2);
            }
        }
        
    }
This is right after my original one but i got an error for IndexOutOfBoundsExeption for these two lines
            Ball ball = (Ball)getObjects(Ball.class).get(0);
and
removeCatcher2();
The code above is from the act method and it right after the first removeCatcher.
danpost danpost

2016/11/14

#
LimeLava wrote...
if i wanted to remove the second and third catcher but only after the one before it was removed would i just replace all the places there it says catcher with the name of the second catcher (Catcher2) and then do it again for the third one (Catcher3)?
If you did that, one 'remove' method would not know whether another removed a catcher or not. Better would be to create just one method to deal with all catchers:
private void removeCatcher()
{
    if (getObjects(Ball.class).isEmpty()) return; // cannot remove catcher if no ball in world
    Actor ball = (Actor)getObjects(Ball.class).get(0);
    if (ball.getY() < 300) return; // ball not in position to remove catcher
    Actor catcher = null;
    if (!getObjects(Catcher.class).isEmpty()) catcher = getObjects(Catcher.class).get(0);
    else if (!getObjects(Catcher2.class).isEmpty()) catcher = getObjects(Catcher2.class).get(0);
    else if (!getObjects(Catcher3.class).isEmpty()) catcher = getObjects(Catcher3.class).get(0);
    if (catcher != null)
    {
        removeObject(catcher);
        removeObject(ball);
    }
}
LimeLava LimeLava

2016/11/15

#
I had a problem with the code where for this line
if (!getObjects(Catcher.class).isEmpty()) catcher = getObjects(Catcher.class).get(0);
Said incompatible types for "(0)"
danpost danpost

2016/11/15

#
LimeLava wrote...
I had a problem with the code where for this
if (!getObjects(Catcher.class).isEmpty()) catcher = getObjects(Catcher.class).get(0);
Said incompatible types for "(0)"
The compiler only knows the element as of type Object and I forgot to tell it that the object was an Actor object. Change the line to this:
if ( ! getObjects(Catcher.class).isEmpty()) catcher = (Actor)getObjects(Catcher.class).get(0);
You need to login to post a reply.
1
2