# Thread: Character moving algo

1. ## Simplier way of doing this

I feel like I am posting too much in this board; I appreciate you guys graciously dealing with my ignorance. Anyway I am stumbling around with this DirectX thing and like you have all said it is easier than it first appeared, but alas to the point of this post.

In order to learn DirectX I have decided to remake a simplified version of the 8 bit NES Mario brothers game. As all of you undoubtedly remember Mario can do a lot of things like move left and right, jump left and right, and squat facing left and right. My code is currently getting really sloppy when it is coming to selecting the correct sprite to render to the screne and I was hoping someone might have a better way of doing things, or at least point me in the right direction.

Currently I have loaded all the mario sprites into a structure that is defined as such:

Code:
```struct _marioSprite {
_Sprite Mario[10];
int xPos, yPos, currentMario;
D3DXMATRIX d3dMario;
bool bBig;
}marioSprite;```
Where currentMario is an integer between 0 and 9 which selects which sprite gets rendered. Now the "fun part" starts here with the slop you can kind of call a function that takes the message and WPARAM from the message loop:

Code:
```void marioEngine::marioMove (WPARAM wParam, UINT message) {

static int vector;
static int offset;

switch(wParam) {

case VK_LEFT:
vector = -1;
offset = 5;
break;
case VK_RIGHT:
vector = 1;
offset = 0;
break;
case VK_DOWN:
marioSprite.currentMario = offset + 4;
marioSprite.yPos = 1300;
break;
}
if(message == WM_KEYUP) {
if(wParam == VK_DOWN)
marioSprite.yPos = 1240;
marioSprite.currentMario  = 0 + offset;
}else if(marioSprite.currentMario == (0 + offset)){
marioSprite.currentMario++;
marioSprite.xPos = marioSprite.xPos + (1 * vector);
}else if(marioSprite.currentMario == (1 + offset)) {
marioSprite.currentMario++;
marioSprite.xPos = marioSprite.xPos + (2 * vector);
}else if(marioSprite.currentMario == (2+offset)){
marioSprite.currentMario--;
marioSprite.xPos = marioSprite.xPos + (2 * vector);
}
_bRedraw = true;
}```
Now vector holds whether or not Mario moves right or left and is set under the VK_RIGHT and VK_LEFT cases above. Offset represents the number of places to move in the mario array to switch between right and left facing sprites. As you can see when you press left offset becomes 5 which starts you off at left facing sprites. The whole
Code:
```else if(marioSprite.currentMario == (1 + offset)) {
marioSprite.currentMario++;
marioSprite.xPos = marioSprite.xPos + (2 * vector);
}else if(marioSprite.currentMario == (2+offset)){
marioSprite.currentMario--;
marioSprite.xPos = marioSprite.xPos + (2 * vector);```
thing simply switches between the 2 running sprites I have and causes the animation effect. I am currently removing the hardcoded values (1240 and 1300). They were simply because a sqatting mario is smaller than a standing mario and mario still needed to touch the ground. I am just going to lengthen my sprite vertically to get around the adjusting yPos thing.

As you can see the code is pretty hairy and introduces alot of room for bugs. Any comments or pointers will be greatly appreciated.

And of course Happy Holidays

2. Well you need to look into using vectors or arrays.

3. vector as in D3DXVECTOR, or vector as in move right/left. I am afraid I am just not quite following. Thankyou for entertaining my ignorance.

4. Vectors as in arrays. I just recently learned how to use them, and they are handy.