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

2023/3/16

code has error

Merten Merten

2023/3/16

#
i am quitte new to java and greenfoot so i dont know to fix the error i am getting. i am getting the error in class "Fighter_Kogel" at line 9. This is a subclass of "Kogel"
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Fighter_Kogel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fighter_Kogel extends Kogel
{
    /**
     * Act - do whatever the Fighter_Kogel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }
}
error: constructor Kogel in class Kogel cannot be applied to given types; trquired: int found: no arguments reason: actual and formal arguments lists differ in length
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Kogel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Kogel extends beweeg
{
    private Speler speler;
    private int kogelSnelheid = 5;
    private int richting;

    /**
     * Act - do whatever the Kogel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        geschoten();
        move(kogelSnelheid);
        

    }
        
    /*
    public Kogel(Speler speler)
    {
        this.speler = speler;
    } */

    public void geschoten()
    {
        int ypos = getY();
        int xpos = getX();
        if (ypos < 5 || xpos < 5 || ypos > 763 || xpos > 1361){
            getWorld().removeObject(this);
        }

    }


    public Kogel(int rot)
    {
        setRotation(rot);
        if (rot > 0 && rot < 90) {
            setRotation(0);
        }
        if (rot > 90 && rot < 180) {
            setRotation(90);
        }
        if (rot > 180 && rot < 270) {
            setRotation(180);
        }
        if (rot > 270 && rot < 360) {
            setRotation(270);
        }
    }

}
danpost danpost

2023/3/16

#
Merten wrote...
i am quitte new to java and greenfoot so i dont know to fix the error i am getting. i am getting the error in class "Fighter_Kogel" at line 9. This is a subclass of "Kogel" << Code Omitted >> << Error Omitted >> << Code Omitted >>
You do not have a constructor in the subclass Fighter_Kogel , which, by default calls the non-existent constructor Kogel(); but, the only constructor in the Kogel class is Kogel(int). Two ways to fix that are: (1) add a constructor to the Fighter_Kogel class that calls the constructor you do have in the Kogel class:
public Fighter_Kogel()
{
    super(Greenfoot.getRandomNumber(360));
}
(2) add a second constructor to the Kogel class that has no parameter:
public Kogel()
{
    this(Greenfoot.getRandomNumber(360));
}
Either way, the Kogel(int) constructor will execute when creating a Fighter_Kogel instance. The second way allows you to create a Kogel object directly without having to supply a rotational value. I guess a third way might be: (3) add a constructor to the Fighter_Kogel class with an int parameter for the rotation:
public Fighter_Kogel(int rot)
{
    super(rot);
}
This will require you to supply a rotation when creating an instance of the Fighter_Kogel class:
    new Fighter_Kogel(/** insert int rotational value here */);
I, myself, would prefer the second way.
danpost danpost

2023/3/16

#
Merten wrote...
public Kogel(int rot)
{
    setRotation(rot);
    if (rot > 0 && rot < 90) {
        setRotation(0);
    }
    if (rot > 90 && rot < 180) {
        setRotation(90);
    }
    if (rot > 180 && rot < 270) {
        setRotation(180);
    }
    if (rot > 270 && rot < 360) {
        setRotation(270);
    }
}
The code above can be simplified to:
public Kogel(int rot)
{
    setRotation((rot/90)*90);
}
Oftentimes, maths can reduce extensive codes.
danpost danpost

2023/3/16

#
Merten wrote...
i
public void act()
{
    // Add your action code here.
}
This code, in your Fighter_Kogel class, should be removed if you want the instances to move and get removed at edges like a Kogel instance is programmed to do. As an alternative, you could change it to:
public void act()
{
    super.act();
}
You need to login to post a reply.