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

2013/8/28

Object hits an object

dominicarrojado dominicarrojado

2013/8/28

#
What code can I use if for example, Object A is the player, Object B is a moving object. If Object A hits Object B it can either be that Object A will be remove or Object A is respawned to its initial location.
davmac davmac

2013/8/28

#
I recommend you read through the tutorials in the documentation section of this site. In particular tutorial #3 covers what you are asking.
Gevater_Tod4711 Gevater_Tod4711

2013/8/28

#
To check whether object A touches object B you can use this method:
//in your A class;
public boolean touchingObjectB() {
    return !getOneIntersectingObject(B.class).isEmpty();
}
//instead of B.class you have to use the real classname;
Now you can check whether object A touches object B in your act method. You just need to know whether you want to remove object A when it touches B or to respawn. You can use this code to handle this:
//again in your class A;
public void act() {
    if (touchingObjectB()) {
        if (respawn) {//you have to know whether you want to remove or respawn;
            //here your actor is respawned;
            getWorld().addObject(new A(), 5, 5);//here you have to use the right classname and the right coordinates for the respawn;
            getWorld().removeObject(this);
        }
        else {
            //here your object just gets removed;
            getWorld().removeObject(this);
        }
    }
}
Zamoht Zamoht

2013/8/28

#
Wouldn't the touchingObjectB method be:
public boolean touchingObjectB() {  
     return !getIntersectingObjects(B.class).isEmpty();  
}  
or
public boolean touchingObjectB() {  
    return (getOneIntersectingObject(B.class) != null);
}  
Or am I wrong?
Gevater_Tod4711 Gevater_Tod4711

2013/8/28

#
@Zamoht Yes, thanks for the advice. I mixed up the method names. getIntersectingObjects(B.class) is the right method name.
davmac davmac

2013/8/28

#
You don't need to use getIntersectingObjects or getOneIntersectingObject at all; you can use the isTouching(...) method.
danpost danpost

2013/8/28

#
davmac wrote...
You don't need to use getIntersectingObjects or getOneIntersectingObject at all; you can use the isTouching(...) method.
This is provided you are using Greenfoot version 2.3.0. I am still using the USB version 2.2.1.
davmac davmac

2013/8/28

#
Ah, that gave me the kick I needed to get the USB version updated. Get it now from the download page - it includes BlueJ 3.1.0, Greenfoot 2.3.0, JDK 7, and removes the requirement to unzip at the root of the storage device.
danpost danpost

2013/8/28

#
@davmac, thanks. Are there, or have there been any issues I should be aware of with Greenfoot 2.3.0? Have the issues, if any, found with the version been corrected in the USB version?
davmac davmac

2013/8/28

#
There's a list of known bugs, including those that have been fixed since the 2.3.0 release, here: http://bugs.bluej.org/greenfoot/report/11 However, the USB version is identical to the 2.3.0 release except for minor changes in the launcher executable, that is to say it does not include the fixes that have been made since. I don't believe there any issues significant enough to warrant staying at version 2.2.1, particularly since several bugs were fixed between 2.2.1 and 2.3.0. See here (under the 2.3.0 milestone): http://bugs.bluej.org/greenfoot/report/6
You need to login to post a reply.