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

2012/9/22

Asteroid Question

mncomic mncomic

2012/9/22

#
Trying to get have asteroid destroyed on one bullet...any ideas? public void hit(int damage) { stability = stability - damage; if(stability <= 0) explode();
danpost danpost

2012/9/23

#
If the asteroid is to explode upon the first bullet to 'hit' it, then 'damage' and 'stability' are irrelavent. Just check for intersecting bullet in asteroid act method. If found, remove bullet and explode asteroid. The code can also be placed within a method the act method calls. It is sort of like the Wombats seeLeave()/eatLeave() theme; instead, it will be seeBullet()/(eatBullet() and explode()).
mncomic mncomic

2012/9/24

#
Dan... Appreciate the reply...could you spell it out a little more for me...I'm new to this...ha...thanks Pal
Gevater_Tod4711 Gevater_Tod4711

2012/9/24

#
You just have to write a code like the following in your class bullet:
public class Bullet ... {
    
    public void act() {
        ...
        if (getWorld().getOneObjectAtOffset(0, 0, Asteriod.class).size() != 0) {
        getWorld().removeObject(getWorld().getOneObjectAtOffset(0, 0, Asteroid.class)); //removing the asteroid;
            getWorld().removeObject(this); //remove the bullet.
            return; //not necessary but if other code follows this there might be Exceptions.
        }
        ...
    }
}
If you add this code it should work.
danpost danpost

2012/9/24

#
@Gevater_Tod4711, 'getOneObjectAtOffset(int, int, class)' is a method in the Actor class. Therefore, 'getWorld()' should not prefix it. Also, 'getObjectsInRange(int, class)' would probably be a better choice of methods (since the Asteroid is fairly round) and with 'getOneObjectAtOffset(0, 0, Bullet.class)' the image of the bullet must contact the center of the asteroid to return 'true', which is unrealistic. Oh, well, I see you had put the code in the Bullet class (which is not so bad, then); but still, it is the asteroid that should check for bullet and explode when found (not the bullet that should check for asteroid). The only thing the bullet should be doing is moving and checking for world edges to remove self.
danpost danpost

2012/9/24

#
@mncomic, 'getObjectsInRange(int, class)' will return a 'list' object with which you will have to check to see that the list is not empty before attempting to get a bullet from the list:
// in the Asteroid act method
if (seeBullet())
{
    eatBullet();
    explode();
    return;
}
// methods
private boolean seeBullet()
{
    return !getObjectsInRange(getWidth() / 2, Bullet.class).isEmpty();
}

private eatBullet()
{
    getWorld().removeObject((Bullet) getObjectsInRange(getWidth() / 2, Bullet.class).get(0));
}
The most commonly used methods that work on list objects are: (1) isEmpty(): return true if the list is empty else false; (2) size(): returns the number of objects returned in the list; and (3) get(int): returns from the list the object at the specified index (the index of the first item being zero) You can find the documentation here.
mncomic mncomic

2012/9/24

#
Thanks for all the info...still confused...I appreciate the help...will try to decipher what's been posted...
danpost danpost

2012/9/24

#
Sorry, I see I forgot to put the return type in the 'eatBullet()' method declaration (line 14). Should be
private void eatBullet()
mncomic mncomic

2012/9/24

#
do I have to create the eatBullet() method...I don't see it
danpost danpost

2012/9/24

#
My last post was in reference to my post previous to that.
You need to login to post a reply.