Sign in to follow this  
Dark_Glitch

SDL: Control Problems

Recommended Posts

Dark_Glitch    138
Hello again. This time, I decided to try to make a program that would behave similar to games such as The Guardian Legend. TGL had a labyrinth mode that let the player move in 8 directions, each with its own set of animations. I have used LazyFoo's tutorials and everything works fine except the following: Currently, when I use a joystick, the character will move in a certain direction, lets say down. But as soon as the joystick is centered, in the designated Neutral position, the character will begin to move up and to the left when it's supposed to stay still! If you attempt to move the joystick again, the freaking character will move only in diagonals... This is the function that handles the joystick input

void Player::handle_Events()
{
	if(window_Events.type == SDL_JOYAXISMOTION)
	{
		if(window_Events.jaxis.which == 0)		// Joytick 0 or Player One
		{
			if(window_Events.jaxis.axis == 0)	// X Axis
			{
				if( (window_Events.jaxis.value > -8000) && (window_Events.jaxis.value < 8000) )
				{
					Hvelocity = 0;
				}
				
				if(window_Events.jaxis.value > 8000)		// RIGHT
				{
					Hvelocity = 5;
				}

				if(window_Events.jaxis.value < 8000)		// LEFT
				{
					Hvelocity = -5;
				}
			}

			if(window_Events.jaxis.axis == 1)	// Y Axis
			{
				if( (window_Events.jaxis.value > -8000) && (window_Events.jaxis.value < 8000) )
				{
					Vvelocity = 0;
				}
				
				if(window_Events.jaxis.value > 8000)		// DOWN
				{
					Vvelocity = 5;
				}

				if(window_Events.jaxis.value < 8000)		// UP
				{
					Vvelocity = -5;
				}
			}
		}
	}
} // end of void Player::handle_Events()


And this, for your viewing pleasure, is the function that controls what animation should be shown based on a set of enumerated values that are quite self-explanatory.

void Player::show()
{
	if(Hvelocity < 0)			// If player is moving to the LEFT
	{
		pStatus = Player::LEFT;
		lastStatus = Player::LEFT;
		frame++;
	}

	if(Hvelocity > 0)			// If player is moving to the RIGHT
	{
		pStatus = Player::RIGHT;
		lastStatus = Player::RIGHT;
		frame++;
	}

	if(Vvelocity > 0)			// If player is moving DOWN
	{
		pStatus = Player::DOWN;
		lastStatus = Player::DOWN;
		frame++;
	}

	if(Vvelocity < 0)			// If player is moving UP
	{
		pStatus = Player::UP;
		lastStatus = Player::UP;
		frame++;
	}

	if(Vvelocity == 0 && Hvelocity == 0)
	{
		frame = 0;
		pStatus = Player::NEUTRAL;
	}

	if(Vvelocity > 0 && Hvelocity > 0)		// Diagonal Down RIGHT
	{
		pStatus = Player::DOWNRIGHT;
		lastStatus = Player::DOWNRIGHT;
		frame++;
	}

	if(Vvelocity > 0 && Hvelocity < 0)		// Diagonal Down LEFT
	{
		pStatus = Player::DOWNLEFT;
		lastStatus = Player::DOWNLEFT;
		frame++;
	}

	if(Vvelocity < 0 && Hvelocity > 0)		// Diagonal Up RIGHT
	{
		pStatus = Player::UPRIGHT;
		lastStatus = Player::UPRIGHT;
		frame++;
	}

	if(Vvelocity < 0 && Hvelocity > 0)		// Diagonal Up LEFT
	{
		pStatus = Player::UPLEFT;
		lastStatus = Player::UPLEFT;
		frame++;
	}

	if(frame > 3)
	{
		frame = 0;
	}

	if(pStatus == Player::UP)
	{
		apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &Up[frame]);
	}

	if(pStatus == Player::DOWN)
	{
		apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &Down[frame]);
	}

	if(pStatus == Player::LEFT)
	{
		apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &Left[frame]);
	}

	if(pStatus == Player::RIGHT)
	{
		apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &Right[frame]);
	}

	if(pStatus == Player::UPRIGHT)
	{
		apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &UpRight[frame]);
	}

	if(pStatus == Player::UPLEFT)
	{
		apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &UpLeft[frame]);
	}

	if(pStatus == Player::DOWNRIGHT)
	{
		apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &DownRight[frame]);
	}

	if(pStatus == Player::DOWNLEFT)
	{
		apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &DownLeft[frame]);
	}

	if(pStatus == Player::NEUTRAL)
	{
		if(lastStatus == Player::UP)
		{
			apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &Up[frame]);
		}

		if(lastStatus == Player::DOWN)
		{
			apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &Down[frame]);
		}

		if(lastStatus == Player::LEFT)
		{
			apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &Left[frame]);
		}

		if(lastStatus == Player::RIGHT)
		{
			apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &Right[frame]);
		}

		if(pStatus == Player::UPRIGHT)
		{
			apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &UpRight[frame]);
		}

		if(pStatus == Player::UPLEFT)
		{
			apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &UpLeft[frame]);
		}

		if(pStatus == Player::DOWNRIGHT)
		{
			apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &DownRight[frame]);
		}

		if(pStatus == Player::DOWNLEFT)
		{
			apply_Surface(Player::offSetX, Player::offSetY, guardian, screen, &DownLeft[frame]);
		}
	}
} // end of void Player::show()


Some things I noticed: On first run, when I tilt the stick to the right, the character moves right, but when I let go, it moves left. After that, if I try to move it again, it wigs out and the diagonal movement starts. Some of the animations are screwed up. i'll have to check my clips.

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