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

2013/12/11

Steady image

1
2
Chaveron Chaveron

2013/12/11

#
Hey, I'm pretty new to Greenfoot and I have a small problem. How can I make my actors having the original image all the time? When the actor is turning, the image should not turn with the actor but be in the starting position. Can anyone please help me with that?
danpost danpost

2013/12/11

#
You should add an instance field to the class of your actor to track the 'rotation' of it. Then use the following steps to move the actor: 1) rotate actor to value of 'rotation' 2) adjust rotation of actor, if necessary (like because it was user instructed or implementing a random turn) 3) move the actor the appropriate distance 4) save the rotation of the actor back into the field 5) set the rotation of the actor back to zero
lordhershey lordhershey

2013/12/11

#
Could you simply make an invisible actor and have a visible one follow it?
danpost danpost

2013/12/11

#
lordhershey wrote...
Could you simply make an invisible actor and have a visible one follow it?
You could. That is more of a 'cheat' way of doing it. Adding an actor to replace a field does not seem appropriate (all the invisible actor will end up doing is track the 'rotation' of the main actor by taking on that rotation). Plus, now you have two objects to move. Both objects would probably have to 'know' about each other (more fields). Then, things could get really complicated when dealing with collision detection or when adding more behavior. The drawbacks seem to outweigh the benefits.
Chaveron Chaveron

2013/12/11

#
danpost wrote...
1) rotate actor to value of 'rotation' 2) adjust rotation of actor, if necessary (like because it was user instructed or implementing a random turn) 3) move the actor the appropriate distance 4) save the rotation of the actor back into the field 5) set the rotation of the actor back to zero
Umm.. That's a bit too much for me I guess
danpost danpost

2013/12/11

#
You could use the 'setLocation' method instead of 'setRotation' and 'move'.
erdelf erdelf

2013/12/11

#
its not really much actually, maybe u should write ur code as the rotation of the image wouldn't be important, then trying to do what danpost said and if you have problems then post it here, so we can help you
danpost danpost

2013/12/11

#
If you post what movement/rotation code you have now, we might be able to help in getting it to do what you want.
lordhershey lordhershey

2013/12/12

#
How about this? public class PointUp extends Actor { private int rotation = 0; /** * Act - do whatever the PointUp wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. move(2); turn(5); } @Override public void setRotation(int rotation) { this.rotation = rotation; } @Override public int getRotation() { return this.rotation; } @Override public void move(int distance) { super.setRotation(this.rotation); super.move(distance); super.setRotation(0); } @Override public void turnTowards(int x,int y) { super.turnTowards(x,y); this.rotation = super.getRotation(); super.setRotation(0); } @Override public void turn(int amount) { rotation += amount; while(rotation < 0) { rotation += 360; } if (rotation >= 360) { rotation = rotation % 360; } } }
lordhershey lordhershey

2013/12/12

#
I made this: http://www.greenfoot.org/scenarios/10361 with source if you want to check it out.
danpost danpost

2013/12/12

#
All that you needed to do (from the state it was in when you initially started this discussion is:
// add instance field
private int rotation;
// before the move code that you already had
setRotation(rotation); // my step 1
// after the move code that you already had
rotation = getRotation(); // my step 4
setRotation(0); // my step 5
That was it.
lordhershey lordhershey

2013/12/12

#
That is great, but you would have to do it for every object type that needed this behavior. You can make the PointUp abstract, extend from it then in the act you would have the properties carried through the subclass. Your example will fall short if they wish to use turn towards or turn by a few degrees. In the scenario, I create 4 objects, their acts just say move and turn, I do not need to worry about holding their rotation or setting it back anymore. . . . Oh wait, I think I am wrong, you wrapper would work with turn as well. The only thing you would have to insure is that the setRotation(0) happens at every exit point of the routine...
danpost danpost

2013/12/12

#
I guess I should have specified that turning is done while moving is done (or side by side, between lines 4 and 6 in my last post (although it was mentioned in my initial procedural listing). Anyway, read the first sentence of the first post in this discussion -- Chaveron stated "I'm pretty new to Greenfoot. Let's keep things simple.
Chaveron Chaveron

2013/12/12

#
First of all, thank you so much for your replies!
danpost wrote...
All that you needed to do (from the state it was in when you initially started this discussion is: view plaincopy to clipboardprint? // add instance field private int rotation; // before the move code that you already had setRotation(rotation); // my step 1 // after the move code that you already had rotation = getRotation(); // my step 4 setRotation(0); // my step 5 That was it.
That code makes my actors move into one direction as they can't turn anymore..
lordhershey wrote...
How about this? public class PointUp extends Actor { private int rotation = 0; /** * Act - do whatever the PointUp wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. move(2); turn(5); } @Override public void setRotation(int rotation) { this.rotation = rotation; } @Override public int getRotation() { return this.rotation; } @Override public void move(int distance) { super.setRotation(this.rotation); super.move(distance); super.setRotation(0); } @Override public void turnTowards(int x,int y) { super.turnTowards(x,y); this.rotation = super.getRotation(); super.setRotation(0); } @Override public void turn(int amount) { rotation += amount; while(rotation < 0) { rotation += 360; } if (rotation >= 360) { rotation = rotation % 360; } } }
That code works for me but now my actors don't move through the world edge and come out at the other side. My code for that: public boolean atWorldEdge() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; }
danpost danpost

2013/12/12

#
The code you supplied only checks for the world edges. We need to see the code that actually transports the actor from one edge to the opposite one ('move through the world edge and come out at the other side').
There are more replies on the next page.
1
2