Error with constructor when inheriting using a base class - c#

Hey im new to c# and XNA. My goal is to inherit from a Sprite class and make classes such as pongSprite, paddleSprite etc... Im getting an error on my constructor. I have extended the sprite class and i have put the variables and objects from the Sprite class into the :base()
Here is my code:
**
Sprite.cs
**
namespace SimplePong
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class Sprite : DrawableGameComponent
{
protected string id;
protected Texture2D texture;
//bounding box
//protected Rectangle sourceRectangle;
protected Rectangle destinationRectangle;
protected Color color;
protected Main game;
private Sprite pongBall;
public Sprite(Main game, string id, Texture2D texture,
Rectangle destinationRectangle, Color color)
: base(game)
{
this.game = game;
this.id = id;
this.texture = texture;
this.destinationRectangle = destinationRectangle;
this.color = color;
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
game.spriteBatch.Begin();
game.spriteBatch.Draw(texture, destinationRectangle, color);
game.spriteBatch.End();
base.Draw(gameTime);
}
}
}
**
ballSprite.cs
**
using Microsoft.Xna.Framework;
namespace SimplePong
{
public class BallSprite : Sprite
{
// public Main game;
public Sprite pongBall;
public BallSprite()
: base(Main game, string id, Texture2D texture,
Rectangle destinationRectangle, Color color)
{
}
public override void Initialize()
{
base.Initialize();
}
public override void Update(GameTime gameTime)
{
destinationRectangle.X += 2;
destinationRectangle.Y += 2;
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
}
}
}

The syntax of the constructor your inherited class is wrong. I suspect you want:
public BallSprite(Main game, string id, Texture2D texture,
Rectangle destinationRectangle, Color color)
: base(game, id, texture, destinationRectangle, color)
{
}
If you want a parameterless constructor then you're going to have to come up with the values for the parameters yourself (or not call the base constructor).

Related

XNA Sprite Class using Game Component and IDrawable

I am working on my Sprite and Actor class for a game and I have 2 different types of enemies, a pumpkin and a bat. What I wanted to do was to make them both updatable so the pumpkin will fall straight down from its spawn and the bat will follow the player until the player's death or its destruction. But right now I am trying to create their Actor class and I am kind of new to XNA and Stack-Overflow.
Sprite Class:
public class Sprite : Microsoft.Xna.Framework.GameComponent, IDrawable
{
public Texture2D texture;
public Vector2 position;
public Vector2 moveSpeed;
public Vector2 scale;
public Color tint;
public bool active;
public Rectangle rect
{
get { return new Rectangle((int)position.X, (int)position.Y, Width, Height); }
}
public int Height
{
get { return (int)(texture.Height * scale.Y); }
}
public int Width
{
get { return (int)(texture.Width * scale.X);}
}
public Sprite(Game game, String tex)
: base(game)
{
// TODO: Construct any child components here
texture = game.Content.Load<Texture2D>(tex);
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
position = Vector2.Zero;
tint = Color.White;
active = true;
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
base.Update(gameTime);
}
public void Draw(GameTime gameTime)
{
((Game1)Game).spriteBatch.Begin();
((Game1)Game).spriteBatch.Draw(texture,rect,tint);
((Game1)Game).spriteBatch.End();
}
public int DrawOrder
{
get { return 1; }
}
public event EventHandler<EventArgs> DrawOrderChanged;
public bool Visible
{
get { return active; }
}
public event EventHandler<EventArgs> VisibleChanged;
}
Actor Class:
public class Actor : Microsoft.Xna.Framework.GameComponent
{
public Sprite sprite; //Call Instance of Sprite class
public String tag; //String for the texture
public float health; //Health of the sprite
public int value; //The enemies will need a score
public Actor(Game game, String tex)
: base(game)
{
// TODO: Construct any child components here
sprite = new Sprite(game, tex);
game.Components.Add(this);
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
health = 100.0f; //Initialize the health of Enemy
value = 10; //Initialize the value of enemy destruction
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
if(sprite.rect.Intersects(sprite.rect))
{
health -= 100;
sprite.active = false;
}
base.Update(gameTime);
}
}

XNA An object reference is required for the non-static field, method, or property error

I've done some research around the internet including this site but I am still confused about what to do with this error. I have 2 classes I'm using MainSkeleton which is the Game1 class and it has the move method while I also made a class InputSkeleton that handles user inputs. When I try calling the move method I'm getting the error even though I called the class.
MainSkeleton
/// <summary>
/// This is the main type for your game
/// </summary>
public class MainSkeleton: Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
InputSkeleton input;
SpriteBatch spriteBatch;
Color backColor = Color.CornflowerBlue;
public MainSkeleton()
{
input = new InputSkeleton(1);
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>
// This is a texture we can render.
Texture2D myTexture;
// Set the coordinates to draw the sprite at.
Vector2 spritePosition = Vector2.Zero;
// Store some information about the sprite's motion.
Vector2 spriteSpeed = new Vector2(50.0f, 50.0f);
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load<Texture2D>("Person");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
input.Update();
base.Update(gameTime);
}
/// <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);
// Draw the sprite.
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(myTexture, spritePosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
public void Move(String direction)
{
int MaxX = graphics.GraphicsDevice.Viewport.Width - myTexture.Width;
int MinX = 0;
int MaxY = graphics.GraphicsDevice.Viewport.Height - myTexture.Height;
int MinY = 0;
if (direction.Equals("up"))
{
if (spritePosition.Y > MinY)
{
spritePosition.Y = spritePosition.Y - 1;
}
else
{
spritePosition.Y = MinY;
}
}
if (direction.Equals("down"))
{
if (spritePosition.Y < MaxY)
{
spritePosition.Y = spritePosition.Y + 1;
}
else
{
spritePosition.Y = MaxY;
}
}
if (direction.Equals("left"))
{
if (spritePosition.X > MinX)
{
spritePosition.X = spritePosition.X - 1;
}
else
{
spritePosition.X = MinX;
}
}
if (direction.Equals("right"))
{
if (spritePosition.X < MaxX)
{
spritePosition.X = spritePosition.X + 1;
}
else
{
spritePosition.X = MinX;
}
}
}
}
InputSkeleton
public class InputSkeleton
{
public KeyboardState newKState;
public KeyboardState oldKState;
public MouseState newMState;
public MouseState oldMState;
public Boolean menuState;
public Boolean gameRun;
public int player;
public InputSkeleton(int p)
{
player = p;
}
public void Update()
{
if (menuState == true && gameRun == false)
{
MenuInput();
}
else if (menuState == false && gameRun == true)
{
PlayerInput();
}
}
public void MenuInput()
{
}
public void PlayerInput()
{
newKState = Keyboard.GetState();
if (newKState.IsKeyDown(Keys.Up))
{
MainSkeleton.Move("up");
}
if (newKState.IsKeyDown(Keys.Down))
{
MainSkeleton.Move("down");
}
if (newKState.IsKeyDown(Keys.Left))
{
MainSkeleton.Move("left");
}
if (newKState.IsKeyDown(Keys.Right))
{
MainSkeleton.Move("right");
}
oldKState = newKState;
}
}
A static method can be called on a class, while a non static method needs to be called on an object. You're trying to call a non static method on a class;
MainSkeleton.Move("up");
Either you need to make the Move() method a static method (which in this case is not an option since you want to call the method on the same object every time), or you need to somehow get to the original MainSkeleton object and call the method on that.
A simple way to get to the original object is to pass it in to the InputObject constructor which you call from MainSkeleton;
input = new InputSkeleton(1, this); // Pass in myself
...and take the object in the constructor;
MainSkeleton mainSkeleton;
public InputSkeleton(int p, MainSkeleton m)
{
player = p;
mainSkeleton = m;
}
...and finally call move() on the object instead;
mainSkeleton.Move("up");
Problem is you are calling the class without creating an instance.
You need to first create an instance to call a non-static method in a class.
Example;
private MainSkeleton _MainSkeleton = new MainSkeleton();
private void Update()
{
// The below is calling a method and telling what object is calling that method.
_MainSkeleton.Move("right");
// The below calls a method but does not link to any object. The below is only able to call non-static methods.
MainSkeleton.Move("right");
}
You should look up more about static vs non-static methods to see what the difference is.

C# Monogame snake game

Well first i must say i am a complete noob, I just started with 2d graphics and i experiment on a snake game, in the Update method i call snakeUpdate which updates the position according the keystate the thing is it never updates the position, someone explain a bit please.
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Utilities;
namespace SnakeGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Texture2D background;
private Texture2D shuttle;
private Texture2D earth;
KeyboardState newState = new KeyboardState();
KeyboardState oldState = new KeyboardState();
private float angle = 0;
public Vector2 position= new Vector2(480,240);
public Vector2 velocity;
public Game1()
: base()
{
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);
// TODO: use this.Content to load your game content here
background = Content.Load<Texture2D>("stars.jpg"); // change these names to the names of your images
shuttle = Content.Load<Texture2D>("shuttle.png"); // if you are using your own images.
earth = Content.Load<Texture2D>("earth.png");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
listener();
snakeUp();
// TODO: Add your update logic here
//angle += 0.01f;
base.Update(gameTime);
}
/// <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();
spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
spriteBatch.Draw(earth, new Vector2(400, 240), Color.WhiteSmoke);
Vector2 origin = new Vector2(0, 0);
spriteBatch.Draw(shuttle, position, new Rectangle(0,0, shuttle.Width, shuttle.Height), Color.White, angle, origin, 0.5f, SpriteEffects.None, 1);
spriteBatch.End();
base.Draw(gameTime);
}
public void listener()
{
if (newState.IsKeyDown(Keys.Right) && (oldState.IsKeyUp(Keys.Right)))
velocity = new Vector2(3, 0);
if (newState.IsKeyDown(Keys.Left) && (oldState.IsKeyUp(Keys.Left)))
velocity = new Vector2(-3, 0);
if (newState.IsKeyDown(Keys.Down) && (oldState.IsKeyUp(Keys.Down)))
velocity = new Vector2(0, 3);
if (newState.IsKeyDown(Keys.Up) && (oldState.IsKeyUp(Keys.Up)))
velocity = new Vector2(0, -3);
}
public void snakeUp() {
position += velocity;
}
}
}
You're not assigning oldState and newState correctly: you're initializing those variables (incorrectly) only one time when Game1 is instantiated. You'll need something like this:
public class Game1 : Game {
KeyboardState oldState = Keyboard.GetState();
KeyboardState newState = Keyboard.GetState();
// ...
protected override void Update(GameTime gameTime) {
oldState = newState;
newState = Keyboard.GetState();
// ...
}
}
You are not updating your keyboard state. Write something like this inside your Update method (before listener):
oldState = newState;
newState = Keyboard.GetState();

Sound Buttons don't play sound

I'm making a program where one clicks buttons to create sound, like those instant button apps. But when I click the button, it won't play any sound. I don't know why either.
All my "Build" debugs are succeeding too.
These are my classes:
Game1.cs:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D Background;
Button button;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.Window.Title = "Bengt Slår På Knappar";
graphics.PreferredBackBufferHeight = 720;
graphics.PreferredBackBufferWidth = 1280;
IsMouseVisible = true;
}
/// <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()
{
Background = Content.Load<Texture2D>("bengtbakgrund");
Texture2D Btexture = Content.Load<Texture2D>("Button");
SoundEffect dundundun = Content.Load<SoundEffect>("DUN DUN DUN");
button = new Button(dundundun, Btexture);
spriteBatch = new SpriteBatch(GraphicsDevice);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Rectangle mouseRect = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 10, 10);
MouseState mouseState = Mouse.GetState();
if (mouseRect.Intersects(button.rect) && mouseState.LeftButton == ButtonState.Pressed)
{
button.MySound.Play();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(Background, new Vector2(0,0), Color.White);
button.Draw(spriteBatch, new Vector2(300, 300));
spriteBatch.End();
base.Draw(gameTime);
}
}
Button.cs:
public class Button
{
Texture2D Texture { get; set; }
public SoundEffect MySound { get; set; }
public Rectangle rect;
public Button(SoundEffect mySound, Texture2D texture)
{
MySound = mySound;
Texture = texture;
}
public void LoadContent(ContentManager Content)
{
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
Rectangle rect = new Rectangle((int)0, (int)0, Texture.Width, Texture.Height);
spriteBatch.Draw(Texture, location, Color.White);
}
}
What I'm trying to do is have one base button class, so if you want to add a new button with a sound, you just write: button = new Button(//sound effect, //texture);
But I can't get it to work. Any help would be appreciated.
The way you are handling your buttons position is incorrect, and I am not sure how it compiles. In your draw method you define a local variable rect that already exists. And even if it did, you are not taking into account the actual position of the button.
I think you could also move your button update logic into the class, so I will propose what I think you need to do.
public class Button
{
//Properties
private Texture2D Texture { get; set; }
private SoundEffect Sound { get; set; }
private Rectangle Rectangle { get; set; }
public Button(SoundEffect sound, Texture2D texture, Rectangle position)
{
Sound = sound;
Texture = texture;
Rectangle = position;
}
public void Update(GameTime gameTime, MouseState mouseState)
{
//If mouse is down and the rectangle contains the mouse point (Better for points than Intersect())
if (mouseState.LeftButton == ButtonState.Pressed && Rectangle.Contains(new Point(mouseState.X,mouseState.Y))
{
Sound.Play()
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, rectangle, Color.White);
}
}
You can then just do button.Update(Mouse.GetState()) (Or use another MouseState if you have one already)
It appears there was some confusion with the drawing position, vs the actual position of the button (in the draw method)
You need to make sure the button is aware of its position on the screen, as well as it's width and height.
button = new Button(dundundun, Btexture, new Rectangle(300, 300, Btexture.Width, Btexture.Height);
That will make the button at 300,500 and be the default size for the texture, now your sound button is done done done :)
I would also recommend learning and using event handlers, so you can add events for the button such as button.Click += //Do something easily

XNA - The name 'ball' does not exist in the current context

I'm working on a pong game since I'm pretty new to XNA, and I've ran into a problem...
i have 3 classes, "Game1.cs", "Ball.cs", and "GreenPaddle.cs".
The GreenPaddle.cs contains Rectangle gpRect, Texture2D gPtexture, Vector2 position.
I have movement and such, and in the ball class I have an intersect boolean.
But when i try to initialize it in game1.cs i get errors. Here are the classes:
GreenPaddle.cs:
public class GreenPaddle
{
Texture2D gPtexture;
public Vector2 position;
public Rectangle gpRect;
public Vector2 origin;
public int speed = 2;
public GreenPaddle()
{
}
public void LoadContent(ContentManager Content)
{
gPtexture = Content.Load<Texture2D>("greenpaddle");
position = new Vector2(20, 200);
gpRect = new Rectangle((int)position.X, (int)position.Y,
gPtexture.Width, gPtexture.Height);
}
public void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
//Movement
PaddleMovement();
//Border Collision
isCollidingWithBorders();
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(gPtexture, position, gpRect, Color.LawnGreen);
}
public Boolean isCollidingWithBorders()
{
if (position.Y < 83 && gpRect.Y < 83)
{
position.Y = 83;
gpRect.Y = 83;
return true;
}
if (position.Y > 400 && gpRect.Y > 400)
{
position.Y = 400;
gpRect.Y = 400;
return true;
}
else { return false; }
}
public void PaddleMovement()
{
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.W))
{
position.Y -= speed;
gpRect.Y -= speed;
}
if (keyState.IsKeyDown(Keys.S))
{
position.Y += speed;
gpRect.Y += speed;
}
}
}
Ball.cs:
public class Ball
{
GreenPaddle gPaddle;
Texture2D ballTexture;
Vector2 ballPosition;
Rectangle ballRect;
int speed = 2;
bool movingUp, movingLeft;
public Ball(GreenPaddle paddle)
{
this.gPaddle = paddle;
movingLeft = true;
movingUp = true;
}
public void LoadContent(ContentManager Content)
{
ballTexture = Content.Load<Texture2D>("ball");
ballPosition = new Vector2(380, 225);
ballRect = new Rectangle((int)ballPosition.X, (int)ballPosition.Y,
ballTexture.Width, ballTexture.Height);
}
public void Update(GameTime gameTime)
{
BallMovement();
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(ballTexture, ballPosition, ballRect, Color.White);
}
public void BallMovement()
{
if (movingUp) { ballPosition.Y -= speed; }
if (!movingUp) { ballPosition.Y += speed; }
if (movingLeft) { ballPosition.X -= speed; }
if (!movingLeft) { ballPosition.X += speed; }
if (ballRect.Intersects(gPaddle.gpRect))
movingLeft = !movingLeft;
movingUp = !movingUp;
if (ballPosition.Y < 85)
{
movingUp = false;
}
}
}
Game1.cs:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GreenPaddle gPaddle = new GreenPaddle();
Texture2D BackGround;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 500;
}
protected override void Initialize()
{
Ball ball = new Ball(gPaddle);
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);
BackGround = Content.Load<Texture2D>("pongBG");
gPaddle.LoadContent(Content);
//ball.LoadContent(Content);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
gPaddle.Update(gameTime);
ball.Update(gameTime);//Error Line
base.Update(gameTime);
}
/// <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);
spriteBatch.Begin();
spriteBatch.Draw(BackGround, new Vector2(0f, 0f), Color.White);
gPaddle.Draw(spriteBatch);
ball.Draw(spriteBatch);//Error Line
spriteBatch.End();
base.Draw(gameTime);
}
}
Thats it, I don't know what to do with the initialize part :/
Look carefully how you initialize your ball, you declare it in the scope of the method (Between the braces). This means you cannot access it anywhere else, which you are trying to do in the Update and Draw methods.
protected override void Initialize()
{
Ball ball = new Ball(gPaddle);
base.Initialize();
}
Additionally, as soon as the function ends your Ball object is deleted.
This can be fixed by putting Ball ball into your class scope so that it is available to all members of the class, like so:
Ball ball; //In the class scope
protected override void Initialize()
{
ball = new Ball(gPaddle);
base.Initialize();
}
If your not familiar with scopes, check out this article (Nothing really good on MSDN)

Categories