how would i change this so that player follows..
Jul 27, 2012 at 3:55am Jul 27, 2012 at 3:55am UTC
i am currently trying to get the player to follow the mouse in SDL, at the moment i am trying to change from using keys to move a player to the mouse but i'm not sure what i need to do to get it to use the mouse instead of the keys...here is what the code looks like and what i would like it to do,...
instead of SDLK_UP etc... i want to use a function called GetMousePos()...which moves the player in the direction and place the mouse is currently hovering over....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
void Player::Input()
{
Uint8* keysArray = SDL_GetKeyState(NULL);
{
if ( keysArray[ SDLK_UP ] == true )
{
WhichDirection = Up;
}
else if ( keysArray[ SDLK_DOWN ] == true )
{
WhichDirection = Down;
}
else if ( keysArray[ SDLK_RIGHT ] == true )
{
WhichDirection = Right;
}
else if ( keysArray[ SDLK_LEFT ] == true )
{
WhichDirection = Left;
}
else
{
WhichDirection = NoKeyPressed;
}
}
}
void Player::Update()
{
Input();
ResetPos();
Speed();
//AboveOrBelow();
if (WhichDirection == Up)
{
m_Pos.y -= PlayerSpeed;
}
else if (WhichDirection == Down)
{
m_Pos.y += PlayerSpeed;
}
else if (WhichDirection == Right)
{
m_Pos.x += PlayerSpeed;
}
else if (WhichDirection == Left)
{
m_Pos.x -= PlayerSpeed;
}
else if ( WhichDirection == NoKeyPressed )
{
PlayerSpeed = 0;
}
}
void Player::ResetPos()
{
if (m_Pos.x + m_WIDTH >= 1024)
{
m_Pos.x = 512;
m_Pos.y = 398;
}
else if (m_Pos.x <= 0)
{
m_Pos.x = 512;
m_Pos.y = 384;
}
if (m_Pos.y + m_WIDTH >= 768)
{
m_Pos.x = 512;
m_Pos.y = 398;
}
else if (m_Pos.y <= 0)
{
m_Pos.x = 512;
m_Pos.y = 384;
}
}
void Player::Speed()
{
if ( WhichDirection != NilMove && WhichDirection == Left)
{
PlayerSpeed -= PlayerSpeed - SpeedDecrease;
}
else if ( WhichDirection != NilMove && WhichDirection == Right)
{
PlayerSpeed -= PlayerSpeed - SpeedDecrease;
}
else if ( WhichDirection != NilMove && WhichDirection == Up)
{
PlayerSpeed -= PlayerSpeed - SpeedDecrease;
}
else if ( WhichDirection != NilMove && WhichDirection == Down)
{
PlayerSpeed -= PlayerSpeed - SpeedDecrease;
}
}
does anyone know anywhere were i could find some tutorials on how to use the mouse in sdl..or any advice as to what i would need to be able to implement such features?
Jul 27, 2012 at 8:10am Jul 27, 2012 at 8:10am UTC
Last edited on Jul 27, 2012 at 8:10am Jul 27, 2012 at 8:10am UTC
Topic archived. No new replies allowed.