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

2015/5/7

Whats wrong with my constructor?

janichu janichu

2015/5/7

#
Whats wrong with my Constructor? Hello, i got a Problem. I got two classes, one is the Archer:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Schwertkämpfer_2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bogenschütze extends Player1
{
    /**
     * Act - do whatever the Schwertkämpfer_2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int maxzugB;
    private int x1;
    private int y1;
    private int schussmodus = 0;
    public void act() 
    { 
        if(welcherSpieler()==1)
        {
           if(zug()==maxzugB)
           {
              move(8);
              getPosition(); 
              schussmodus = 1;
              while (schussmodus == 1) {
                  getSchussmodus();
              }
           }
        }
    }
    
    public Bogenschütze(int zug1B) {
        maxzugB = zug1B;
    }
    
    public void getPosition() {
        x1=getX();
        y1=getY();
    }
    
    public int getSchussmodus() {
        return schussmodus;
    }
    
    public int getXBogen() {
        return x1;
    }
    
    public int getYBogen() {
        return y1;
    }
    
    public void setSchussmodus(int pSchussmodus) {
        schussmodus = pSchussmodus;
    }
 }
And the Arrow
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PfeilP1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PfeilP1 extends Bogenschütze
{
    /**
     * Act - do whatever the PfeilP1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   private int maxzugB; 
    
   public void act() 
    {
        if(welcherSpieler()==1) {
            if(getSchussmodus()==1) {
               shoot();
            }
        }
    }
       
   public void shoot() {
       
        while(getSchussmodus() == 1) {
        
           if(Greenfoot.isKeyDown("right"))
           {  
               PfeilP1 PP1Rechts = new PfeilP1();
               getWorld().addObject(PP1Rechts,getXBogen(),getYBogen());
               
               for(int i=6; i >= 0; i--){
                  setLocation(getX()+1, getY());
                  Greenfoot.delay(10);
               }   
               setSchussmodus(0);
               zugende();
            }
            
           if(Greenfoot.isKeyDown("left"))
           {  
              PfeilP1 PP1Links = new PfeilP1(); 
              getWorld().addObject(PP1Links,getXBogen(),getYBogen());
              
              for(int i=6; i >= 0; i--){
                  setLocation(getX()-1, getY());
                  Greenfoot.delay(10);
               } 
               
              setSchussmodus(0);
              zugende();
            }
           
           if(Greenfoot.isKeyDown("up"))
           {   
               PfeilP1 PP1Hoch = new PfeilP1();
               getWorld().addObject(PP1Hoch,getXBogen(),getYBogen());
               
               for(int i=6; i >= 0; i--){
                  setLocation(getX(), getY()-1);
                  Greenfoot.delay(10);
               }  
               
               setSchussmodus(0);
               zugende();
            }
            
           if(Greenfoot.isKeyDown("down"))
           {   
               PfeilP1 PP1Runter = new PfeilP1();
               getWorld().addObject(PP1Runter,getXBogen(),getYBogen());
               
               for(int i=6; i >= 0; i--){
                  setLocation(getX(), getY()+1);
                  Greenfoot.delay(10);
               }  
               
               setSchussmodus(0);
               zugende();
            }
       }
    }
       public PfeilP1() {
       
   }


}
But now, when i compile it, it gives me the Problem
Constructor Bogenschütze in class Bogenschütze cannot be applied to given types; required: Int; found: no arguments; Actual and formal argument list differ in length.
If i now when i put a parameter in when i create a new PfeilP1, it gives me the same answer with "Required: No arguments; found: int;" or something like that. What am i doing wrong?
davmac davmac

2015/5/7

#
What am i doing wrong?
Well, the error is because the Archer constructor requires an 'int' parameter, and your Arrow class extends Archer so its constructor needs to call the Archer constructor, but you don't provide a parameter. You could add an explicit call to the superclass' constructor with a parameter, i.e.
super(0);
... inserted at the beginning of your Arrow constructor (line 87). However, is an Arrow a kind of Archer? It's not - so it's wrong that your Arrow class extends the Archer class at all. Most likely it should just extend Actor.
Super_Hippo Super_Hippo

2015/5/7

#
Ok, there are many things which aren't done as they should: An arrow should not extend the archer. The archer shoots the arrow, but the arrow isn't an archer. Don't create arrows from the arrow class. Let the archer shoot the arrows. You have an infinite loop here. I don't know what it should do, but this won't be correct!
              schussmodus = 1;
              while (schussmodus == 1) {
                  getSchussmodus();
              }
Not sure, but I think you misunderstood how Greenfoot is working. The act methods are executed in every object in the active world in every "frame" - called act cycle.
You need to login to post a reply.