Thanks, that’s what I needed.
A number of your methods are declared within your Update
method. This is valid if you’re using C# 7, which you are if you are using Visual Studio 2017, where methods declared within methods are known as “local functions” or “local methods”, which are only accessible from within the method in which they are declared. However, local functions cannot be declared public, so that is an error. See this documentation for information about local functions.
However, it’s more likely that you weren’t intending to declare them as local methods. To solve the problem, you’ll need to move the functions outside of the Update
method so that they are in the same scope as the Update
method, and then call them, as opposed to declaring them, from within the Update
method.
The Shoot
method also has some problems, with an incomplete initialization of a new Vector2
, missing the second argument and closing parentheses and a semicolon.
Here’s the Game1
class with the errors I mentioned fixed:
namespace Game1
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<Enemies> enemies = new List<Enemies>();
Random random = new Random();
// Screen Parameters
int screenWidth;
int screenHeight;
//PLAYER PLANE
private Texture2D _texture;
private Vector2 _position;
//BACKGROUNDS
Scrolling scrolling1;
Scrolling scrolling2;
//BULLETS
List<Bullets> bullets = new List<Bullets>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//SCREEN
{
screenWidth = GraphicsDevice.Viewport.Width;
screenHeight = GraphicsDevice.Viewport.Height;
}
//PLAYER
{
_texture = Content.Load<Texture2D>("Sprites/PlayPlane1");
_position = new Vector2(350, 370);
}
//BACKGROUNDS
{
scrolling1 = new Scrolling(Content.Load<Texture2D>("Backgrounds/sky1"), new Rectangle(0, 0, 800, 900));
scrolling2 = new Scrolling(Content.Load<Texture2D>("Backgrounds/sky2"), new Rectangle(0, 700, 800, 900));
}
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
float spawn = 0;
private object myRectangle;
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
//KEYBOARD COMMANDS FOR PLAYER
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
_position.X -= 3;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
_position.X += 3;
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
_position.Y -= 3;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
_position.Y += 3;
}
//SCROLLING BACKGROUNDS
if (scrolling1.rectangle.Y + scrolling1.texture.Height <= 10)
scrolling1.rectangle.Y = scrolling2.rectangle.Y + scrolling2.texture.Height;
if (scrolling2.rectangle.Y + scrolling2.texture.Height <= 10)
scrolling2.rectangle.Y = scrolling1.rectangle.Y + scrolling1.texture.Height;
scrolling1.Update();
scrolling2.Update();
UpdateBullets();
Shoot();
LoadEnemies();
base.Update(gameTime);
}
//BULLETS
public void UpdateBullets()
{
foreach (Bullets bullet in bullets)
{
bullet.position += bullet.velocity;
if (Vector2.Distance(bullet.position, spritePosition) > 500)
bullet.isVisible = false;
}
for (int i = 0; i < bullets.Count; i++)
{
if (!bullets[i].isVisible)
{
bullets.RemoveAt(i);
i--;
}
}
}
public void Shoot()
{
Bullets newBullet = new Bullets(Content.Load<Texture2D>("PlayerBullet/PlaneShoot"));
newBullet = new Vector2(); //This needs to be given a proper value
}
//ENEMIES
public void LoadEnemies()
{
int randY = random.Next(100, 400);
if (spawn >= 1)
{
spawn = 0;
if (enemies.Count < 4)
enemies.Add(new Enemies(Content.Load<Texture2D>("Enemy1"), new Vector2(1100, randY)));
}
{
}
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
scrolling1.Draw(spriteBatch);
scrolling2.Draw(spriteBatch);
spriteBatch.Draw(_texture, _position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}