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

2018/6/26

Bug in Shooting

Vincent199000000 Vincent199000000

2018/6/26

#
Can somebody help me with this?
public void zerstöreDeutschgegner()
  
[code]public void zerstöreDeutschgegner()
    {
        Actor Deutschgegner = getOneIntersectingObject(Deutschgegner.class);
        if(Deutschgegner != null)
        {
            getWorld().removeObject(Deutschgegner);
            getWorld().removeObject(this);
        }
    }
If the Bullets hit this Enemy, the hole prgramm stop and i don`t why
danpost danpost

2018/6/26

#
Vincent199000000 wrote...
If the Bullets hit this Enemy, the hole prgramm stop and i don`t why
You are probably getting some terrminal error output. Please show (copy/paste) content.
Vincent199000000 Vincent199000000

2018/6/30

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


public class Rakete extends Actor
{
    private World myWorld;
    private boolean spaceDown;
    public void act() 
    {
        Aktionen();
        Hinderniserwischt();
        
    }    
    public void Aktionen()     
    {
        if(Greenfoot.isKeyDown("Up"))
        {
            setLocation(getX(),getY()-10);
        }
        
        if(Greenfoot.isKeyDown("Down"))
        {
            setLocation(getX(),getY()+10);
        }
        
        if(Greenfoot.isKeyDown("Right"))
        {
            setLocation(getX()+10,getY());
        }
        if(Greenfoot.isKeyDown("Left"))
        {
            setLocation(getX()-10,getY());
        }
        if(!spaceDown && Greenfoot.isKeyDown("space"))
        {
            spaceDown = true;
            Kugel kugel=new Kugel();
            getWorld() .addObject (new Kugel(), getX(), getY());
        }
        if(spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown = false;
        }
    }
    public void Hinderniserwischt()
    {
        Actor Hindernis = getOneIntersectingObject(Hindernis.class);
        if(Hindernis != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            Greenfoot.stop();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
        Actor d1 = getOneIntersectingObject(Deutschgegner.class);
        if(d1 != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            Greenfoot.stop();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
    }
    }

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

/**
 * Write a description of class CopyOfRaum here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Raum extends World
{
    HPLeiste hpleiste = new HPLeiste();
    private Deutschgegner d1;
    private Rakete r1;
    public Raum()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);  
        GreenfootImage img = new GreenfootImage(800,600);
        img.fill();
        setBackground(img);
        addSterne(500);
        addObject(new Rakete(), 100, 300);
        addObject(new HPLeiste(), 400, 25);
        addObject(new Boss(), 650, 300);
         
        
        
       
        Actor Rocket;
    }
    public HPLeiste getHPLeiste()
    {
        return hpleiste;
    }
    public void act()
    {
        if(Greenfoot.getRandomNumber(100)<2)
        {
            addObject(new Hindernis(), getWidth()-5, Greenfoot.getRandomNumber( getHeight()));
        }
        if(Greenfoot.getRandomNumber(100)<2)
        {
            addObject(new Deutschgegner(), getWidth()-5, Greenfoot.getRandomNumber( getHeight()));
        }
    }
    public void addSterne(int n)
    {
        for( int s=0; s<n; s++ )
        {
            int x = Greenfoot.getRandomNumber( getWidth());
            int y = Greenfoot.getRandomNumber( getHeight());
            addObject(new Sterne(), x, y);
        }
    }
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Sterne here. * * @author (your name) * @version (a version number or a date) */ public class Sterne extends Actor { private int speed =3; /** * Act - do whatever the Sterne wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation( getX()-speed, getY() ); if( getX()<speed ) { setLocation( getWorld().getWidth()-speed, getY()); } } public Sterne() { GreenfootImage img = new GreenfootImage(3,3); int c = Greenfoot.getRandomNumber(256); img.setColor(new Color(c,c,c)); img.fillOval(0,0,3,3); setImage(img); } }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends Actor
{
    /**
     * Act - do whatever the GameOver wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public GameOver()
    {
        setImage(new GreenfootImage("GameOver", 48, Color.WHITE, Color.BLACK));
        
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Kugel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Kugel extends Actor
{
    private int speed = 20;
    /**
     * Act - do whatever the Kugel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        zerstöreDeutschgegner();
        setLocation(getX()+speed, getY());
        if( getX()>getWorld().getWidth()-speed)
        {
            getWorld().removeObject(this);
        }
    }   
    public void zerstöreDeutschgegner()
    {
        Actor Deutschgegner = getOneIntersectingObject(Deutschgegner.class);
        if(Deutschgegner != null)
        {
            getWorld().removeObject(Deutschgegner);
            getWorld().removeObject(this);
        }
    }
}

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

/**
 * Write a description of class Deutschgegner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Deutschgegner extends Actor
{
    private int speed = 3;
    /**
     * Act - do whatever the Deutschgegner wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Bewegdich();   
        ZerstöreRakete();
        setLocation( getX()-speed, getY() );
        if( getX()<speed )
        {
            getWorld().removeObject( this );
        }
    }    
    public void Bewegdich()
    {
       /* 
        if(Greenfoot.getRandomNumber(100)<10)
        {
            turn(Greenfoot.getRandomNumber(90));
        }
        */
    }
    public void ZerstöreRakete()
    {
        Actor Rakete = getOneIntersectingObject(Rakete.class);
        if(Rakete != null)
        {
            getWorld().removeObject(Rakete);
            getWorld().removeObject(this);
        }
    }
}
import greenfoot.*;

/**
 * Write a description of class Hindernis here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hindernis extends Actor
{
    private int speed = 3;
    /**
     * Act - do whatever the Hindernis wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       ZerstöreRakete();
       setLocation( getX()-speed, getY() );
        if( getX()<speed )
        {
            getWorld().removeObject( this );
        }
    }    
    
     public void ZerstöreRakete()
    {
        Actor Rakete = getOneIntersectingObject(Rakete.class);
        if(Rakete != null)
        {
            getWorld().removeObject(Rakete);
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2018/6/30

#
Line 19 in the Kugel class gets the x- and y-coordinates of the actor -- or, at least it tries to. The line will fail if the actor is not in a world -- and it will not be in a world if the bullet hits a Rakete object (by way of the call on line 18 to the zerstöreDeutschgegner method. Insert the following at line 19 (after the call):
if (getWorld()== null) return;
You will need to do similarly in the Hindernis and Deutschgegner classes, as well. The same type situation may also happen in the Rakete class; you can use and "else if" in the Hinderniserwischt method to avoid the issue there.
You need to login to post a reply.