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

2013/11/22

How to getRotation() of an object

1
2
JasonZhu JasonZhu

2013/11/22

#
    private void bounceOff(Actor Object)
    {       
        int angleDeflect = //Paddle.getRotation()-90;
        int angle = angleDeflect+90;
        getMovement().setDirection(angle);
        if(getSpeed()<0.5){
            falling=false;
            getWorld().removeObject(this);
        }
    }
"Paddle.getRotation()-90;" does not simply do it, how do I do this?
Gevater_Tod4711 Gevater_Tod4711

2013/11/22

#
If you want to get the rotation of the object you used as parameter for this method you need to refer to it using the objects name (here its Object) and get the rotation of this object:
    private void bounceOff(Actor Object)  
    {         
        int angleDeflect = Object.getRotation()-90;
        int angle = angleDeflect+90;  
        getMovement().setDirection(angle);  
        if(getSpeed()<0.5){  
            falling=false;  
            getWorld().removeObject(this);  
        }  
    }  
Because Object is also a class you should use lower cases for object names. But this doesn't realy matter here.
JasonZhu JasonZhu

2013/11/22

#
This method I have is in my Drop class, I want it to get the Rotation of the Paddle's rotation. I'm still unsure of how to write it... Do I have to do something like "Actor Paddle = paddle" or something?
Gevater_Tod4711 Gevater_Tod4711

2013/11/22

#
You should have a look at this Greenfoot Tutorial. That may help you.
JasonZhu JasonZhu

2013/11/22

#
It helped, however, I'm getting a NullPointerException when I run the code. java.lang.NullPointerException at Drop.bounceOff(Drop.java:50) at Drop.act(Drop.java:36) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
Gevater_Tod4711 Gevater_Tod4711

2013/11/22

#
Could you post the act method of your Drop class? The Exception seems to occour in this method.
danpost danpost

2013/11/22

#
Gevater_Tod4711 wrote...
Could you post the act method of your Drop class? The Exception seems to occour in this method.
From the error message, the error is occurring in the 'bounceOff' method at line 50 of the class. However, the cause still may be related to code in your 'act' method of that class. The two possibles that I can come up with are that 'Object' (from the parameter) is 'null' or that 'this' object was already removed from the world and 'getWorld' is returning 'null' when trying to remove it again. More likely, it is the latter case, and you need to qualify the call to the method with 'if (getWorld() != null) bounceOff();'.
JasonZhu JasonZhu

2013/11/22

#
    public void act()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        Actor container = getOneIntersectingObject(Container.class);        
        if (paddle!=null){
            bounceOff(paddle);
            accelerate(0.85);            
        }
//         if (containerEdge()==true){
//             bounceOff(container);
//             accelerate(0.85);            
//         }
        if (falling){
            fall(); 
        }        
    } 
Here it is.
I tried writing getWorld()!=null, but when my drop touches that paddle, I get this error still.
danpost danpost

2013/11/22

#
The code for line 9 should be similar to the code of line 5.
JasonZhu JasonZhu

2013/11/22

#
Oh never mind about the commented code, the bouncing off paddle is still erroring out. I need to fix that before worrying about the container
danpost danpost

2013/11/22

#
Comment out lines 13 and 14 and report back if it errors out still.
JasonZhu JasonZhu

2013/11/22

#
Still getting the error...
danpost danpost

2013/11/22

#
Please inform which line is line 50 in your Drop class
JasonZhu JasonZhu

2013/11/22

#
        int angleDeflect = world.getContainer().getRotation()-90;
danpost danpost

2013/11/22

#
Ok, that does not look anything like what you had in your 'bounceOff' method before. Please post that entire method again, as it is now. Also, post your latest error message.
There are more replies on the next page.
1
2