Don't worry. Take as much time as you like.I know the current versions, but I feel comfortable working with .Net/xna 2.0 and I do it for increased compatibility. Enjoy your holidays.
Texture not displaying in my XNA game app.
Collapse
X
-
Ok, I'm back...
I'm unable to run the compiled version, it crashes for me. I'm going to guess it's because I don't have the runtimes installed for 2.0; however, I think it's safe to say that since your test project works for me and for you, you've done something funny in your main project.
As we suspected, it's just fine to pass the ContentManager along to another class and have it load something, so we've got something else going on. My usual process here is to start commenting out code in the main project to direct the flow and boil it down to something simpler. See if you can isolate the program flow that generates this problem and identify any issues.
Check to make sure the texture is being loaded into your Content and ensure the name is correct. You said it will work if you load it from the main project and the issue only happens when you load from the Test class?Comment
-
Let me make a diagram to illustrate how so far the problem has occurred:
main proj = Main
test proj = Test
Main
| ************ |
Game1 ****** hero
| ************* |
Draw()<--------- Draw()= fail
///////////////////////////////////
Main
(all code in the same beginning class)
|
Game1
|
Draw()= works
////////////////////////////////////
Test
| ************ |
Game1 ******* hero
| ************ |
Draw()<---------Draw()= works.
///////////////////////////////////
I'm going to try commenting the code out.Also, I'm not sure if I gave you an outdated piece of code, because I had to comment out a part that required a test video to load around the time when I uploaded the code sample. I uploaded an updated version that should run an show you what is displayed so you have an idea.Attached FilesComment
-
I still can't run your code, and I don't really want to rebuild your project to update it for the latest XNA.You should really do this as most people will will be running .NET 4.0 and XNA 4.0 anyway. It's generally best to keep up with the latest versions where you can.
Anyway, your version of XNA isn't really the problem here.
Though honestly, I'm having a hard time understanding what is. I'm also having trouble keeping track of what the problem is. When we boiled it down to the test scenario, everything worked fine so it's definitely something within your project's code. What that is, I think you're going to need to find on your own. We've shown that the general idea of what you're trying to do is sound, so it's just something you've done in there.
What confuses me is what's happening. Earlier you said the image just didn't draw, then later you said you were getting an error on the texture load. Are you changing what your Texture2D object refers to at any point? I'm reasonably sure that the Draw method will throw an exception if it's null, so if you're not getting anything out of your Draw it must be sending an empty image.Comment
-
I'm thinking along those lines too, for I tried your suggestion of commenting out some of the code and the output is the same. So I don't think the other draw calls are interfering. Is xna 4.0 radically different from 2.0? I'm thinking of updating to that and checking out how it behaves. Maybe by moving the code to a newer version of the framework and modifying the code to fit it, I will unknowingly fix whatever was giving me trouble. Thanks for everything.Comment
-
From the standpoint of a simple 2D game I don't think it's that different. There might be a few changes in the Draw overloads, but nothing that's difficult to deal with.
I'm out of ideas for how to fix your problem, but maybe a general suggestion?
I notice you're letting your hero class load its own textures in itself by giving it the content manager... this is a different approach than I usually take. I'm not saying my method is better, but I'll describe it so you can consider it.
Basically, I create a sprite manager class and load all my game's sprites into that. It's usually stored in a hash table where the key is a string name for what I'm looking for and the value is the Texture2D object. Then, to use it, I might do one of two things...
1) If I'm concerned about performance, I'll pass the sprite table to the object in the constructor and copy references to local objects.
2) If speed isn't an issue (and keep in mind, for this kind of lookup in a 2D game it usually isn't), I'll either give the object a reference to the sprite table (again, via the constructor), or pass the reference to the object's draw method.
That way I load all my sprites in one place where I can find them if i need to do any modifications.Comment
-
I saw that but thought it was a typo and didn't give it any thought. I didn't think to look in the code and see if it was there.
I don't know if you can do that either, I thought it would generate a compile error which is why it went out of my head immediately afterward... I'll be amused if that's the issue.
Nice spot, Rabbit!Comment
-
I thought so too. I don't think the code would compile as is. So I don't know where the disconnect is between what he's posted and how he was able to get it to compile. Perhaps C# handles white space differently and you're allowed to do that, or the code he attached is different from the code he's compiling, or it's some setting in the compiler to ignore errors.Comment
-
That's in my code alright. But when using the code browser that appears when accessing functions and variables of classes, the code ends up with the extra space. I sometimes delete the extra spaces, but if I'm focused on figuring out why something is not working as intended or how to make a function more general for the program I totally ignore them. Furthermore, I don't get compiler errors and I thought they are normal, because in C++ the compiler would ignore most white spaces.Comment
Comment