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

2019/4/12

getRotation() value

ckverdad ckverdad

2019/4/12

#
I'm trying to figure out how to get the angle of a superclass inside the subclass but I need the value to be updated constantly as the superclass also turns constantly so the angle is always changing. Any way to do this?
danpost danpost

2019/4/12

#
Just use the getRotation method. It always returns the current rotation of the actor. Btw, there is no such thing as a rotation of a superclass -- or any class, for that matter. What you mean is: the rotation of an Actor object; or of an instance of type Actor. An instance of any class is also an instance of all its superclasses.
ckverdad ckverdad

2019/4/12

#
Sorry for the confusion. What I meant was how do I call the variable offsetX that is present in the superclass A, in it's subclass B?
public class A extends Actor
{
    int offsetX = 0;

    public void act() 
    {
         turn(-2);
         offsetX = (-(getRotation() - 360));
    }
}
and
public class B extends A
{
    public void act()
    {  
         System.out.println(super.offsetX);
    }
}
With this code, the console would always output 0, though if offsetX is printed within the superclass, A, it would output the correct angles.
danpost danpost

2019/4/12

#
Remove the 'super.' { s-u-p-e-r-(dot) }
ckverdad ckverdad

2019/4/12

#
Still produces an output 0 :/
danpost danpost

2019/4/12

#
ckverdad wrote...
Still produces an output 0 :/
Show full class codes for A and B.
Super_Hippo Super_Hippo

2019/4/12

#
You need to understand the difference between an object and a class. If you have two different objects in your world, one is of class A (and B) and the other one is only of class B, then no, you can't get the rotation of the other one directly like this. Every object of class A and every object of class B will have their own offsetX variable. You need to get a reference to the object and get the current value of the variable.
You need to login to post a reply.