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

2012/5/18

Picking Up Objects

Denzien Denzien

2012/5/18

#
Ok so for picking up an object, I don't want to use getOneObjectAtOffset(0, 0, <actor>.class); as this only picks it up at the centre, so I tried this:
    public void getCoin()
    {
        Actor Coin;
        Coin = getObjectsInRange(16, Coin.class);
        if (Coin != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Coin);
            addMoney();
        }
    }
But it doesn't work, so how would I do it? (and I can see how it doesn't work, just not how to make somthing that does work).
needcodehelp needcodehelp

2012/5/18

#
I am also having a similar problem. I want my item to collect other objects once it touches another object, not just in the center. THANKS!
Denzien Denzien

2012/5/21

#
Anyone?
IsVarious IsVarious

2012/5/21

#
The code you want will be detecting another object/actor at it's bounding boxes. I would do a google search for something in java, along these lines. Also, you could have your main actor,(the one you want to pick up items) just eat the other actors, and keep track of it with a variable for everytime it eats an object it ads 1 to that variable.
kartikitrak kartikitrak

2012/5/22

#
You could use intersecting object method. This method allows you to return a value if one object touches the coin object. Simply put this in the coin class and call for it in the act() of actor that collects it... I presume.
public boolean collectCoin()
    {
        Actor coinTouched= getOneIntersectingObject(Coin.class);
        return coinTouched!=null;
    }
Denzien Denzien

2012/5/23

#
Oh yeah, I could make the coin remove itself rather than the character remove itself :3 ok I don't know why I didn't think of that before
danpost danpost

2012/5/23

#
@kartikitrak, it appears you want code in the Coin class, for a coin to look for an intersecting coin; which, of course, is not what is wanted. In the class of the actor that is to collect the item, add the following method:
public boolean collectItem(Class cls)
{
if (getOneIntersectingObject(cls) == null) return false;
getWorld().removeObject(getOneIntersectingObject(cls));
return true;
}
and call it from the act method with (example is for coins):
if (collectItem(Coin.class)) coinsCollected++;
You need to login to post a reply.