initializing varibles in game constructor

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
krrice
Posts: 6
Joined: February 7th, 2012, 2:51 am

initializing varibles in game constructor

Post by krrice » February 7th, 2012, 3:04 am

I have written some code to draw a line and it works if I declare and initialize x and y in ComposeFrame
but if I declare in game.h and initialize in game.cpp constructor it crashes and it shows that y is at -1.

This is the code

Code: Select all


	for( int i = 0;i < 150;i++ ){
	gfx.PutPixel( x,y,255,0,0 );
	x--;
	y--;
Thank you for doing these tutorials they are great.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: initializing varibles in game constructor

Post by chili » February 7th, 2012, 3:49 am

If you declare and initialize it in ComposeFrame(), you are resetting its value every frame. If you declare it as a member of the Game object and initialize it in the constructor, it retains its value from frame to frame, since the contructor is only called once at the beginning of the program.

Since you're decrementing 150 times every frame, if you don't reset the value of your y variable, it will soon reach -1 and be out of bounds of the screen.

Hope this helps! :)
Chili

Post Reply