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

2020/3/4

Score Counter

hr17_ hr17_

2020/3/4

#
Hi, I have a problem with my score counter. The "Blasterschuss" class is a laser class. When firing the laser there is an error when it leaves the world. I don't know what the error is, just where it is. Please help me!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Blasterschuss here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Blasterschuss extends Actor
{
    /**
     * Act - do whatever the Blasterschuss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int BlasterschussSpeed = 10; 
    private boolean removed = false;
    static boolean TieKilled = false;
    boolean BlasterschussKilled = false;
    public void act() 
    {
        setLocation(getX(), getY()-BlasterschussSpeed);
        removed = true;
        if(getY() < 2) {
            getWorld().removeObject(this);
            removed = true;
        }
        /*
        if (!removed && canSee(Tie.class)); {     
             eat(Tie.class); //der tie wird bereits in der Tie-Klasse removed. da der tie nicht 2 mal removed werden kann, bekommst du hier den error.
        } */
   
        if(!TieKilled) {
            if (canSee (Tie.class)){
            
             eat1(Tie.class);
             
              
           
           }
        }
        
         if(Tie.class != null)
        {
          Hintergrund1 hintergrund1 = (Hintergrund1)getWorld();
          Counter counter = hintergrund1.getCounter();  - '{' - Here is something wrong
          counter.addScore();
        }
        
        
            
    }    
    
    
     
    public void eat(Class clss) {  
        Actor Tie = getOneObjectAtOffset(0, 0, clss);  
        if(Tie != null) {  
            getWorld().removeObject(Tie); 
        }   
    }
    
    public boolean canSee(Class clss) {  
        if(this!=null) {
            return false;
        } else {
            return getOneObjectAtOffset(0, 0, clss) != null;  
        }
    }  
    public void eat1(Class clss) {  
        Actor xWing = getOneObjectAtOffset(0, 0, clss);  
        if(xWing != null) {  
            getWorld().removeObject(xWing);
        }  
          
    
        
    
    }
       private void die() 
      { 
           Greenfoot.playSound("au.wav");
           getWorld().removeObject(this);
           
           
          
    }
   
}
hr17_ hr17_

2020/3/4

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

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int score = 0;
    public Counter()
    {
    
     setImage (new GreenfootImage("Score: " + score, 40, Color.WHITE, Color.BLACK));
    
    }
    
    
    public void act() 
    {
        setImage (new GreenfootImage("Score: " + score, 40, Color.WHITE, Color.BLACK));
    
    }    
    public void addScore()
    {
     score++;
    
    
    }
}
hr17_ hr17_

2020/3/4

#
The first code is the laser class, which shold kill the Tie.class und the kills shold be count.
hr17_ hr17_

2020/3/4

#
This ist the world class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Hintergrund1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Hintergrund1 extends World

    
{
   
    int imageCount = 0;
    int spawnRate = 1;
    
    GreenfootImage bgImage = new GreenfootImage("NeuerBackground.jpg");
    Counter counter = new Counter();
    /**
     * Constructor for objects of class Hintergrund1.
     * 
     */
    
    public Hintergrund1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(850, 650, 1,true);
        prepare();
        
        
    }
    public Counter getCounter()
    {
     return counter;
    
    }
    
    public void act() {
        imageCount -=1;
        
        if(imageCount < -bgImage.getHeight()) {
            imageCount += bgImage.getHeight();
        }
        int temp = imageCount;
        getBackground().drawImage(bgImage, 0, -temp);
        getBackground().drawImage(bgImage, 0, -(temp + bgImage.getHeight()));
        
        
    }
    

    
    public void loescheObjekte()
       {
        
           List objekte = getObjects(null);
           removeObjects(objekte);
           
        
        
        
    }
    
    private void prepare()
    {
     addObject(new xWing(),425,570);
        addObject(new Tie(),Greenfoot.getRandomNumber(850),50); 
        addObject(new Tie(),Greenfoot.getRandomNumber(850),50);
        addObject(new Tie(),Greenfoot.getRandomNumber(850),50);
        addObject(new Tie(),Greenfoot.getRandomNumber(850),50);
        addObject(new Tie(),Greenfoot.getRandomNumber(850),50);
        addObject(new Tie(),Greenfoot.getRandomNumber(850),50);
        addObject(new Wand(),850,50);
        addObject(new Counter(),100, 40);
    
    
    
    }
    
}
hr17_ hr17_

2020/3/4

#
This is the Tie.class, which should be killed...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tie extends Actor
{
    /**
     * Act - do whatever the Tie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     boolean TieKilled = false;
     boolean xWingKilled = false;
     boolean BlasterschussKilled = false;
     private int TieSpeed = 2;
    
    public void act() 
    {
        setLocation(getX(), getY()+TieSpeed);
        TieKilled = false;
        
        if(getObjectsInRange(40, Blasterschuss.class).size() >= 1) {
            getWorld().removeObject(this);
            TieKilled = true;
        }
        
        if(!TieKilled && getY() > 850) {
            getWorld().removeObject(this);
            xWingKilled = true;
        }
        
        if(!xWingKilled) {
            if (canSee (xWing.class)){
            
             eat1(xWing.class);
            
            }
        }
        
        if(atWorldEdge()){
        
         getWorld().removeObject(Tie.this);
        
        
        }
        
       
    }   
        
       
    public void eat1(Class clss) {  
        Actor xWing = getOneObjectAtOffset(0, 0, clss);  
        if(xWing != null) {  
            getWorld().removeObject(xWing);
        }  
          
    
        
    
    }
    public boolean canSee(Class clss) {
        if(TieKilled) {
            return false;
        } else {
            return getOneObjectAtOffset(0, 0, clss) != null;
        }
    }  
    
    public boolean atWorldEdge()
    {  
        if(TieKilled) {
            return false;
        } else {
            if(getX() < 10 || getX() > getWorld().getWidth() - 10)  
                return true;  
            if(getY() < 10 || getY() > getWorld().getHeight() - 10)  
                return true;  
            else 
                return false;
            }
    }  
 }
    
danpost danpost

2020/3/4

#
I think line 32 is the problem (which follows line 31 -- see what it does).
You need to login to post a reply.