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

2017/4/9

How do I reverse an Actor's direction?

1
2
ET78759 ET78759

2017/4/9

#
I wanted to know how to reverse the direction an Actor is moving. I've tried to do it, but I can't show my code here because all my code for this is in two different classes...
Nosson1459 Nosson1459

2017/4/9

#
ET78759 wrote...
I can't show my code here because all my code for this is in two different classes...
How is that a reason for not being able to show code? Just show two separate classes. To reverse an actors direction you can either turn it 180 degrees, or negate the direction/speed variable which moves the actor. It depends how you're moving the actor. If you are using the move method the you'll have to rotate the actor, but if you are doing setLocation(getX() + speed... then you'll have to do speed = -speed.
ET78759 ET78759

2017/4/10

#
Thanks, and I didn't know you couldn't embed more than 1 section of code per post, I thought I would have had to put code on the question and then reply to myself.
Nosson1459 Nosson1459

2017/4/10

#
ET78759 wrote...
I didn't know you couldn't embed more than 1 section of code per post,
You could post more than one class.
I thought I would have had to put code on the question and then reply to myself.
What does posting code, have to do with replying to myself? Look, I can do this:
1
2
3
4
5
class Example {
    public static void main(String[] args) {
        System.out.println("First class");
    }
}
And then another:
1
2
3
4
5
class Example2 {
    public static void main(String[] args) {
        System.out.println("Second class");
    }
}
That's the two classes.
Nosson1459 Nosson1459

2017/4/10

#
...And now I can also do this:
1
2
3
4
5
class Example3 {
    public static void main(String[] args) {
        System.out.println("Third class");
    }
}
I don't see what could be wrong with doing what I've done above.
ET78759 ET78759

2017/4/10

#
I didn't know I could put more than one section of code in one post. That's it. I didn't say anything about me trying it and failing.
Nosson1459 Nosson1459

2017/4/10

#
ET78759 wrote...
I didn't know I could put more than one section of code in one post. That's it. I didn't say anything about me trying it and failing.
Well that's half the problem - you didn't even try.
ET78759 wrote...
Thanks,
Does that mean that reversing the actor's direction has been successful?
ET78759 ET78759

2017/4/10

#
no, I'm still working on it. I meant, "Thanks for the information". Actually, I've put it into my code and it just reversed the image, not the direction the actor was moving.
Nosson1459 Nosson1459

2017/4/10

#
ET78759 wrote...
no, I'm still working on it. I meant, "Thanks for the information". Actually, I've put it into my code and it just reversed the image, not the direction the actor was moving.
How are you moving the actor? With move or setLocation?
ET78759 ET78759

2017/4/10

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void ignite(boolean boosterOn)
{
    if (boosterOn)
    {
        setImage (rocketWithThrust);
        addToVelocity (new Vector(getRotation(), 0.3));
    }
    else
    {
        setImage(rocket);       
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void checkKeys()
{
    ignite(Greenfoot.isKeyDown("w"));
    Space space = (Space) getWorld();
 
    if (Greenfoot.isKeyDown("a"))
    {
        turn(-5);
    }
    if (Greenfoot.isKeyDown("d"))
    {
        turn(5);
    }
    if (Greenfoot.isKeyDown("space"))
    {
        fire();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void move()
{
    exactX = exactX + velocity.getX();
    exactY = exactY + velocity.getY();
    if (exactX >= getWorld().getWidth()) {
        exactX = 0;
    }
    if (exactX < 0) {
        exactX = getWorld().getWidth() - 1;
    }
    if (exactY >= getWorld().getHeight()) {
        exactY = 0;
    }
    if (exactY < 0) {
        exactY = getWorld().getHeight() - 1;
    }
    super.setLocation((int) exactX, (int) exactY);
}
With these three methods.
Nosson1459 Nosson1459

2017/4/10

#
So what's the problem? Where are you trying to reverse an actors direction and where is it not working?
ET78759 ET78759

2017/4/10

#
1
2
3
4
5
6
7
8
9
public void checkShield()
{
    Actor shield = getOneIntersectingObject(Shield.class);
    if (shield != null)
    {
        getWorld().removeObject(shield);
        setRotation(180);
    }
}
my code is here.
Nosson1459 Nosson1459

2017/4/10

#
I can't really tell what's going on here since you gave me four different snippets of code. I don't know where they're all coming from. If it's all one class and you want to reverse the rocket's direction then do exactX = -exactX; exactY = -exactY;
ET78759 ET78759

2017/4/10

#
I want to reverse the asteroid's direction when it touches the shield. The first two pieces of code were from rocket's class, the third was from a class that made all movement smoother(it was called SmoothMover) and the fourth was from the class Asteroid.
ET78759 ET78759

2017/4/10

#
And I've already tried reversing exactY and exactX. Didn't work.
There are more replies on the next page.
1
2