Ok, so I did a tutorial on basic animation and then I made a sprite map and altered the program to use the numpad to go in all 8 directions (tut was 4). This all worked just peachy, then I decided to try and alter it again to chase the mouse around the screen, which is does but if it goes to the right or bot of the screen it locks up.
Code:
void SPRITE::HandleEvent(SDL_Event* event)
{
switch(event->type)
{
case SDL_QUIT:
{
Running = false;
break;
}
case SDL_MOUSEMOTION:
{
MousePosition(event);
break;
}
default: break;
}
}
bool SPRITE::MousePosition(SDL_Event* event)
{
int mX = event->motion.x;
int mY = event->motion.y;
if(mX != mouseX || mY != mouseY)
{
mouseX = mX;
mouseY = mY;
return true;
}
return false;
}
void SPRITE::ChaseMouse()
{
int direction = 0;
int moveX, moveY;
// is sprite at cursor?
while( (x != mouseX) || (y != mouseY))
{
moveX = ( mouseX - x );
moveY = ( mouseY - y );
// decide which direction to move towards cursor
if(moveX > 0 && moveY > 0)
direction = 3;
else if(moveX > 0 && moveY < 0)
direction = 9;
else if(moveX < 0 && moveY > 0)
direction = 1;
else if(moveX < 0 && moveY < 0)
direction = 7;
else if(moveX > 0)
direction = 6;
else if(moveX < 0)
direction = 4;
else if(moveY > 0)
direction = 2;
else if(moveY < 0)
direction = 8;
// Set velocity and section of spritemap
switch(direction)
{
case 8: {
yVel -= SPRITE_HEIGHT / 63;
section.x = 65;
section.y = 0;
break;
}
case 2: {
yVel += SPRITE_HEIGHT / 63;
section.x = 65;
section.y = 130;
break;
}
case 4: {
xVel -= SPRITE_WIDTH / 63;
section.x = 0;
section.y = 65;
break;
}
case 6: {
xVel += SPRITE_WIDTH / 63;
section.x = 130;
section.y = 65;
break;
}
case 7: {
xVel -= SPRITE_WIDTH / 63;
yVel -= SPRITE_HEIGHT / 63;
section.x = 0;
section.y = 0;
break;
}
case 9: {
xVel += SPRITE_WIDTH / 63;
yVel -= SPRITE_HEIGHT / 63;
section.x = 130;
section.y = 0;
break;
}
case 1: {
xVel -= SPRITE_WIDTH / 63;
yVel += SPRITE_HEIGHT / 63;
section.x = 0;
section.y = 130;
break;
}
case 3: {
xVel += SPRITE_HEIGHT / 63;
yVel += SPRITE_HEIGHT / 63;
section.x = 130;
section.y = 130;
break;
}
default: break;
}
Move();
switch( direction )
{
case 8: yVel += SPRITE_HEIGHT / 63; break;
case 2: yVel -= SPRITE_HEIGHT / 63; break;
case 4: xVel += SPRITE_WIDTH / 63; break;
case 6: xVel -= SPRITE_WIDTH / 63; break;
case 7: xVel += SPRITE_WIDTH/63; yVel += SPRITE_HEIGHT/63; break;
case 9: xVel -= SPRITE_WIDTH/63; yVel += SPRITE_HEIGHT/63; break;
case 1: xVel += SPRITE_WIDTH/63; yVel -= SPRITE_HEIGHT/63; break;
case 3: xVel -= SPRITE_WIDTH/63; yVel -= SPRITE_HEIGHT/63; break;
default: break;
}
}
section.x = 65;
section.y = 65;
Move();
}
bool SPRITE::Execute(TIMER& GameTimer, SDL_Event* event)
{
Running = true;
if(Initialize() == false)
{
return -1;
}
while(Running)
{
GameTimer.Start();
SDL_PollEvent(event);
{
HandleEvent(event);
}
ChaseMouse();
if( GameTimer.Get_Ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - GameTimer.Get_Ticks() );
}
GameTimer.Stop();
}
SDL_Quit();
return 0;
}
void SPRITE::Move()
{
//Move the sprite left or right
x += xVel;
//If the sprite went too far to the left or right
if( ( x < 0 ) || ( x + SPRITE_WIDTH > SCREEN_WIDTH ) )
{
//move back
x -= xVel;
}
//Move the sprite up or down
y += yVel;
//If the sprite went too far up or down
if( ( y < 0 ) || ( y + SPRITE_HEIGHT > SCREEN_HEIGHT ) )
{
//move back
y -= yVel;
}
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
//Show the sprite on the screen
Show();
//Update the screen
SDL_Flip( screen );
}
void SPRITE::Show()
{
//Show the sprite ApplySurface( sprite, section, screen, x, y );
}
This is all the relevant code. I tried changing the right and bot boundries in Move(), but it just locked up farther away from the edge. Being ignorant while learning is such a pain.
Any feedback on the ChaseMouse() and MousePosition() funcs would be cool as well, as these are mostly mine. Please feel free to straighten me out on what I did wrong and how. EDIT:: My commenting is really poor, this is something I need to work on, thanks for your patience.