Using while loop with enums.

Hey, I am trying to get an object in my console application to move itself continuously. I'm getting really stuck and I know its something basic that I should know!

I am just checking what key is pressed and then based on that I pass the direction into a function which will move the object. I want it to move continuously but I am having trouble with my code. It is only being called once.

I also tried using a while loop, which compiles but the screen is completely blank when I do so, I can only presume I was doing something wrong.

Any help/advice would be great thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Object::setDirection(Object::Direction dir)
{
		switch (dir)
		{
		case UP:
			Object[0].y -= 1;
			break;
	}
}
void Object::update()
{

	if (_kbhit())
	{
		switch (_getch())
		{
		case 'w':
			setDirection(UP);
			break;
		}
	}
Last edited on
Think I've found a solution!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Object::moveObject()
{
	if (_kbhit())
	{
		switch (_getch())
		{
		case 'w':
			dir = UP;
			break;
}
void Object::update()
{
if (dir == UP)
	{
		Object[0].y -= 1;
	}
}


I can then do this for all directions. Not sure if it is the most elegant way, let me know if it could be done better please!
dir = UP;

That would be one of the usual ways to change coordinates.

A more advanced way would be to use mathematical vectors, put perhaps that would be too much for you at this point.
Topic archived. No new replies allowed.