[Solved] Loading enemies method

Started by
6 comments, last by Tim Ogunmakin 11 years, 3 months ago

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

Advertisement

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.

Moltar - "Do you even know how to use that?"

Space Ghost - “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer."

Dan - "Best Description of AI ever."

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!
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

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.

Moltar - "Do you even know how to use that?"

Space Ghost - “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer."

Dan - "Best Description of AI ever."

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.

Moltar - "Do you even know how to use that?"

Space Ghost - “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer."

Dan - "Best Description of AI ever."

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 :)

Fixed it all by re-doing the whole spawning code myself. Note to self: try something first before looking at books to do it

thanks for the help!

This topic is closed to new replies.

Advertisement