Trouble accessing class Variable

Started by
1 comment, last by captacha 12 years, 3 months ago
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]


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();
}
}
}
Advertisement
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.
void Input(SDL_Rect Rect)
{
...
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.
void Input(SDL_Rect& Rect)
{
...
Thanks so much for helping me with that. I'm still a begginner with pointers so please excuse the stupid mistake.

This topic is closed to new replies.

Advertisement