# Ball should change direction after touching edge of the frame

**URL:** <https://discourse.processing.org/t/ball-should-change-direction-after-touching-edge-of-the-frame/20207>\
**Category:** Coding Questions\
**Created:** [April 27, 2020, 11:57am UTC](https://discourse.processing.org/t/ball-should-change-direction-after-touching-edge-of-the-frame/20207 "2020-04-27T11:57:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![julia1312](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@julia1312](https://discourse.processing.org/u/julia1312)\
**Post date:** [April 27, 2020, 11:57am UTC](https://discourse.processing.org/t/ball-should-change-direction-after-touching-edge-of-the-frame/20207/1 "2020-04-27T11:57:00Z")

</div>

Hi! I am completely new in the world of programming! I have started my programming lessons recently and I am stuck with my homework. What I basically have to do is a moving ball. The ball should move with velocity 20 pixels/second and change the direction after touching one of the edges (right or left). What script/code should I use?  
Here is what I already created:

```auto
int currTime, prevTime;  
float elapsedTime;       
float xspeed = 20;        
float x0, x1;
 
void setup(){
  size(600, 200);
  frameRate(200);
  noStroke();
  
  currTime = prevTime = millis();
}
  
void draw(){
  
  currTime = millis();
  elapsedTime = (currTime - prevTime) / 1000.0;
  prevTime = currTime;  
 
  background(150, 150, 150);
  
  x0 += xspeed * elapsedTime; 
  
  fill (250, 250, 0);
  ellipse(x0, 64, 20, 20);
  
 if (x0 >= 600 ) { 
    xspeed= -1;
}
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 27, 2020, 12:56pm UTC](https://discourse.processing.org/t/ball-should-change-direction-after-touching-edge-of-the-frame/20207/2 "2020-04-27T12:56:57Z")

</div>

> [@julia1312](#):
>
> if (x0 \>= 600 ) {  
> xspeed= -1;  
> }

Additionally if(x0\<10) xspeed=1;

---

<div class="post-metadata">

**Author:** ![julia1312](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@julia1312](https://discourse.processing.org/u/julia1312)\
**Post date:** [April 27, 2020, 1:06pm UTC](https://discourse.processing.org/t/ball-should-change-direction-after-touching-edge-of-the-frame/20207/3 "2020-04-27T13:06:07Z")

</div>

Still not working! The ball disappeared behind the frame and started again slinding from right to left (like the begininng).

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 27, 2020, 2:42pm UTC](https://discourse.processing.org/t/ball-should-change-direction-after-touching-edge-of-the-frame/20207/4 "2020-04-27T14:42:38Z")

</div>

> [@julia1312](#):
>
> currTime = millis();  
> elapsedTime = (currTime - prevTime) / 1000.0;  
> prevTime = currTime;

I think this is flawed.

Which value does prevTime get? Is it different from current time?
