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

2014/7/20

Explotion class not removeing other class.

efx22 efx22

2014/7/20

#
I'm having a couple issues. 1) My "Explosion" is way too sensitive. In my game I have a missile/s that are being propelled toward a ship. the problem is that even at what I would consider a safe distance from the missile the explosion occurs anyways. How would I decrease the blast area? I can post code if need be. 2) My missile actor creates an explosion and removes itself but it will not remove my ship "Hero" actor. Again I can post code if need be. Thanks in advance for any help.
NikZ NikZ

2014/7/20

#
Are you trying to have the missile explode when it is close to the ship only?
danpost danpost

2014/7/20

#
Issue one may be due to the size of your GreenfootImage objects compared to the size of the non-transparent part of the image. That is, you may have excess transparency around your image(s) that will intersect well before the actual non-transparent parts of the images intersect. If this be the case, you should remove the excess transparency around all your images. Issue two is too open to supposition as explained. All I can say about it is that the hero should be removed before itself. Other than that, the error message you are getting, if any, and the relevant code should be posted for further assistance (copy/paste any error messages in their entirety and use the 'code' tag below the 'Post a reply' box for inserting code into your post).
efx22 efx22

2014/7/23

#
Thanks for the quick replay NikZ & danpost. I will post code later as I am away from my workstation right now.
efx22 efx22

2014/7/24

#
Thanks in advance for your help Explosion code is as follows
public class Explosion extends Actor
{
    /** How many images should be used in the animation of the explostion */
    private final static int IMAGE_COUNT= 8;

    /** 
     * The images in the explosion. This is static so the images are not
     * recreated for every object.
     */
    private static GreenfootImage[] images;

    /** Current size of the explosion */
    private int size=0;

    /** How much do we increment the index in the explosion animation. */
    private int increment=1; 

    public Explosion() {

        initialiseImages();
        setImage(images[0]);        
        Greenfoot.playSound("explosion.wav");

    }

    /** 
     * Create the images for explosion.
     */
    public synchronized static void initialiseImages() {
        if(images == null) {
            GreenfootImage baseImage = new GreenfootImage("explosion.png");
            int maxSize = baseImage.getWidth()*8;
            int delta = maxSize / (IMAGE_COUNT+1);
            int size = 0;
            images = new GreenfootImage[IMAGE_COUNT];
            for(int i=0; i < IMAGE_COUNT; i++) {
                size = size + delta;
                images[i] = new GreenfootImage(baseImage);
                images[i].scale(size, size);
            }
        }
    }

    /**
     * EXPLODE!
     */
    public void act() 
    {
        setImage(images[size]);

        size += increment;
        if(size>=IMAGE_COUNT) {
            increment = -increment;
            size += increment;
        }
        
        explodeOthers();
        if(size <= 0) {
            getWorld().removeObject(this);
        }
    }

    /**
     * Explodes all intersecting objects.
     */
    private void explodeOthers() 
    {        
        List explodeEm = getIntersectingObjects(null);        
        Iterator i = explodeEm.iterator();
        
        }
    }
The Hero code is as follows
public class Hero extends Actor
{
    /**
     * Act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
    }    

    /**
     *Check wich key is being pressed
     */   

    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-4);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+4);
        }

    }      
}
Finally the missile code is the following
public class Missile extends Actor
{
    /**
     * Act - do whatever the Missile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(4);
        if(getOneIntersectingObject(Hero.class) != null) {
            explode();
        } 
        
    }

    /**
     * Make an explosion.
     */
    private void explode() {
        Space w;
        List hero = getWorld().getObjects(Hero.class);

        getWorld().addObject(new Explosion(), getX(), getY());
        getWorld().removeObject(this);
    }

    
}
danpost danpost

2014/7/24

#
In the act method of your Missile class you are checking to see if a Hero object intersects the missile. The method call 'getOneIntersectingObject(Hero.class)' returns that Hero object you need to remove. Replace line 21 with an actor removal statement that removes that Hero object. You can also remove line 20, as you are not actually using 'w' anywhere (nor are you even assigning anything to it).
You need to login to post a reply.