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

2016/6/1

get another objects rotation and location

ashjim999 ashjim999

2016/6/1

#
this is my code and it keeps saying that i am reffering to a non static method in a static way. How do i fix this.
1
2
3
4
5
6
7
8
9
10
public void act()
{
    if(Greenfoot.mouseClicked(this))
    {
        CannonBall cannonball = new CannonBall();
        getWorld().addObject(cannonball, CannonBarrel.getXCB(), CannonBarrel.getYCB());
        cannonball.setRotation(CannonBarrel.getRotationCB());
        cannonball.move(4);
    }
}
1
2
3
4
5
6
7
8
9
10
11
public int getRotationCB(){
    return getRotation();
}
 
public int getXCB(){
    return getX();
}
 
public int getYCB(){
    return getY();
}
SPower SPower

2016/6/1

#
Line 6 and 7 in your first piece of code cause the problem.
1
2
getWorld().addObject(cannonball, CannonBarrel.getXCB(), CannonBarrel.getYCB());
cannonball.setRotation(CannonBarrel.getRotationCB());
You've defined the three methods as an instance method (a method that can be called on objects), while you're now calling it on a class (CannonBarrel). You can only call static methods on classes. You'll need a way to get the object you want to call the methods on.
danpost danpost

2016/6/1

#
Actually, the three methods are redundant. You are creating methods just to call methods that already exists. In, other words, anywhere you can call these added methods, you could instead just call the method that the methods call from inside them and returns. Besides that, the rotation of a new object is always zero and the x and y of the object are not gettable until the object is added, where you are trying to get them before the object is added (to determine where to add it) into the world. So even if you were able to call the 'getXCB' and 'getYCB' methods, you would get IllegalStateException run-time errors within them. When I first looked at the top code block, before seeing the three methods, I thought that you had static methods that maybe chose specific random values that were returned. But, obviously that was not the case.
ashjim999 ashjim999

2016/6/3

#
i have changed my code but it still presents errors. World code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public MyWorld(CannonBarrel cannon)
  {   
      super(1200, 800, 1);
      setBackground("World.png");
      cannon = cannonbarrel;
      addObject(cannonbarrel, 201, 295);
      cannonbarrel.setRotation(315);
      CannonBase cannonbase = new CannonBase();
      addObject(cannonbase, 200, 310);
      cannonbase.setRotation(0);
      Slider slider = new Slider();
      addObject(slider, 200, 75);
      FireButton fireButton = new FireButton();
      addObject(fireButton, 800, 75);
  }
   
  public CannonBarrel getCannonBarrel(){
      return cannonbarrel;
  }
Fire Button Code:
1
2
3
4
5
6
7
8
9
if(Greenfoot.mouseClicked(this))
{
    CannonBall cannonball = new CannonBall();
    getWorld().addObject(cannonball, 201, 295);
    MyWorld myWorld = (MyWorld) getWorld();
    CannonBarrel cannonbarrel = myWorld.getCannonBarrel();
    cannonball.setRotation(cannonbarrel.getDirection());
    cannonball.move(4);
}
But with this code my world needs an object to initialise and without it i cant obtain the object for the cannon barrel.
ashjim999 ashjim999

2016/6/3

#
I deleted the things in cannon barrel.
danpost danpost

2016/6/3

#
You never set the 'cannonball' field in your World subclass to anything. You will find the problem at line 5 above. The value of 'cannon' is passed in and being replaced with the value of 'cannonball' there -- not what you wanted to do.
ashjim999 ashjim999

2016/6/3

#
no 'cannon is being changed to cannonbarrel not cannon ball. My world class doesnt spawn the cannon ball and therefore would not need code for the cannon ball.
danpost danpost

2016/6/3

#
ashjim999 wrote...
no 'cannon is being changed to cannonbarrel not cannon ball. My world class doesnt spawn the cannon ball and therefore would not need code for the cannon ball.
Correct -- in that I meant 'cannonbarrel'. But, why are you changing 'cannon', which has a value and not 'cannonbarrel' which does not?
ashjim999 ashjim999

2016/6/6

#
When I delete line 5 my cannon ball fires at 45 degrees (initial angle of cannon barrel) but never changes when the cannon barrel does.
SPower SPower

2016/6/6

#
You're not getting the problem. In this line:
1
cannon = cannonbarrel;
You're making cannon equal cannonbarrel. But cannonbarrel is null (as far as I can tell), because it is an uninitiated instance variable. cannon on the other hand does have a value, but you're then changing it to be null! It looks to me like you have the order reversed.
ashjim999 ashjim999

2016/6/6

#
i forgot to add this above.
1
2
3
4
5
6
7
CannonBarrel cannonbarrel = new CannonBarrel();
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
ashjim999 ashjim999

2016/6/6

#
i can remove cannon due to it not being used but this still presents the same problem
SPower SPower

2016/6/6

#
Maybe elaborating on what 'errors' you get would be helpful.
ashjim999 ashjim999

2016/6/6

#
The cannon ball always fires at 45 degrees even when i change the angle of the cannon barrel
SPower SPower

2016/6/6

#
First, I'm not entirely sure about this:
1
cannonball.setRotation(cannonbarrel.getDirection());
I don't know how you've defined the getDirection method, but simply using cannonbarrel.getRotation() might be easier. If that doesn't fix it, I suspect it's a problem with the precision in movement. Try to use the SmoothMover class to track the position and such (you can import that class into your project from Greenfoot).
You need to login to post a reply.