Jump to content
  • Advertisement
Sign in to follow this  
Toshio

[SDL] Player won't move

This topic is 2534 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I've got a problem I just can't seem to solve.
The problem is, my player just won't move.
Here is the code:

This is a part of my Player.cpp, where I handle input and move the player.

void Player::Handle_Input(SDL_Event* event)
{
if(event->type == SDL_KEYDOWN)
{
switch(event->key.keysym.sym)
{
case SDLK_RIGHT: xVel += 1; break;
case SDLK_LEFT: xVel -= 1; break;
}

}

else if(event->type == SDL_KEYUP)
{
switch(event->key.keysym.sym)
{
case SDLK_RIGHT: xVel -= 1; break;
case SDLK_LEFT: xVel += 1; break;
}

}
}

void Player::Move()
{
x += xVel;
}


This is my main.cpp. It handles events here.

int main(int argc, char* args[])
{
Game g;

Player Player;

SDL_Event event;

g.OnInit();

while(g.running)
{
while(SDL_PollEvent(&event))
{
g.OnEvent(&event);

Player.Handle_Input(&event);
}

g.OnLoop();
g.OnRender();
}

g.OnCleanup();

return 0;
}


Now, can somebody help me? I just can't make the player move. I'm out of ideas.
Thanks. :)

Share this post


Link to post
Share on other sites
Advertisement

Hello. I've got a problem I just can't seem to solve.
The problem is, my player just won't move.
Here is the code:

This is a part of my Player.cpp, where I handle input and move the player.

void Player::Handle_Input(SDL_Event* event)
{
if(event->type == SDL_KEYDOWN)
{
switch(event->key.keysym.sym)
{
case SDLK_RIGHT: xVel += 1; break;
case SDLK_LEFT: xVel -= 1; break;
}

}

else if(event->type == SDL_KEYUP)
{
switch(event->key.keysym.sym)
{
case SDLK_RIGHT: xVel -= 1; break;
case SDLK_LEFT: xVel += 1; break;
}

}
}

void Player::Move()
{
x += xVel;
}


This is my main.cpp. It handles events here.

int main(int argc, char* args[])
{
Game g;

Player Player;

SDL_Event event;

g.OnInit();

while(g.running)
{
while(SDL_PollEvent(&event))
{
g.OnEvent(&event);

Player.Handle_Input(&event);
}

g.OnLoop();
g.OnRender();
}

g.OnCleanup();

return 0;
}


Now, can somebody help me? I just can't make the player move. I'm out of ideas.
Thanks. :)


you don't seem to call player.Move(); anywhere. (I'd expect that to be done in g.OnLoop, but you don't seem to pass your player to your game object at any point)

Share this post


Link to post
Share on other sites
Yes, the Player.Move(); is in the game Loop.
What I also tried, is this:

void Player::Move()
{
x += 1;
}


... which did move the player. That's why I suspect something is wrong with Handling the Input.

Share this post


Link to post
Share on other sites
Hidden

Yes, the Player.Move(); is in the game Loop.
What I also tried, is this:

void Player::Move()
{
x += 1;
}


... which did move the player. That's why I suspect something is wrong with Handling the Input.


Maybe try this in your code.

void Player::Move()
{
x += xVel;
}

Share this post


Link to post
try using sdl_event instead of sdl_event* for your function. I feel like youre supposed to be calling event.dothis instead of event->dothis.

I also assume you call the move function in g.OnEvent, so Itd be cool if you showed us that. ;D I think thats what the poster above was talkin 'bout: you dont call Player::Move() in main.cpp.

Share this post


Link to post
Share on other sites
The Player you have in main() is different from the one in the game class. The solution is to pass the player from main() into Game, or to not declare a Player in main, and to pass the events into the Game class, which can forward them to the player.

Share this post


Link to post
Share on other sites

The Player you have in main() is different from the one in the game class. The solution is to pass the player from main() into Game, or to not declare a Player in main, and to pass the events into the Game class, which can forward them to the player.


Thank You!
I didn't declare Player in main, and I put events in the Game class, and it works perfectly now!
:D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!