# Trying to change states using the dist function

**URL:** <https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606>\
**Category:** Coding Questions\
**Created:** [March 11, 2020, 4:54pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606 "2020-03-11T16:54:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chriswolf378](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@Chriswolf378](https://discourse.processing.org/u/Chriswolf378)\
**Post date:** [March 11, 2020, 4:54pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/1 "2020-03-11T16:54:15Z")

</div>

Essentially what I want to happen is when the player hits a certain point the game will change to the next state at the moment I have this code:

```auto
void checkWinLose(){  
    if (dist(x, y, 500.0 ,0.0) < 45 ){
    state = "WIN";

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [March 11, 2020, 5:13pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/2 "2020-03-11T17:13:12Z")

</div>

Hello!

I don’t how many states you you want to change through but using a switch statement, reference here:  
[https://processing.org/reference/switch.html](https://processing.org/reference/switch.html)  
May be of interest?

---

<div class="post-metadata">

**Author:** ![Chriswolf378](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@Chriswolf378](https://discourse.processing.org/u/Chriswolf378)\
**Post date:** [March 11, 2020, 5:14pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/3 "2020-03-11T17:14:44Z")

</div>

Switching between states isn’t the issue because I can handle that fine the issue is that I specifically want the state to switch when the player hits a certain point, i’m working with 4 states rn

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [March 11, 2020, 5:20pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/4 "2020-03-11T17:20:12Z")

</div>

Mmmm, I’m not sure I completely understand. If you can show more code, that would be helpful.  
Also, make sure you format it. It’ll be easier for people to read. 🤓  
How to do that is here: [https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

---

<div class="post-metadata">

**Author:** ![Chriswolf378](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@Chriswolf378](https://discourse.processing.org/u/Chriswolf378)\
**Post date:** [March 11, 2020, 5:22pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/5 "2020-03-11T17:22:13Z")

</div>

```auto
//global variables
Float x = 150; 
Float y = 100;
Float speed = 5.5;
String state;
 Float e = 5.0;
  Float z = 7.0;
// Float v =
  //Float s =
 // Float j =
   //loat c =

void setup(){
  size(500,500);
  state = "START";
}
void draw() {

 background(0);
 if (state == "START") {
   startGame();
 } else if (state == "PLAY"){
   playGame();
 } else if (state == "WIN") {
   winGame();
 } else if (state == "LOSE") {
   loseGame();
 }
}

void startGame(){
  textAlign(CENTER);
  textSize(18);
  fill(255,0,0);
  text("Press H to play!" , width/2, height/2);
  if (key == 'h') {
   state = "PLAY";
  }
}

void playGame(){
  fill (245,400,93);
  ellipse (x,y,50,50);
  rect( l, 20, 30, 30);
  rect( 20,e, 30, 30);
  rect( z, 20, 30, 30);
  ellipse(500,0,100,100);
   l = l + 1;
   e = e + 2;
   z = z + 3;
   if(z>width){
     z= 0.0;
   if(e>height){
     e= 4.0;
   }
  if(l>width){
  l= .5;
  }
   }
checkWinLose(); 
    if (dist(x, y, 500.0 ,0.0) < 45 ){

}

void keyPressed(){

if(key == 'w')
{
  y = y - speed;
}

  {
if(key == 's'){
  y = y + speed;
}
if(key == 'a'){
  x = x - speed;
}
if(key == 'd'){
  x = x + speed;
}
  }
}

void checkWinLose(){  
    if (dist(x, y, 500.0 ,0.0) < 45 ){
    state = "WIN";
// if (dist(a, c, 650, 500) < 250/2 && mousePressed) {
// state = 3;
  // if (dist(b, d, 650, 500) < 250/2 && mousePressed) {
// state = 3;
  // }
 // }
   }
}
  

void winGame(){
  
      background(0, 255, 200);
    fill(0);
    text("You did a great job", 775, 50);    
    text("Press the 'P' key to play again.",775, 425);  
    if (keyPressed && key == 'P') {
      state = "START";
    }  
}

void loseGame(){
  background(220, 0, 225);
    text("You did a poor job",775, 50);
    text("Press the 'R' key to retry", 775,425);
    if (keyPressed && key == 'R') {
      state = "PLAY";
}
}

```

---

<div class="post-metadata">

**Author:** ![Chriswolf378](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@Chriswolf378](https://discourse.processing.org/u/Chriswolf378)\
**Post date:** [March 11, 2020, 5:30pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/6 "2020-03-11T17:30:18Z")

</div>

Wait I stopped trying to run the code on openprocessing and it worked lool

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [March 11, 2020, 5:31pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/7 "2020-03-11T17:31:02Z")

</div>

Thank you! Much easier on the eyes! 😀

So, after reading through, unfortunately I cannot answer the specifics of your question. However there are a lot of other members who can!  
🙂

---

<div class="post-metadata">

**Author:** ![Chriswolf378](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@Chriswolf378](https://discourse.processing.org/u/Chriswolf378)\
**Post date:** [March 11, 2020, 5:31pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/8 "2020-03-11T17:31:31Z")

</div>

But now this brings up another issue the previous scene doesn’t refresh itself now, how would I go about refreshing the previous state to it’s default?

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [March 11, 2020, 5:47pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/9 "2020-03-11T17:47:31Z")

</div>

after losing you’d probably want the player to START a new game, not just PLAY it?

---

<div class="post-metadata">

**Author:** ![Chriswolf378](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@Chriswolf378](https://discourse.processing.org/u/Chriswolf378)\
**Post date:** [March 11, 2020, 6:00pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/10 "2020-03-11T18:00:37Z")

</div>

Well this is really just an example so i’d rather it just refresh the previous level I was attempting to use the redraw function but it’s not really working out the way I intended any advice? Same goes for when the player “wins” as well I want the scene to redraw itself

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [March 11, 2020, 6:55pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/11 "2020-03-11T18:55:40Z")

</div>

I could be completely misunderstanding what you are trying to do but if you use the switch statements instead of if-else, then you’d be able to direct to the specific level you want to return to. ??

---

<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:** [March 11, 2020, 7:01pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/12 "2020-03-11T19:01:49Z")

</div>

> [@Chriswolf378](#):
>
> state == “START”

Attention, == won’t work well with Strings; instead use if(state.equals(“START”)) {

equals is important here

---

<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:** [March 11, 2020, 7:09pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/13 "2020-03-11T19:09:06Z")

</div>

> [@Chriswolf378](#):
>
> how would I go about refreshing the previous state to it’s default

When you want to restart the game:  
Please set values back to initial values,  
especially for x,y,z, l, e

---

<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:** [March 11, 2020, 7:12pm UTC](https://discourse.processing.org/t/trying-to-change-states-using-the-dist-function/18606/14 "2020-03-11T19:12:10Z")

</div>

> [@debxyz](#):
>
> if you use the switch statements instead of if-else…

I don’t really think there is a difference between switch and if, concerning what you can do with it in terms of state

* * *

Consider pressing ctrl-t in processing to get auto-format

Do this prior to posting

* * *

> [@Chriswolf378](#):
>
> if (key == ‘h’) { state = “PLAY”;

Here you need keyPressed

* * *

Sometimes you check the keys with capital letters, sometimes lower case. That could confuse the user. Or you do it intentionally?
