Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Trouble with Sprite objects and syncing

Author
Message
zerth
13
Years of Service
User Offline
Joined: 20th Apr 2011
Location:
Posted: 21st Apr 2011 03:25
Hello all, I've been working on a simple game using the Sprite and MoveableSprite objects from the Gaddis textbook, and I'm having a weird issue getting my Sprite background to draw properly. If I could show you some examples...



The above code works perfectly and simply displays my image as a sprite background. However, if I add a conditional, something gets upset. Changing the contents of the game loop to this...



...gives me nothing but a black screen. Obviously I'd like to make that conditional something relevant to the game, but I don't understand why it's not drawing the background even when the conditional always passes. I feel like I'm making a really silly error, or that there is something about the sync function that I don't understand. Thanks for any help you can provide!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 22nd Apr 2011 12:00 Edited at: 22nd Apr 2011 12:38
The brackets of the "if" statement make a new variable scope. If you create a local variable within the "if" brackets, it doesn't exist any more after the "if" statement. Your Sprite variable gets out of scope and gets deleted. For example, try this code:



It doesn't even compile because x doesn't exist outside the "if" statement, so the compiler will say "undeclared identifier".

So the solution is to create your Sprite before the "if" statement.

EDIT: If the sprite is not needed outside the "if", then you have a bigger problem because you must find a structure for your program that allows you to create and delete resources based on the current game phase (e.g. different resources are needed for title screen and game play). It is really a question of program design. Also keep in mind that recreating a sprite every loop is not the best solution, even if it works. I suppose that Sprite is some kind of class which calls a constructor with that image, it's a waste of time to do that in every loop again.
zerth
13
Years of Service
User Offline
Joined: 20th Apr 2011
Location:
Posted: 22nd Apr 2011 22:48
Okay, this was helpful, I didn't realize it was a matter of variable scope. The basic idea was to create a simple framework for a three level game (plus a title screen), where the program could switch between levels and other states based on the player's performance (or lack thereof). I'll do a little more pencil-work and see if I can map out something that works better at managing my variables and assets.

Login to post a reply

Server time is: 2024-11-19 10:25:35
Your offset time is: 2024-11-19 10:25:35