Sign in to follow this  
Tim Ogunmakin

[Solved] Loading enemies method

Recommended Posts

I'm trying to spawn a new enemy in my game, I've been following a book to do this :

 

public void LoadEnemies()                {                    int randY = random.Next(100,400);                    if (spawnzz >= 1)                    {                        spawnzz = 0;                        if (enemies.Count < 4)                        {                            enemies.Add(new EnemySpawn(Content.Load("Graphics/Sprites/enemy"), new Vector2(1100, randY)));                        }                    }                    for (int i = 0; i < enemies.Count; i++)                    {                        if (!enemies[i].isVisible)                        {                            enemies.RemoveAt(i);                            i--;                        }                    }                }


"enemies.Add(new EnemySpawn(Content.Load("Graphics/Sprites/enemy"), new Vector2(1100, randY)));"

The part there it says Content.Load is where it comes up in red and displays "The name content does not exist in current context"

Help!

Regards,
Tim

Share this post


Link to post
Share on other sites
It's hard to help you with the information you shared, what does the "The name content does not exist in current context" error message mean for example? Is it because the file wasn't found? I Assume that Graphics/Sprites/enemy is suppose to be a path to a file but it seems to be missing what format the file has.

edit:
Seems like I missunderstood the question! Edited by simpler

Share this post


Link to post
Share on other sites
Oh!  my bad,  that function either needs to sit in the Game1 class, (where you will find the Content object) or, you need to somehow expose the Content object.

 

I dont have a Game1 function, most of my code is in the GameplayScreen class, here is my heirarchy:

 

heirarchy.png

Share this post


Link to post
Share on other sites
I'm refering to the "Class" called game.cs.  It's in the picture as well.  Essentially, your code is asking for an object called "Content", Content exists in the Game.cs class, which inside is usually named Game1.  
 
This function for loading enemies needs to be inside the Game1 class, or the Content object needs to be exposed to the other class some how.  

Share this post


Link to post
Share on other sites
I'm refering to the "Class" called game.cs.  It's in the picture as well.  Essentially, your code is asking for an object called "Content", Content exists in the Game.cs class, which inside is usually named Game1.  
 
This function for loading enemies needs to be inside the Game1 class, or the Content object needs to be exposed to the other class some how.  

 

Ive now done this, I've moved the loading enemies into the Game class in the LoadContent() region however I'm getting the same thing :S

Also, what is the book you are using.  They are having you repetitively load an image during game play.  That's typically a bad idea, as it is faster to load it once in to a variable, and then reuse it as you need.

 

Im using "Learning XNA 4.0" by Aaron reed :)

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