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

2022/12/8

how can I pick up items and throw them away in the game

alex45254 alex45254

2022/12/8

#
I need a dude to pick up a duck and throw it somewhere else Does i need to use getOneIntersectingObject() or something else regards alex
danpost danpost

2022/12/8

#
alex45254 wrote...
I need a dude to pick up a duck and throw it somewhere else Does i need to use getOneIntersectingObject() or something else
The getOneIntersectingObject method would work just fine. The isTouching method would also work fine. What you will need, however, is a field to hold the held item:
private Actor held;
// or
private Duck duckHeld;
which will hold the actor being held. Its value should be null if not holding on to anything or should reference an actor being held. The image of the holder may or may not be changed to visually indicate that the item is being held; but, something, whether a gui actor or the image of the holder, should indicate whether it is being held or not.
alex45254 alex45254

2022/12/8

#
is it be like that? public void pickup(){ if(isTouching(duck.class)){ Actor actor = getOneIntersectingObject(duck.class); } }
alex45254 alex45254

2022/12/8

#
I understand what you said about held, but I don't understand how to implement it at all
danpost danpost

2022/12/9

#
alex45254 wrote...
is it be like that? public void pickup() { if (isTouching(duck.class)) { Actor actor = getOneIntersectingObject(duck.class); } }
Well, the actor would not want to pick one up if it was already holding one. So:
private void pickup()
{
    if (duckHeld == null)
    {
        duckHeld = getOneIntersectingObject(duck.class);
    }
}
You need to login to post a reply.