Jump to content
  • Advertisement
Sign in to follow this  
Ekim_Gram

SdlDotNet TextSprite problems

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

Honestly, I'm enjoying C# and SdlDotNet, it's absolutely amaazing at how well it was structured. To familiarize myself with both the language and the API, I'm making a Pong clone, shouldn't take too long, right? Well, I'm starting off with writing a little menu to choose what type of game play the user wants (1 Player, 2 Player, Quit). I'm using TextSprites and strings to manage the text that there is on the menu and I'm attempting to use a SpriteCollection to draw all those TextSprites. I have the following code: Menu.cs
using System;

using SdlDotNet;
using SdlDotNet.Sprites;

namespace CSharp_Pong
{
	class Menu
	{
		#region fonts, text strings, and surfaces
		private Font menu_font,title_font;
		private SpriteCollection text_sprites;
		private string font_filepath = @"../../data/fonts/";
		private string[] menu_options = {"1 Player", "2 Players", "Quit"};
		private TextSprite[] menu_text;
		private TextSprite title_text;
		#endregion
		
		#region ctor
		public Menu()
		{
			// Font inits
			menu_font = new Font(font_filepath + "FreeSans.ttf", 16);
			title_font = new Font(font_filepath + "FreeSans.ttf", 32);

			text_sprites = new SpriteCollection();

			// TextSprite inits
			for (int i=0; i<menu_options.Length; i++)
			{
				// Loop through the menu_options array and add them to the menu_text array
				// spacing them apart by 20 px on the Y-axis and then add them to the collection
				menu_text = new TextSprite(menu_options, menu_font, System.Drawing.Color.White,
											                new Vector(220, 320 + (i*20)));
				text_sprites.Add(menu_text);
			}

			title_text = new TextSprite("C# Pong", title_font, System.Drawing.Color.White, 																			  
																	new Vector(200,300));
			text_sprites.Add(title_text);
		}
		#endregion
	}
}

When I run the program in debug mode, I get an error at the line:
text_sprites.Add(menu_text);
with an error saying: An unhandled exception of type 'System.NullReferenceException' occurred in CSharpPong.exe Additional information: Object reference not set to an instance of an object. I don't see how menu_text can be null if I'm initializing it within the loop.

Share this post


Link to post
Share on other sites
Advertisement
There may be a bug in TextSprite. Are you trying to add menu_text to the collection before you set the text in menu_text? Try setting the text first then adding it. If that works, let me know and I will fix the problem. TextSprite unfortunately assumes that it has some text before you do anything with it.

Share this post


Link to post
Share on other sites
Well, last night I was messing with the code and came across some code that did work:


// TextSprite inits
for (int i=0; i<menu_options.Length; i++)
{
// Loop through the menu_options array and add them to the menu_text array
// spacing them apart by 20 px on the Y-axis and then add them to the collection
TextSprite menu_text = new TextSprite(menu_options, menu_font, System.Drawing.Color.White,
new Vector(250, 150 + (i*35)));
text_sprites.Add(menu_text);
}

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!