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

2014/3/19

Automatically adding a rotated object

kzb94 kzb94

2014/3/19

#
I'm trying to automatically place an actor in my world, but I want the actor to be rotated. I can't create another actor in the rotated form, I have to actually rotate the original actor in my world code. Any advice?
danpost danpost

2014/3/19

#
If you want all actors created from this class to be rotated, then set their rotation in the constructor of the class of the actor. If this is like an every once in a while, when I create an object of this class, I want it rotated, then create it outside the 'addObject' statement and then separately do both, rotate the actor and add it into the world.
1
2
3
4
5
6
// instead of
addObject(new Actor(){}, x y);
// use this
Actor actor = new Actor(){};
actor.turn(180);
addObject(actor, x, y);
kzb94 kzb94

2014/3/19

#
Here's all of my code for this method: public MazeWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 600, 1); addObject(new Treasure(), 950,30); addObject(new MiddleWall(), 460/2, 300); addObject(new MiddleWall(), 1000 - 460/2, 300); addObject(new WallSize45_1(), 32, 236); addObject(new WallSize45_1(), 89, 167); addObject(new WallSize90_1(), 116, 63); addObject(new WallSize90_1(), 233, 104); addObject(new WallSize90_1(), 209, 234); addObject(new WallSize45_1(), 315, 175); addObject(new WallSize90_1(), 407, 231); addObject(new WallSize90_1(), 503, 65); addObject(new WallSize45_1(), 483, 127); addObject(new WallSize90_1(), 578, 127); addObject(new WallSize90_1(), 689, 127); addObject(new WallSize45_1(), 763, 127); addObject(new WallSize90_1(), 858, 127); addObject(new WallSize45_1(), 755, 64); addObject(new WallSize90_1(), 940, 71); addObject(new WallSize90_1(), 599, 190); addObject(new WallSize90_1(), 715, 248); addObject(new WallSize45_1(), 757, 188); addObject(new WallSize 90_1(), 59,114 ); addObject(new WallSize 90_1(), 181, 115); addObject(new WallSize 90_1(), 284, 51); addObject(new WallSize 45_1(), 275, 203); addObject(new WallSize 90_1(), 355, 178); addObject(new WallSize 90_1(), 457,178); addObject(new WallSize 45_1(), 566,35); addObject(new WallSize 45_1(), 643, 83); addObject(new WallSize 90_1(), 545, 243); addObject(new WallSize 45_1(), 656, 218); addObject(new WallSize 45_1(), 787, 33); addObject(new WallSize 45_1(), 787, 157); addObject(new WallSize 90_1(), 925,135); addObject(new WallSize 90_1(), 858, 245); addObject(new WallSize 45_1(), 925,212); The second set of "addObjects" all need to be rotated 90 degrees, but the first set does not.
kzb94 kzb94

2014/3/19

#
I added the changes. Everything compiled, but no changes were made in the actual game.
danpost danpost

2014/3/19

#
kzb94 wrote...
I added the changes. Everything compiled, but no changes were made in the actual game.
Where did you add the changes? Anyway, since there are a bunch of these objects and about half of them need rotated, and, thankfully, only two differenct classes to deal with ( 'WallSize45_1' and 'WallSize90_1' ), it would probably be best, if not easiest, to have these actors rotate themselves. We can let them know to rotate themselves by adding an argument to the constructors. For one example, in the WallSize45_1 class, add the following constructor:
1
2
3
4
public WallSize45_1(boolean rotated)
{
    if (rotated) setRotation(90);
}
Do the same for the other wall class. Then, for all 'addObject' statements that add a wall you want rotated, put 'true' in the empty set of parentheses:
1
addObject(new WallSize45_1(true), ....
kzb94 kzb94

2014/3/20

#
My prior post didn't contain the changes. I can't alter the classes. How could I make the changes in the world class code?
danpost danpost

2014/3/20

#
danpost wrote...
1
2
3
4
5
6
// instead of
addObject(new Actor(){}, x y);
// use this
Actor actor = new Actor(){};
actor.turn(90); // edited from 180 above
addObject(actor, x, y);
Do this change for every line in the second group (the group you need rotated). You can use the same field for all the changes (only use 'Actor actor' the first time; after that, just use 'actor'). Start like this: Actor actor = new WallSize90_1(); actor.setRotation(90); addObject(actor, 59, 114 ); actor = new WallSize90_1(); // repeat the rotating and adding for this actor // etc.
You need to login to post a reply.