Sign in to follow this  
44cll

Texture2D question

Recommended Posts

44cll    134
So basically I'm trying to add textures defined in my Game.cs class to a list called scrollingTextures defined in a class called ScrollingBackground.

I know this code doesn't work but it's basically what I'm trying to do.

[b]In ScrollingBackground.cs[/b]
[CODE]
public void AddBackground(Texture2D texture)
{
Texture2D textureDefinedInGame.cs = new Texture2D();
scrollingTextures.Add(textureDefinedInGame.cs);
}
[/CODE]

[b]In Game.cs[/b]
[CODE]
scrollingBackground.AddBackground(background);
[/CODE]

What would I need to do to go about this? Edited by Gihzah

Share this post


Link to post
Share on other sites
FalconDragoon    485
It really depends on what it is you're [i]REALLY [/i]trying to do. There are a couple of ways to load Textures, but the most common is to use the ContentManager
[source lang="csharp"]Content.Load<Texture2d>("textureName");[/source]
If the ScrollingBackground object has a reference to the Game object, then you should be able to pass the name of the texture:
[source lang="csharp"] public void AddBackground(string textureName)
{
scrollingTextures.Add(Game.Content.Load<Texture2d>(textureName);
}[/source]
Alternatively, in the Game class you load up a texture named background, and pass it just as you have it coded to AddBackground.
[source lang="csharp"]//In a method somwhere in Game.cs
Texture2d background = Content.Load<Texture2d>("SomeTexName");
scrollingBackground.AddBackground(background);

//AddBackground method in ScrollingBackground
public void AddBackground(Texture2D texture)
{
scrollingTextures.Add(texture);
}[/source]

Hope that helps some.

Share this post


Link to post
Share on other sites
44cll    134
[quote name='Jutaris' timestamp='1340032619' post='4950281']
//AddBackground method in ScrollingBackground
public void AddBackground(Texture2D texture)
{
scrollingTextures.Add(texture);
}[/source]
[/quote]

This was what I was looking for, didn't think it would be so simple. Thanks for the help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this