Advertisement

[SDL] Player won't move

Started by October 14, 2011 04:55 PM
4 comments, last by Toshio 13 years, 1 month ago
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. :)

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)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Advertisement
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.

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;
}
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.
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.
Advertisement

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

This topic is closed to new replies.

Advertisement