Jump to content

  • Log In with Google      Sign In   
  • Create Account


SDL_Events working in my main function but not in a class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 mcjohnalds45   Members   -  Reputation: 106

Like
0Likes
Like

Posted 14 April 2012 - 05:42 AM

First post, hooray! I'm trying to call onEvent (a function of my player class) every time I call SDL_PollEvent in my game loop to move my player up when I press W.

Part of player definition.
void Player::onEvent(SDL_Event Events)
{
	if (events.type == SDL_KEYDOWN)
	{
		switch (Events.key.keysym.sym)
		{
		case SDLK_w: ySpeed = -maxSpeed; break;
		}
	}
	if (events.type == SDL_KEYUP)
	{
		switch (Events.key.keysym.sym)
		{
		case SDLK_w: ySpeed = 0; break;
		}
	}
}

Part of my main function where onEvent is called.
while (SDL_PollEvent(&events))
{
	if (events.type == SDL_QUIT)
	run = false;
	player.onEvent(events);
}

I expect my player to move up when I press W but it does not, I am almost certain there is something wrong with onEvent's definition or how I am calling it because changing the above piece of code to the following makes the program run as expected.


while (SDL_PollEvent(&events))
{
	if (events.type == SDL_QUIT)
		run = false;
	if (events.type == SDL_KEYDOWN)
	{
		switch (events.key.keysym.sym)
		{
		case SDLK_w: player.ySpeed = -player.maxSpeed; break;
		}
	}
	if (events.type == SDL_KEYUP)
	{
		switch (events.key.keysym.sym)
		{
		case SDLK_w: player.ySpeed = 0; break;
		}
	}
}

Sorry for goofing up any terminology, I'm pretty new to c++.

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12020

Like
0Likes
Like

Posted 14 April 2012 - 05:51 AM

Use a debugger to see what happens when you call onEvent.

#3 SiCrane   Moderators   -  Reputation: 9432

Like
2Likes
Like

Posted 14 April 2012 - 06:02 AM

Is this your actual code? You seem to have a variable named events and another variable named Events. You probably want the events to be Events.

#4 sc2slash   Members   -  Reputation: 131

Like
1Likes
Like

Posted 14 April 2012 - 01:07 PM

Events instead of events in the function onEvent.

#5 Álvaro   Crossbones+   -  Reputation: 12020

Like
0Likes
Like

Posted 14 April 2012 - 02:12 PM

There's still the question of why `events' was a valid identifier in that context. It looks like it shouldn't be.

#6 mcjohnalds45   Members   -  Reputation: 106

Like
1Likes
Like

Posted 14 April 2012 - 08:11 PM

Problem solved, I made stupid mistake, I was using 'events' in my onEvent function instead of 'Events', thanks heaps for pointing that out.

(Note to self: Never code at 1 am ever again!)

#7 Álvaro   Crossbones+   -  Reputation: 12020

Like
0Likes
Like

Posted 14 April 2012 - 11:41 PM

What is `events' in that context? Chances are the answer to that question reveals some other problem with your code.

#8 mcjohnalds45   Members   -  Reputation: 106

Like
0Likes
Like

Posted 16 April 2012 - 06:12 AM

The non-capital events was valid because I had left accidentally left a variable that I wasn't using any more in my Player class definition, if I had remembered to delete that I wouldn't have had a problem, anyway, thanks a lot!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS