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

2014/5/16

Incompatible types

Midas97 Midas97

2014/5/16

#
hello, I don't know why Greenfoot shows an incompatible types error at the Brackets of the greenfoot-method "getOneObjectAtRange" when I want to compile it:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Bullet extends Actor
{
    private int Speed;
    public Bullet(){
        Speed = 3;
    }

    public void act()
    {
        setLocation(getX()+Speed,getY());
    }    

    public void switchDirection(){
        if(this.getObjectsInRange(5, Barrier.class)){
            if(Speed>0){
                Speed = -3;
            }
            if(Speed<0){
                Speed = +3;
            }
        }
    }
}
The same comes up with "getOneObjectAtOffset" And the Barrier.class exists. Does anyone know the problem? Thanks
danpost danpost

2014/5/16

#
'if' statements require a boolean, not a List object. In line 16, your condition is a List object, not a boolean. Maybe you want the following:
if (!this.getObjectsInRange(5, Barrier.class).isEmpty()){
which says 'if the list returned is not empty ...'.
Midas97 Midas97

2014/5/16

#
Thanks danpost. Now it worked with the extra method. Now I have also gameresult-problem, that the bullet don't switches the direction if it "collides" with the Barrier. I've corrected the known mistakes at the code so far:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Bullet extends Actor
{
    private int Speed;
    public Bullet(){
        Speed = 3;
    }

    public void act()
    {
        setLocation(getX()+Speed,getY());
    }    

    public boolean switchDirection(){
        if(!this.getObjectsInRange(2, Barrier.class).isEmpty()){
            if(Speed==3){
                Speed = Speed -6;
            }
            if(Speed==-3){
                Speed = Speed +6;
            }
            return true;
        }else{
            return false;
        }       
    }
}
danpost danpost

2014/5/16

#
You probably could have just moved it back off the barrier after changing directions:
public boolean switchDirection(){
    if(!getObjectsInRange(2, Barrier.class).isEmpty()){
        Speed = -Speed
        setLocation(getX()+Speed, getY());
        return true;
    }
    return false;
}
Midas97 Midas97

2014/5/16

#
Okay so the location needs to refresh by doing the setlocation in the if-part, so I cutted the act out and wrote it should act the method. Now it worked. Thanks again. :D
Midas97 Midas97

2014/5/16

#
Sorry to interrupt again but "getObjectsInRange" is not the best way for it, because the dimensions aren't the same of both objects, it is a radius-sensor with its middlepoint at the center of the object. So I took "getOneIntersectingObject(Barrier.class)" and there's again the error, but now it don't knows the "isEmpty()"-method.
danpost danpost

2014/5/16

#
'getOneIntersectingObject' returns an Object, not a List (or a boolean). You can use 'getIntersectingObjects' which returns a List object (that 'isEmpty' will work on) or you can use change the line to:
if (getOneIntersectingObject(Barrier.class) != null){
You need to login to post a reply.