Sign in to follow this  
Toshio

[SDL] Player won't move

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.

[code]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;
}[/code]

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

[code]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;
}
[/code]

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
[quote name='Toshio' timestamp='1318611318' post='4872581']
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.

[code]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;
}[/code]

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

[code]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;
}
[/code]

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

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:

[code]void Player::Move()
{
x += 1;
}[/code]

... 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
[quote name='Toshio' timestamp='1318623607' post='4872656']
Yes, the Player.Move(); is in the game Loop.
What I also tried, is this:

[code]void Player::Move()
{
x += 1;
}[/code]

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

Maybe try this in your code.
[code]
void Player::Move()
{
x += xVel;
}
[/code]

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
[quote name='rip-off' timestamp='1318691454' post='4872865']
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.
[/quote]

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this