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

2022/12/2

How to reference non-static getRotation() in static Context.

Riovanni Riovanni

2022/12/2

#
I can't figure out how to return a bullet's rotation. I am making a car chase, where there are two cars, and there will be bullet fire from both sides. I want to make sure that the blue team isn't getting hit with their own bullets.
public static boolean isEnabled(){
    if (getRotation() == 0 && getX() <= 950){
    return true;
    }else if (getRotation() == 90 && getX() >= 100){
    return true;
    } else{
    return false;
    }
    }
It says that it can't be referenced in a static context, but I have figured out ways around stuff like that before. I don't know what's different about this. =|
danpost danpost

2022/12/2

#
Riovanni wrote...
It says that it can't be referenced in a static context, but I have figured out ways around stuff like that before. I don't know what's different about this. =|
You cannot call non-static methods from a static one (without supplying an object for the non-static method to work on). A static method (or field) does not refer to any particular object of the class (so, there is no rotation or location to speak of). You could make subclasses of the bullet for red team bullets and blue team bullets and then have each fire their specific types; or, you could make use of an inner class (a subclass of bullet in generic Car class) to link the bullet to its creator. The second way may be more preferable, as less code is required, only a method to return the type of its parent or the parent itself so the type can be determined.
Riovanni Riovanni

2022/12/4

#
oh okay. thanks
You need to login to post a reply.