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

2013/12/4

Help with getObjectsInRange

Amature Amature

2013/12/4

#
Hey guys I am a sophpmore in high school and just started computer prgramming this year, right now I am making my first game in greenfoot, which will be a kind of Plants vs Zombies game just with kids as plans and parents as zombies. I am trying to figure out how the kid can tell when the parent is inside of the screen and if it is start shooting the Ball. This is what i have for the getObjectsInRange method. Though non of my attempt on different codes have worked. public boolean isParentInRange() { if (!getObjectsInRange(1200, Parent.class).isEmpty()){ return Parent != null; } then I dont know how to get further, just if it helps this is my ball code: public void act() { setLocation(getX() + speed, getY()); checkBoundaries(); destroyEnemies(); } public void checkBoundaries() { if(getX() > getWorld().getWidth() - 1) getWorld().removeObject(this); else if (getX() < 1) getWorld().removeObject(this); if (getY() > getWorld().getHeight() - 1) getWorld().removeObject(this); else if (getY() < 1) getWorld().removeObject(this); } public void destroyEnemies() { Actor Parent = getOneIntersectingObject(Parent.class); if(Parent != null) { getWorld().removeObject(Parent); getWorld().removeObject(this); } } private int speed = 5; }
danpost danpost

2013/12/4

#
Your 'isParentInRange' method tries to treat 'Parent' as a field name (in 'Parent != null') instead of a class name. Change the method to the following:
public boolean isParentInRange()
{
    return !getObjectsInRange(1200, Parent.class).isEmpty();
}
Amature Amature

2013/12/5

#
nice! thanks but how do I now make the Kid shoot once she knows the parent is in the picutre. Girl class is called WaterBallGirl
danpost danpost

2013/12/5

#
If you want the kid to shoot AT the parent, they you might want to return the Parent found (or null if none found) instead of a true/false which only indicates if one was found.
public Actor getParentInRange()
{
    if (getObjectsInRange(1200, Parent.class).isEmpty()) return null;
    return (Actor)getObjectsInRange(1200, Parent.class).get(0);
}
I renamed the method to indicate what it does. Now, you can code this to shoot:
public void shoot()
{
    Actor parent = getParentInRange();
    if (parent != null && getObjectsInRange(500, WaterBall.class).isEmpty())
    {
        WaterBall wb = new WaterBall();
        getWorld().addObject(wb, getX(), getY());
        wb.turnTowards(parent.getX(), parent.getY());
        wb.move(10);
    }
}
The second condition in line 4 prevent a WaterBall object from being created every act method (otherwise, you would have a stream of WaterBall objects being shot at the parent). I put line 9 in the code so the WaterBall object starts closer to the outer edge of the image of the girl.
Amature Amature

2013/12/5

#
thanks dude :) helped a lot, I probably will still get some more errors though haha looking forward to your advice :)
Amature Amature

2013/12/9

#
Is the public void shoot still in the class of the girl or in the class of the Ball? Also when i try to compile the first code you wrote, it says that null is an incompatible types? What do I do now?
danpost danpost

2013/12/9

#
The 'public void shoot' is in the class of the girl. As far as 'incompatible types', you need to show the code you are referring to and state which line the error occurs.
Amature Amature

2013/12/9

#
Alright thanks, so another problem i have now is that i want to make a kid spawn when i click on a spot on the screen. It should spawn there was the mouse was clicked. Is that possible?
You need to login to post a reply.