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

2014/4/30

Just a quick question about Radius Collision...

Firedudeet Firedudeet

2014/4/30

#
Hi, I'm working on my program for....stuff yeah. I'm making my class 'Astercore', and I'm currently using this for my collision:
    private void checkCollision()
    {
        Actor hit = getOneIntersectingObject(Shot.class);
        if (hit != null)
        {
            if (shotCount > 0)
            {
                getWorld().removeObject(hit);
                shotCount = shotCount - 1;
                resistShot.play();
            }
            if (shotCount <= 0)
            {
                getWorld().removeObject(hit);
                getWorld().removeObject(this);
            }
        }
    }
Now, I'm currently looking at a way to use: getObjectsInRange(int radius, java.lang.Class cls) Since I don't want the detection range to be the image itself, and rather have a radius detection range. However, I'm unsure how to use this method, since I have never used this before and I am unsure how I would use this in my own program. I have had a look at other discussions about this, but I'm still a little unsure, so I'm here just asking about it myself. :P So what I'm basically saying is, I need some help changing my getOneIntersectingObject to hopefully getObjectsInRange, so I can get rid of the Shot.class in a radius rather than an image square. If you need any other additional information, I'll try to provide what you ask for. (And slightly nervous while writing this since I don't usually ask for help from others ._.)
Zamoht Zamoht

2014/4/30

#
First of all import java.util.List. Then your code should look something like this:
private void checkCollision()  
{  
    List hits = getObjectsInRange(radius, Shot.class);
    if (!hits.isEmpty())  
    {  
        Actor hit = (Actor)(hits.get(0));
        if (shotCount > 0)  
        {  
            getWorld().removeObject(hit);  
            shotCount = shotCount - 1;  
            resistShot.play();  
        }  
        if (shotCount <= 0)  
        {  
            getWorld().removeObject(hit);  
            getWorld().removeObject(this);  
        }  
    }  
}  
Firedudeet Firedudeet

2014/4/30

#
Thank you for the quick reply, hit detection works exactly how I wanted to; thank you yet again.
You need to login to post a reply.