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

2013/4/1

How to access one class's rotation from another class

1
2
JetLennit JetLennit

2013/4/1

#
I need help accessing one class's rotation from another class. For example, I need to have a bullet class know a turret's rotation so it fires the right way.
Gevater_Tod4711 Gevater_Tod4711

2013/4/1

#
You need to get the reference to the object you want to ghet the rotation from and then you can easily get it's rotation using the getRotation method. To see how you can get a reference of an object you should do this tutorial.
JetLennit JetLennit

2013/4/1

#
The problem it that getRotation() can not be accessed from a static context
Gevater_Tod4711 Gevater_Tod4711

2013/4/1

#
If you are trying to get the value by static methods that will not work. Except you declare a variable rotation (which is static) and in every act cycle you set the value of this variable to the objects rotation:
1
2
3
4
5
public static int rotation = 0;
 
public void act() {
    rotation = getRotation();
}
Then you can excess this value like you are trying to do. But this will only work if you only got one instance of this object in your world, because static variables have got the same value for all instances of the object. If you got more that one instance the values that are returnd will probably be wrong.
JetLennit JetLennit

2013/4/1

#
Although for my thing this wont work.... it does some pretty cool things!
danpost danpost

2013/4/1

#
The easiest way to accomplish this is as follows:
1
2
3
4
// in the turret class code, when triggered to fire a bullet
Bullet bullet = new Bullet();
bullet.setRotation(getRotation());
addObject(bullet, getX(), getY());
Griffon305 Griffon305

2013/4/1

#
I was just about to post a discussion about this, so I put the code in that danpost just put up and it is telling me that the cannot find symbol-addObject method. This is what I have with danpost's code added
1
2
3
4
5
6
7
  if(Greenfoot.isKeyDown("space"))
  
     bullet bullet = new bullet(); 
     bullet.setRotation(getRotation()); 
     addObject(bullet, getX(), getY());
  }
}
Also, I have the code set that the bullet fires every time the space bar is hit, but instead of firing one bullet, it fires a stream of bullets, what is the way around that?
JetLennit JetLennit

2013/4/1

#
add getWorld(). before add object
danpost danpost

2013/4/1

#
JetLennit wrote...
The problem it that getRotation() can not be accessed from a static context
You cannot rotate the Class itself. You have to rotate an actor object created from that class.
JetLennit JetLennit

2013/4/1

#
This is my way around that firts declare an int called ammo then add
1
2
3
4
5
6
7
8
9
10
11
12
if (Greenfoot.isKeyDown("space"))
        {
            if(ammo > 0)
           {
             //the fire code
             ammo = -10;
           }
        }
        else
        {
            ammo = (ammo + 1);
        }
danpost danpost

2013/4/1

#
JetLennit wrote...
add getWorld(). before add object
What he said (thanks @JetLennit).
JetLennit JetLennit

2013/4/1

#
I wrote...
ammo = -10;
change that to a higher number if you want it to wait longer
danpost danpost

2013/4/1

#
JetLennit wrote...
This is my way around that firts declare an int called ammo then add
1
2
3
4
5
6
7
8
9
10
11
12
if (Greenfoot.isKeyDown("space"))
        {
            if(ammo > 0)
           {
             //the fire code
             ammo = -10;
           }
        }
        else
        {
            ammo = (ammo + 1);
        }
It really depends on what functionality is wanted. Whether the action should occur when the spacebar is pressed or not. Whether each press creates a new bullet or a regulated stream while the key is held down.
JetLennit JetLennit

2013/4/1

#
I was just saying that was my way around it.. what he/she does is up to them
danpost danpost

2013/4/1

#
I was just trying to make it easier and faster to come up with a solution that will fit the scenario (not saying that yours will not work; and, it may be all that is needed). If the specifics were given, then a final solution could be readily forthcoming.
There are more replies on the next page.
1
2