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

2016/6/9

This throws up an error help please

SiWeller SiWeller

2016/6/9

#
private void Enemyhit() { Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class); if (enemy != null) { getWorld().removeObject(this); } else { return; } }
SiWeller SiWeller

2016/6/9

#
/**
*This is the code i have for each class.
*/
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(1);
        destroy();
    }
    public void destroy() //desttroy functon
    {
        Actor Ammo = getOneIntersectingObject(Ammo.class);
        if(Ammo != null)
        {
        getWorld().removeObject(this); //remove the instance
        }
    }
}


/**
*Ammo
*/
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ammo here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ammo extends Actor
{
    /**
     * Act - do whatever the Ammo wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Removeammo();
        Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
        if (enemy != null)
        {
            Enemyhit();
        }
        if (enemy == null)
        {
            Move();
        }
    }
    private void Move()
    {
        move(5);
    }
    private void Enemyhit()
    {
        Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
        if (enemy != null)
        {
            getWorld().removeObject(this);
            return;
        }
    }
    
    private void Removeammo()
    {
     if (getY()<10) getWorld().removeObject(this);
    }
}

/**
*Rocket
*/ 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("left"))
        {
            move(-4);// Add your action code here.
        }
        
        if (Greenfoot.isKeyDown("right"))
        {
            move(4);// Add your action code here.
        }
        
        if("space".equals(Greenfoot.getKey()))
        {
            fire();
        }
    }   
    
    private void fire()
    {
        Ammo ammo = new Ammo();
        getWorld().addObject(ammo, getX(), getY());
        ammo.setRotation(270);
        ammo.move(50);
    
    }
}
danpost danpost

2016/6/9

#
What error does it "throw up"? (show entire error message)
SiWeller SiWeller

2016/6/9

#
its ok now thanks anyways
You need to login to post a reply.