# Vectors not adding?

**URL:** <https://discourse.processing.org/t/vectors-not-adding/13900>\
**Category:** Coding Questions\
**Created:** [September 10, 2019, 4:04am UTC](https://discourse.processing.org/t/vectors-not-adding/13900 "2019-09-10T04:04:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![blue1](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@blue1](https://discourse.processing.org/u/blue1)\
**Post date:** [September 10, 2019, 4:04am UTC](https://discourse.processing.org/t/vectors-not-adding/13900/1 "2019-09-10T04:04:46Z")

</div>

//I cant figure out how to get the isMoving function to work, im kind of comfortable with p5js and trying to learn processing but it seems there are always small bugs i cant find.

```auto
Ground ground;
Character character;
PVector gravity = new PVector(0, 0.05);
float friction = 0.9;
void setup() {
	size(1500, 800);
	ground = new Ground();
	character = new Character(ground);
}

void draw() {
	rect(500, 500, 50, 50);
	while(keyPressed) {
		character.isMoving();
	}
	character.update(ground);
	character.show();
	ground.show();
}

class Ground {
	PVector pos;
	float w, h;
	
	Ground() {
		w = width;
		h = 100;
		pos = new PVector(0, height-h);
	}	

	void show() {
		fill(100, 100, 100);
		rect(pos.x, pos.y, w, h);
	}
}

class Character {
	PVector pos, vel, acc;
	int size;

	Character(Ground other) {
		size = 50;
		pos = new PVector(100, other.pos.y-size);
		vel = new PVector(0, 0);
		acc = new PVector(0, 0);
	}

	//why does it not move?
	void isMoving() {
		while(abs(vel.x) < 10) {
			if(keyCode == RIGHT) {
				PVector add = new PVector(1, 0);		
				acc.add(add);
			}
			else{acc.x *= friction;}
			

		}
	}

	void update(Ground other) {
		vel.add(acc);
		pos.add(vel);
		acc.mult(friction);

		while(pos.y < other.pos.y-size) {
			acc.add(gravity);
		}
	}

	void show() {
		fill(255, 0, 0);
		rect(pos.x, pos.y, size, size);
	}
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [September 10, 2019, 5:45am UTC](https://discourse.processing.org/t/vectors-not-adding/13900/2 "2019-09-10T05:45:58Z")

</div>

-a- for diagnostic can use

```auto
println("functionname");

```

and you will see that your code is blocked in the while loop of the Character.isMoving( )

-b- pls avoid WHILE loops and replace with IF or FOR

-c- pls use a

```auto
void keyPressed() {

}

```

instead keyPressed and keyCode inside draw or other function.

for RUNNING instead single step walking ( your while (keypressed) )  
can also use

```auto
void setup(){}
void draw(){
  background(200,200,0);
  text("goRight "+goRight,0,10);
}

boolean goRight = false;

void keyPressed() {
  if ( keyCode == RIGHT ) goRight = true;
}

void keyReleased() {
  if ( keyCode == RIGHT ) goRight = false;
}

```

---

<div class="post-metadata">

**Author:** ![blue1](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@blue1](https://discourse.processing.org/u/blue1)\
**Post date:** [September 10, 2019, 7:21am UTC](https://discourse.processing.org/t/vectors-not-adding/13900/3 "2019-09-10T07:21:54Z")

</div>

Thank you very much for the ideas, I fixed it and continuing to learn more
