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

2020/3/21

cannot find class

Starlord_20 Starlord_20

2020/3/21

#
boolean getActorInFront(Class cls) {
        int r = getRotation() / 90;
        Actor actor = (Actor) getOneObjectAtOffset((1-r)%2,(2-r)%2,Actor.class);
        if(actor instanceof cls) return true;
        else return false;
    } 
I get an error that the class cls couldn't bt found...
danpost danpost

2020/3/21

#
Show how you are calling the method.
Starlord_20 Starlord_20

2020/3/21

#
I already fixed it but thank you... :) I changed the parameter into Class<?> cls and and worked with a list
boolean isActorFront(Class<?> cls) {
        int r = getRotation()/90;
        List<?> obj = getWorld().getObjectsAt(this.getX()+(1-r)%2,this.getY()+(2-r)%2,cls);
        if(obj != null && obj.size() > 0) return true;
        else return false;
    }
and that's the way I call it (I called it like that before I changed too) :
if(isActorFront(Actor.class)) // do something
it works fine
danpost danpost

2020/3/21

#
I think you would have been fine replacing your original lines 4 and 5 with:
return actor != null;
You need to login to post a reply.