Sign in to follow this  

Trouble accessing class Variable

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

I've been working on some 2D Movement in my games for the last few weeks and i'm using the SDL game libary for this game. I have a [font=courier new,courier,monospace]Player[font=arial,helvetica,sans-serif] class with a SDL_Rect embedded in it for tracking x,y coordinates. The problem is that my Player will not move because my [font=courier new,courier,monospace]Input()[font=arial,helvetica,sans-serif] method does not change the X Value. After doing some debugging, I figured out that my [font=courier new,courier,monospace]Input()[font=arial,helvetica,sans-serif]method[/font][/font] works and my [font=courier new,courier,monospace]Draw()[font=arial,helvetica,sans-serif]method also works, but the [font=courier new,courier,monospace]Input()[font=arial,helvetica,sans-serif]method does not change the values at all.[/font][/font][/font][/font][/font][/font][/font][/font]

[font=courier new,courier,monospace][font=arial,helvetica,sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif][font=courier new,courier,monospace]Input():[/font][/font][/font][/font][/font][/font][/font][/font][/font]

[CODE]
void Input(SDL_Rect Rect)
{
SDL_Event Event;
while(SDL_PollEvent(&Event))
{

if(Event.type == SDL_KEYDOWN)
{
if(Key == SDLK_LEFT)
{
Rect.x--;

}

if(Key == SDLK_RIGHT)
{
Rect.x++;
}
}
if(Event.type == SDL_QUIT)
{
Exit();
}
}
}
[/CODE]

Share this post


Link to post
Share on other sites
Hidden
You pass the SDL_Rect by value so the changes to Rect will not be visible from outside the function. Instead pass a reference or a pointer.
[code]void Input(SDL_Rect Rect)
{
...[/code]

Share this post


Link to post
You pass the SDL_Rect by value so the changes to Rect will not be visible from outside the function. Instead pass a reference or a pointer.
[code]void Input(SDL_Rect& Rect)
{
...[/code]

Share this post


Link to post
Share on other sites
Thanks so much for helping me with that. I'm still a begginner with pointers so please excuse the stupid mistake.

Share this post


Link to post
Share on other sites
Sign in to follow this