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

2016/9/28

Objects have no hitbox

LimeLava LimeLava

2016/9/28

#
I can add code if you need it but i have an object that is controlled by my mouse and some object dropping from the top of the screen and when the object touches the object that falls from the top of the screen it will remove it. The problem i am having is that when the falling object touches the "platform" that is catching it it only removes it if it is in the area where my mouse is and not on the side edges of the object.
Super_Hippo Super_Hippo

2016/9/28

#
Which intersection method did you use? Showing the code would probably be the easiest.
LimeLava LimeLava

2016/9/30

#
Here is the code.
    public void remove()
    {
        Actor ball;
        ball = getOneObjectAtOffset(0,0, Ball.class);
        {
            World world;
            world = getWorld();
            world.removeObject(ball);
        }
    }       
This is the code for the platform and the Ball.class is the object that is falling and i want to remove when it touches the platform. feel free to let me know if you need code for anything else.
danpost danpost

2016/9/30

#
If you looked at the documentation of the Actor class API, you would see that the 'getOneObjectAtOffset' method will check for any object of the given type at the specific offset given -- a single cell. There are other methods of the class that perform differently. Please refer to the Actor class API documentation and find one that is more suited to what you want.
LimeLava LimeLava

2016/10/8

#
I have tried a couple different things now Ex. isTouching and !getIntersectingObjects(Ball.class).isEmpty() but I still have the same problem. I wouldn't say I am that experienced. Could I get a little more direction on this? thanks,
danpost danpost

2016/10/8

#
LimeLava wrote...
I have tried a couple different things now Ex. isTouching and !getIntersectingObjects(Ball.class).isEmpty() but I still have the same problem. I wouldn't say I am that experienced. Could I get a little more direction on this? thanks,
Where are you trying to code it and what code (class name and method name; also, where called from, if not 'act' method), exactly, did you use? I presume you tried coding it in the class of the platform using:
if (isTouching(Ball.class))
{
    /** some code here  */
}
but, making all explicitly known is the best course of action when posting an issue -- for better and faster fixes.
LimeLava LimeLava

2016/10/9

#
I have been putting the isTouching and stuff in the Ball class (the object falling from the sky). I have currently ben using the isTouching and it does the thing like is said above but i am also receiving this error. "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed." This is the code for the Ball where i have it removing the object if it touches the platform (Catcher.class)
        return true;
        else
            return false;
    }
    protected void addedToWorld(World world)
    {
       if ( ! getObjectsInRange(107, Ball.class).isEmpty() )
       {
        world.removeObject(this);
       }
LimeLava LimeLava

2016/10/9

#
that is the wrong code sorry. my Greenfoot is having some problems. this is the real code.
    public void remove()
    {
        if (atWorldEdge())
        {
            getWorld().removeObject(this);
        }
        if (isTouching(Catcher.class))
        {
            removeTouching(Ball.class);
        }
            
    }
Super_Hippo Super_Hippo

2016/10/9

#
Start line 7 with an 'else if' or use 'return;' after line 5. Or just put lines 7 through 9 before line 3.
LimeLava LimeLava

2016/10/10

#
Thanks i don't get the error anymore but i could still use some help with removing the objects. If anyone has any expertise or suggestions to share i would really appreciate it.
danpost danpost

2016/10/10

#
You need to explain what you are trying to remove -- and when (what conditions are required for removal). Then you need to show your attempted code, informing us as to which class the code is currently in. From the above code, it seems strange to look for an intersecting Chaser object and then remove a Ball object when a Chaser object intersects 'this' object. It seems that the code is placed in the Ball class. As such, the code would be executing for the Ball object in the world -- and if only one Ball object is in the world at a time, then there would never be any intersecting Ball objects. If it is the case that the code was indeed in the Ball class, then you can remove the Ball object that the code is executing for with:
// possible line 9 replacement
getWorld().removeObject(this);
This would then result in this code (with the suggested 'else'
public void remove()
{
    if (atWorldEdge())
    {
        getWorld().removeObject(this);
    }
    else if (isTouching(Catcher.class))
    {
        getWorld().removeObject(this);
    }       
}
This can be more simply written as follows:
public void remove()
{
    if (isTouching(Catcher.class) || atWorldEdge())
    {
        getWorld().removeObject(this);
    }      
}
LimeLava LimeLava

2016/10/10

#
Thank you for all your help Hippo and Dan, it works now. Thanks for all of the help.
You need to login to post a reply.