• Create Account

### #ActualKhatharr

Posted 25 January 2013 - 01:15 AM

If you arrange your sprite sheet so that rows represent directions to face and columns represent steps being taken then you can keep your direction separate from the animation step and just calculate your source rect x/y independently.

http://imageshack.us/photo/my-images/843/bigsprites.jpg/sr=1

If no direction is held then set the animation counter to zero. Otherwise set the direction and increment the animation counter. You can loop the animation by modulating it whenever you increment it:

int main() {
int x = 0;
int y;
for(y = 0; y < 16; ++y) {
++x;
x %= 4;
printf("X is %i.\n", x);
}
}

It may also help both readability and utility if you implement a direction function:

enum DIRECTION {
DIR_DOWN,
DIR_UP,
DIR_LEFT,
DIR_RIGHT,
DIR_NONE
};

DIRECTION dir4() {
if(keystate[SDLK_DOWN]) return DIR_DOWN;
if(keystate[SDLK_UP]) return DIR_UP;
if(keystate[SDLK_LEFT]) return DIR_LEFT;
if(keystate[SDLK_RIGHT]) return DIR_RIGHT;
return DIR_NONE;
}


Remember that you can optionally assign values to enumeration keys:

enum STUFF {
THINGY = 1,
CRUD = 2,
HERP = 17
};

An alternative to all this would be to simply detect the circumstance where the player is holding two directions and pop up a window that says

"Electric Slide Bonus

555 points

You dancin' GOOD!"

### #5Khatharr

Posted 25 January 2013 - 01:07 AM

If you arrange your sprite sheet so that rows represent directions to face and columns represent steps being taken then you can keep your direction separate from the animation step and just calculate your source rect x/y independently.

http://imageshack.us/photo/my-images/843/bigsprites.jpg/sr=1

If no direction is held then set the animation counter to zero. Otherwise set the direction and increment the animation counter. You can loop the animation by modulating it whenever you increment it:

int main() {
int x = 0;
int y;
for(y = 0; y < 16; ++y) {
++x;
x %= 4;
printf("X is %i.\n", x);
}
}

It may also help both readability and utility if you implement a direction function:

enum DIRECTION {
DIR_DOWN,
DIR_UP,
DIR_LEFT,
DIR_RIGHT,
DIR_NONE
};

DIRECTION dir4() {
if(keystate[SDLK_DOWN]) return DIR_DOWN;
if(keystate[SDLK_UP]) return DIR_UP;
if(keystate[SDLK_LEFT]) return DIR_LEFT;
if(keystate[SDLK_RIGHT]) return DIR_RIGHT;
return DIR_NONE;
}


Remember that you can optionally assign values to enumeration keys:

enum STUFF {
THINGY = 1,
CRUD = 2,
HERP = 17
};

### #4Khatharr

Posted 25 January 2013 - 01:06 AM

If you arrange your sprite sheet so that rows represent directions to face and columns represent steps being taken then you can keep your direction separate from the animation step and just calculate your source rect x/y independently.

http://imageshack.us/photo/my-images/843/bigsprites.jpg/sr=1

If no direction is held then set the animation counter to zero. Otherwise set the direction and increment the animation counter. You can loop the animation by modulating it whenever you increment it:

int main() {
int x = 0;
int y;
for(y = 0; y < 16; ++y) {
++x;
x %= 4;
printf("X is %i.\n", x);
}
}

It may also help both readability and utility if you implement a direction function:

enum DIRECTION {
DIR_DOWN,
DIR_UP,
DIR_LEFT,
DIR_RIGHT,
DIR_NONE
};

int dir4() {
if(keystate[SDLK_DOWN]) return DIR_DOWN;
if(keystate[SDLK_UP]) return DIR_UP;
if(keystate[SDLK_LEFT]) return DIR_LEFT;
if(keystate[SDLK_RIGHT]) return DIR_RIGHT;
return DIR_NONE;
}


Remember that you can optionally assign values to enumeration keys:

enum STUFF {
THINGY = 1,
CRUD = 2,
HERP = 17
};

### #3Khatharr

Posted 25 January 2013 - 12:46 AM

If you arrange your sprite sheet so that rows represent directions to face and columns represent steps being taken then you can keep your direction separate from the animation step and just calculate your source rect x/y independently.

http://imageshack.us/photo/my-images/843/bigsprites.jpg/sr=1

If no direction is held then set the animation counter to zero. Otherwise set the direction and increment the animation counter. You can loop the animation by modulating it whenever you increment it:

int main() {
int x = 0;
int y;
for(y = 0; y < 16; ++y) {
++x;
x %= 4;
printf("X is %i.\n", x);
}
}


### #2Khatharr

Posted 25 January 2013 - 12:35 AM

If you arrange your sprite sheet so that rows represent directions to face and columns represent steps being taken then you can keep your direction separate from the animation step and just calculate your source rect x/y independently. If no direction is held then set the animation counter to zero. Otherwise set the direction and increment the animation counter.

http://imageshack.us/photo/my-images/843/bigsprites.jpg/sr=1

### #1Khatharr

Posted 25 January 2013 - 12:32 AM

If you arrange your sprite sheet so that rows represent directions to face and columns represent steps being taken then you can keep your direction separate from the animation step and just calculate your source rect x/y independently.

http://imageshack.us/photo/my-images/843/bigsprites.jpg/sr=1

PARTNERS