# Need help with code

**URL:** <https://discourse.processing.org/t/need-help-with-code/27845>\
**Category:** Beginners\
**Created:** [February 15, 2021, 6:56pm UTC](https://discourse.processing.org/t/need-help-with-code/27845 "2021-02-15T18:56:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zvodca](https://avatars.discourse-cdn.com/v4/letter/z/85e7bf/32.png) [@zvodca](https://discourse.processing.org/u/zvodca)\
**Post date:** [February 15, 2021, 6:56pm UTC](https://discourse.processing.org/t/need-help-with-code/27845/1 "2021-02-15T18:56:02Z")

</div>

need help to make the ellipse go reverse and start all over again. want the ellipse to go back and forth on the same route.  
so far i have managed to move it forward. here is the code i already have;  
int x = 30;  
int y = 30;  
//int xDirection = 1;  
//int yDirection = 1;  
boolean moveForward = false;  
boolean moveDown = false;  
boolean moveBack = false;  
boolean moveUp = false;

void setup() {  
size(300, 300);  
}

void draw() {  
if ((x==30)&&(y==30)) {  
moveForward = true;  
moveUp = false;  
} else if ((x==270)&&(y==30)) {  
moveForward = false;  
moveDown = true;  
} else if ((x==270)&&(y==270)) {  
moveDown = false;  
moveBack = true;  
} else if ((x==30)&&(y==270)) {  
moveBack = false;  
moveUp = true;  
}  
if (moveForward == true) {  
x++;  
y =30;  
} else if (moveDown == true) {  
x=270;  
y++;  
} else if (moveBack == true) {  
x–;  
y=270;  
} else if (moveUp == true) {  
x=30;  
y–;  
}  
ellipse(x, y, 60, 60);

}

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 15, 2021, 7:54pm UTC](https://discourse.processing.org/t/need-help-with-code/27845/2 "2021-02-15T19:54:06Z")

</div>

Hi,

Welcome to the forum! 😉

Please note that you can format your code using the `</>` button when editing your message on the forum or enclose your code with backticks : ``` your code ``` → `your code`

There’s some errors in your code, I don’t know if it’s the copy pasting stuff but you rather want : `x--;` instead of `x-;` line 35

I think that you are doing too much conditions here and if you want the ellipse to go back, it’s going to add even more complexity. You have 4 conditions for switching before one direction to the other and 4 conditions to move the ball depending on it’s direction.

What I would do is : create a `t` variable representing the time of your animation between `0` and `1`. So if I say `0.5`, then it’s representing the ball at half the animation.

Also you manually specified the values of the border of the window (`270`, `30` for example) but what if I want the window size to change? And the ball size?

I would go like this :

```auto
// Ball parameters
float ballSize = 60;
float ballRadius = ballSize / 2;

// Time parameters
float t = 0;
float timeOffset = 0.005;

// The direction of the ball
boolean forward = true;

void setup() {
  size(300, 300);
}

void draw() {
  background(0);
  
  float x, y;
  
  // Test for each portions
  if (t >= 0 && t < 0.25) { // Up
    x = map(t, 0, 0.25, ballRadius, width - ballRadius);
    y = ballSize / 2;
  } else if (t >= 0.25 && t < 0.5) { // Right
    x = width - ballRadius;
    y = map(t, 0.25, 0.5, ballRadius, height - ballRadius);
  } else if (t >= 0.5 && t < 0.75) { // Down
    x = map(t, 0.5, 0.75, width - ballRadius, ballRadius);
    y = height - ballRadius;
  } else { // Left
    x = ballRadius;
    y = map(t, 0.75, 1, height - ballRadius, ballRadius);
  }
  
  // Display the circle
  circle(x, y, ballSize);
  
  // Move forward or backward in the animation
  if (forward) {
    t += timeOffset;
  } else {
    t -= timeOffset;
  }
  
  // If the animation is finished, invert the direction
  if (t > 1 || t < 0) {
    forward = !forward;
  }
}

```

Since the window is a square, I am dividing the animation in 4 steps for the sides. `1 / 4 = 0.25` so each time I check if the time is between `[0, 0.25[` or `[0.25, 0.5[` or `[0.5, 0.75[`… then based on that I use the `map()` function to get the x and y location of the ball based on the time.

I made a little sketch to explain that :

 ![Capture d’écran de 2021-02-15 20-53-23](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8c27dbb233b75035f284f49cc47d3a3da94006fb.png)

Hope it helps! 😉

---

<div class="post-metadata">

**Author:** ![zvodca](https://avatars.discourse-cdn.com/v4/letter/z/85e7bf/32.png) [@zvodca](https://discourse.processing.org/u/zvodca)\
**Post date:** [February 17, 2021, 4:18am UTC](https://discourse.processing.org/t/need-help-with-code/27845/3 "2021-02-17T04:18:22Z")

</div>

Thankyou it worked perfectly
