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

2011/5/26

Object Interaction

gctaastudents gctaastudents

2011/5/26

#
Hi, we are new to greenfoot, and we were wondering how to have two objects interacting. For example, we have one object shooting bullets, but we do not know how to have those bullets hit and affect another object. Is there a code or method to help us do this?
davmac davmac

2011/5/26

#
There is a youtube tutorial about shooting:
davmac davmac

2011/5/27

#
... except that doesn't really explain how to interact with other objects, that you hit, sorry. You can use the getOneIntersectingObject method from the bullet to discover whether it has hit anything. You can pass in "null" as the argument to check for any collision, or you can pass a class literal to check for collisions with a specific type of actor. For instance, if you have a "Brick" class and want to check for collisions with Brick objects, you can use (in the act method of the Bullet class):
    Brick hitBrick = (Brick) getOneIntersectingObject(Brick.class);
    if (hitBrick != null) {
        // A brick was hit!
        // You can now call methods on the brick to interact with it.
        brick.setLocation(brick.getX(), brick.getY() + 5);
        // The above line is just an example.
        // It moves the brick down slightly.
    }
I hope that makes sense.
zoukm@hotmail.com zoukm@hotmail.com

2013/10/16

#
Hello Every one, I have trouble making an object open and step in to and out of a car. I need the codes that can make this work for me. Thanks in advance for your help. zouk
davmac davmac

2013/10/16

#
Please create a new discussion topic instead of hi-jacking this one.
zoukm@hotmail.com zoukm@hotmail.com

2013/10/17

#
Sorry, my mistake.
You need to login to post a reply.