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

2021/2/12

Moving to the left from the right and vise versa

Dween Dween

2021/2/12

#
I have to programm something for a class. Ive gone for a little shooting game. The Targets spwwawn either on the right or left side of the screen. That i have got working Now I need them to move in a straight line across the screen. The ones that spawn on the left to the right and vice versa. They get destroyed when they get to the other side(got that working too) With move() they automaticly go from left to right, but how do I di it the other way around. And how do I differentiate between the two types, so no one goes in the wrong direction. Like "when spawned right go left and the other way around". How could I do this? Dont need a detailed answer, just a rough tipp with what methods and stuff like that thats possible. I think with a good tipp i can figure this out but im kind of stuck right now.
RcCookie RcCookie

2021/2/12

#
Add a direction parameter into the constructor that sets the initial rotation, like this:
// In Target class
public Target(int rotation) {
    setRotation(rotation);
}
And then to create them:
GreenArm GreenArm

2021/2/12

#
Maybe you can take the width of the world, divide it by 2 (I'll name this variable worldX) and then look if the x coordinate of the target is lower then worldX (then its on the left) or if the x coordinate is higher then worldX (then its on the right). Hope I could help. I actually work on something similar. : )
danpost danpost

2021/2/12

#
You can randomly place them with:
public void addTarget()
{
    int x = 0, y = << constant >>;
    Actor target = new Target();
    if (Greenfoot.getRandomNumber(2) == 1)
    {
        x = getWidth()-1;
        target.turn(180);
    }
    addObject(target, x, y);
}
Dween Dween

2021/2/13

#
thanks everyone.
You need to login to post a reply.