Beginner lesson 5

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
thefatshizms
Posts: 8
Joined: May 5th, 2013, 3:00 pm

Beginner lesson 5

Post by thefatshizms » May 5th, 2013, 6:41 pm

Hello, in the fifth tutorial chili has a slight problem where when he wants to "freeroam" he can't because the variable is being reset back to 400 (frame updating) he then goes onto fix it by making the variables in the class and assigning them in the constructor. Prior to this, I tried to fix it by myself (I already knew some languages prior to learning c++) and went ahead and did this:

Code: Select all

void Game::ComposeFrame()
{
	static int x, y;

	if(kbd.LeftIsPressed())  {
		x-=5;
	}
	
	if(kbd.RightIsPressed()) {
		x+=5;
	}
	
	if(kbd.DownIsPressed()) {
		y+=5;
	}
	
	if(kbd.UpIsPressed()) {
		y-=5;
	}

	if(x >= 390) {
		x = 390;	
	}

	if(y >= 240) {
		y = 240;
	}

	if(x <= -390) {
		x = -390;	
	}

	if(y <= -240) {
		y = -240;	
	}

	gfx.PutPixel(400 + x, 300 + y, 255, 255, 255);
	gfx.PutPixel(401 + x, 300 + y, 255, 255, 255);
	gfx.PutPixel(402 + x, 300 + y, 255, 255, 255);
	gfx.PutPixel(403 + x, 300 + y, 255, 255, 255);
	gfx.PutPixel(404 + x, 300 + y, 255, 255, 255);
	gfx.PutPixel(405 + x, 300 + y, 255, 255, 255);

	gfx.PutPixel(399 + x, 300 + y, 255, 255, 255);
	gfx.PutPixel(398 + x, 300 + y, 255, 255, 255);
	gfx.PutPixel(397 + x, 300 + y, 255, 255, 255);
	gfx.PutPixel(396 + x, 300 + y, 255, 255, 255);
	gfx.PutPixel(396 + x, 300 + y, 255, 255, 255);

	gfx.PutPixel(400 + x, 301 + y, 255, 255, 255);
	gfx.PutPixel(400 + x, 302 + y, 255, 255, 255);
	gfx.PutPixel(400 + x, 303 + y, 255, 255, 255);
	gfx.PutPixel(400 + x, 304 + y, 255, 255, 255);
	gfx.PutPixel(400 + x, 305 + y, 255, 255, 255);

	gfx.PutPixel(400 + x, 299 + y, 255, 255, 255);
	gfx.PutPixel(400 + x, 298 + y, 255, 255, 255);
	gfx.PutPixel(400 + x, 297 + y, 255, 255, 255);
	gfx.PutPixel(400 + x, 296 + y, 255, 255, 255);
	gfx.PutPixel(400 + x, 295 + y, 255, 255, 255);
}


As you can see, I still declare the local variables within the scope but I set them as static variables (Not losing their value even after the function has finished) and this seems to fix it. My question is, my fix is simpler but is it better? Is it better to set it once in the constructor or to use a static variable?

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Beginner lesson 5

Post by LuX » May 6th, 2013, 12:08 pm

The way you use it in this case is good. Probably better if you use this method.

The problem comes when you need to access the x and y variables from another class. Not so good any more is it? For simplicity chili's method is easier to understand and much more logical based on the information learned so far, but both should be used in their appropriate way.
ʕ •ᴥ•ʔ

thefatshizms
Posts: 8
Joined: May 5th, 2013, 3:00 pm

Re: Beginner lesson 5

Post by thefatshizms » May 6th, 2013, 1:42 pm

Thanks for the reply, I'll keep that in mind :)

Post Reply