So I've added my main character and I've gotten the background set up, so now I want to add some code that will randomly generate a background.
For some reason I can't get the search function on these forums to work, so sorry if this has been answered before.
I don't even know where to begin because it just seems so overwhelming to me, so any point in any direction will be helpful.
Thanks guys!
P.S. For some reason the replies to my previous post aren't saving. I have no idea why, but I've been trying to thank people for helping me, but it wont stick.
Seems to me like broken forums.
Randomly Generated Background
Page 1 of 13 Replies - 4877 Views - Last Post: 18 March 2013 - 03:18 PM
Replies To: Randomly Generated Background
#2
Re: Randomly Generated Background
Posted 17 March 2013 - 02:59 PM
Assuming your background is just a Texture2D you could make a List<Texture2D> to hold all the possible background images you would like. You then use the Random class to get a random int that points to an index in the list.
//Fields
List<Texture2D> backgroundTextures;
Random random;
//Load
public void LoadContent(ContentManager content)
{
//Create the Random object
random = new Random();
// Assumes all your background textures are called "backgroundAsset" + a numeric value
// in order to ease loading. EX: backgroundAsset6
string bgAsset = "backgroundAsset";
// The amount of textures you will load.
int maxTextures = 10;
// Initialize the List object
backgroundTextures = new List<Texture2D>(maxTextures);
//This loop will allow us to load a texture into each element of the list
for (int i = 0; i < maxTextures; i++)
{
// Load the texture to the list.
backgroundTextures[i] = content.Load<Texture2D>(bgAsset + i.ToString());
}
}
// Method that will return a random texture from the list
public Texture2D GetRandomBackground()
{
// Call random.Next which will return a random int value between
// 0 and 9 in this case (Remember list indexing is Zero-Based)since
// we have 10 textures
int index = random.Next(0, backgroundTextures.Count - 1);
// Return the Texture2D that the index points to
return backgroundTextures[index];
}
This post has been edited by LiberLogic969: 17 March 2013 - 03:08 PM
#3
Re: Randomly Generated Background
Posted 18 March 2013 - 02:37 PM
Awesome, thanks!
I was wondering today if there's a way that I could separate the background into sectors, say top left, top mid, top right etc. and then use your idea.
Randomly pick through a list of sectors which will compile a completely random map each load.
Sounds like a lot of work o.O
I was wondering today if there's a way that I could separate the background into sectors, say top left, top mid, top right etc. and then use your idea.
Randomly pick through a list of sectors which will compile a completely random map each load.
Sounds like a lot of work o.O
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote



|