Sign in to follow this  

[SDL] Player moves twice on one command

This topic is 3341 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

Hey. I've got an issue where a player moves twice when I press an arrow key once... Just wondering what is causing it to happen as I haven't used SDL in a while.
while(!exit)
	{
		while(SDL_PollEvent(&event_))
		{
			if(event_.type == SDL_QUIT)
			{
				exit = true;
				break;
			}

			level.Update(&event_);
			level.Draw(renderer);

			renderer.Draw_Screen();
		}
	}
void Level::Update(const SDL_Event* event_)
{
	if(party) party->Update(event_);
}
void Party::Update(const SDL_Event* event_)
{
	for(member_it it = members.begin(); it != members.end(); ++it)
	{
		(*it)->Update(event_);
	}
}
void Player::Update(const SDL_Event* event_)
{
	if(!event_) return;

	controller.Do_Logic(*this, event_);
}
void Player_Controller::Do_Logic(Player& player, const SDL_Event* event_)
{
	using namespace Direction;
	using namespace SDL_Tools;

	DIRECTION attempted_dir = get_direction_from_sym(event_->key.keysym);

	if(attempted_dir != NO_DIRECTION && attempted_dir != START)
	{ // Tried to move.
		Point attempted_pos = move_by(player.Position(), attempted_dir, 32);
		if(player.Current_Level().Can_Walk(attempted_pos))
		{ // Can move to attempted position; move.
			player.Move(attempted_dir);
		}
	}
}
Cheers.

Share this post


Link to post
Share on other sites
You're not checking the type of the event that you pass to the player controller, so you're probably moving once when you press the key, and once when you release the key. You probably want to check if the event is a SDL_KEYDOWN event before moving the player.

Share this post


Link to post
Share on other sites
Quote:
Original post by Perost
You're not checking the type of the event that you pass to the player controller, so you're probably moving once when you press the key, and once when you release the key. You probably want to check if the event is a SDL_KEYDOWN event before moving the player.
Ahh yes that's it. Cheers. :)

Share this post


Link to post
Share on other sites

This topic is 3341 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.

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