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

2013/5/12

actual and formal arguments lists differ in length

LittleJacob LittleJacob

2013/5/12

#
While compiling i get this error : constructer laser in class laser cannot be applied to given types; required: Counter required: no arguments reason: actual and formal arguments lists differ in length I would be really thankful if anybody could help me out. I am quite new to greenfoot
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
{
    private int speed = 3;


   
    public void act() 
    {
        if(Greenfoot.isKeyDown("left"))
        {
            move(0 - speed);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            move(speed);
        }
        
     schietAlien();
    }    
    
        /**
  
  */  
 public void schietAlien()
  { String knop;

        knop=Greenfoot.getKey();

        if ( knop=="space")
        {
            getWorld().addObject(new laser(), getX(), getY()-5);

        }

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

public class laser extends Actor
{
    private Counter counter;
 public laser(Counter pointCounter)
 {
     counter = pointCounter;
    }
    public void act() 
    
    {

         
        
        setLocation(getX(),getY()-5);
        
      
           if(foundAlien()) {
           eatalien();
           counter.add(1);
   }
    aanRandWereld();

    }
    

    

    public void eatalien()
    {
        Actor alien = getOneObjectAtOffset(0, 0, alien.class);
        if(alien != null) {
            getWorld().removeObject(alien);
        }
    }
       public boolean foundAlien()
   {
       Actor alien = getOneObjectAtOffset(0, 0, alien.class);
       if(alien != null) {
           return true;
       }
       else {
           return false;
       }
   }
    
    public void aanRandWereld()
    {
        
        if(atWorldEdge())
        {
            getWorld().removeObject(this);
        }
        
      
    }
    public boolean atWorldEdge() 
    { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A simple counter with graphical representation as an actor on screen.
 * 
 * @author mik
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/5/12

#
The problem is in your rocket class in line 39. You try to create a new instance of the class laser like this: new laser(); but the constructor of laser needs a parameter. So you either do this:
new laser(new Counter()), ...
or you create a new constructor in your laser class like this:
public laser() {

}
danpost danpost

2013/5/12

#
Your laser constructor is expecting a counter object passed to it (through its parameter), which you are not sending to it in line 39 of your rocket class. Either pass the counter object to the laser constructor or remove lines 5 through 9 of the laser constructor and get a reference to the counter object at line 21. You can get a reference to it and execute the 'add' method on it with '((Counter) getWorld().getObjects(Counter.class).get(0)).add(1);' (provided it is the only counter object in your world).
LittleJacob LittleJacob

2013/5/12

#
Thanks so much for the quick and helpful replies - really appreciate it! It's working great now. :)
You need to login to post a reply.