# \[SOLVED\] Problems with PVectors

**URL:** <https://discourse.processing.org/t/solved-problems-with-pvectors/3715>\
**Category:** Coding Questions\
**Created:** [September 21, 2018, 3:37pm UTC](https://discourse.processing.org/t/solved-problems-with-pvectors/3715 "2018-09-21T15:37:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OrigamiBoy](https://avatars.discourse-cdn.com/v4/letter/o/8e7dd6/32.png) [@OrigamiBoy](https://discourse.processing.org/u/OrigamiBoy)\
**Post date:** [September 21, 2018, 3:37pm UTC](https://discourse.processing.org/t/solved-problems-with-pvectors/3715/1 "2018-09-21T15:37:39Z")

</div>

Hey all,

I am having some problems with changing the values with PVectors.  
Here is my code:

```auto
Rocket rocket = new Rocket();

void keyPressed() {
  switch(key) {
  case 'w': 
    rocket.up(); 
  case 's': 
    rocket.down();
  case 'd': 
    rocket.right();
  case 'a': 
    rocket.left();
  }
}

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

void draw() {
  background(0);
  rocket.show();
  rocket.updatePos();
}

```

And here is the rocket class:

```auto
class Rocket{
    PVector pos = new PVector(100, 100); 
    PVector acc = new PVector(0, 0);
    
    void show(){
      fill(0, 255, 0);
      ellipse(pos.x, pos.y, 8, 8);
    }
    
    void updatePos(){
      pos = new PVector(pos.x + acc.x, pos.y + acc.y);
    }
    
    void up(){
      acc.y--;
    }
    void down(){
      acc.y++;
    }
    void right(){
      acc.x++; 
    }
    void left(){
      acc.x--;  
    }
}

```

The rocket can move left and down just fine, but it can never move right or up. Any suggestions?

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [September 21, 2018, 4:19pm UTC](https://discourse.processing.org/t/solved-problems-with-pvectors/3715/2 "2018-09-21T16:19:57Z")

</div>

This took me way too long to figure out. I thought I was going crazy, because your class definition was perfectly fine and yet all four direction-methods kept being called at once.

Basically, you forgot your breaks in the switch-conditional!

```auto
void keyPressed() {
  switch(key) {
  case 'w': 
    rocket.up();
    break;
  case 's': 
    rocket.down();
    break;
  case 'd': 
    rocket.right();
    break;
  case 'a': 
    rocket.left();
    break;
  }
}

```

If you don’t call the break, the entire sequence gets called, so the accelerations were being cancelled out.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 24, 2018, 1:23pm UTC](https://discourse.processing.org/t/solved-problems-with-pvectors/3715/3 "2018-09-24T13:23:01Z")

</div>

Every now and then this fall-through behavior of switch can be useful (I first learned about it in loop unrolling and “Duff’s device”).

In Processing’s key methods you can use the switch fall-through behavior to concisely define groups of keys with the same functionality without writing a bunch of “or” statements:

```auto
  switch(key) {
  // up
  case UP: 
  case 'w': 
  case 'i': 
    forward();
    break;
  // down
  case DOWN: 
  case 's': 
  case 'k': 
    back();
    break;
  // action
  case ENTER:
  case RETURN:
  case ' ';
    action();
    break;
  }

```
