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

2014/5/4

Help with getOneIntersectingObject

nc-munk nc-munk

2014/5/4

#
when hit it dont remove the spaceship or add the explosion code
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 */
public class SpaceShip extends SmoothMover
{
    private static final int gunReloadTime = 5;
    
    private int reloadDelayCount;  
    
    private GreenfootImage spaceship = new GreenfootImage("Spaceship.png");
    private GreenfootImage spaceshipRight = new GreenfootImage("spaceship right.png");
    private GreenfootImage spaceshipLeft = new GreenfootImage("spaceship left.png");
        /**
     * Initilise this rocket.
     */
    public SpaceShip()
    {
        reloadDelayCount = 5;
    }
    public void act () 
    {
        GreenfootImage[] images = { spaceshipLeft, spaceship, spaceshipRight };
        int dx = 0;
        if (Greenfoot.isKeyDown("right")) dx++; 
        if (Greenfoot.isKeyDown("left")) dx--;
        setLocation (getX() + 3*dx, getY());
        setImage(images[dx+1]);
         if ("space".equals(Greenfoot.getKey()))
        {
            fire();
        }
    }
    private void fire()
    {
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX(), getY());
        bullet.setRotation(-90);
    }
    /**
     * Check to see if the rocket has collided with an asteroid.
     */
    private void checkCollision()
    {
        Actor Asteroid = getOneIntersectingObject(Asteroid.class);
        if (Asteroid != null)
        {
            getWorld().addObject( new Explosion(), getX(), getY() );
            //Cyberspace Cyberspace = (Cyberspace) getWorld();
            getWorld().removeObject(this);
            //Cyberspace.gameOver();
        }
    }
}
Zamoht Zamoht

2014/5/4

#
You have to put checkCollision() in the act method.
nc-munk nc-munk

2014/5/4

#
ahh of course
nc-munk nc-munk

2014/5/4

#
then the explosion starts the game pause
Zamoht Zamoht

2014/5/4

#
Did the game stop due to an error?
danpost danpost

2014/5/4

#
Where in the act method did you put the 'checkCollision' call?
nc-munk nc-munk

2014/5/4

#
no not an error
public void act () 
    {
        checkCollision();
        GreenfootImage[] images = { spaceshipLeft, spaceship, spaceshipRight };
        int dx = 0;
        if (Greenfoot.isKeyDown("right")) dx++; 
        if (Greenfoot.isKeyDown("left")) dx--;
        setLocation (getX() + 3*dx, getY());
        setImage(images[dx+1]);
         if ("space".equals(Greenfoot.getKey()))
        {
            fire();
        }
    }
Zamoht Zamoht

2014/5/4

#
Pretty sure you get an error trying use setLocation after the actor has been removed from the world. Try to place checkCollision at the bottom of the act method.
nc-munk nc-munk

2014/5/4

#
thanks
You need to login to post a reply.