EDIT: I've added bits of my GameplayScreen class so that you can see how my ball and bats are initialized.
This sounds like a very n00b question, but bear with me here:
I'm trying to access the position of my bat (paddle) in my pong game and use it in my ball class. I'm doing this because I want a particle effect to go off at the point of contact where the ball hits the bat.
Each time the ball hits the bat, I receive an error stating that I haven't created an instance of the bat. I understand that I have to (or can use a static class), but I'm not sure of how to do so in this example.
I've included both my Bat and Ball classes.
public class Ball
{
....
private bool hasHitBat;
private AIBat aiBat;
private Bat bat;
....
/// <summary>
/// Constructor for the ball
/// </summary>
public Ball(ContentManager contentManager, Vector2 ScreenSize)
{
moveSpeed = 15f;
speed = 0;
texture = contentManager.Load<Texture2D>(@"gfx/balls/redBall");
direction = 0;
size = new Rectangle(0, 0, texture.Width, texture.Height);
resetPos = new Vector2(ScreenSize.X / 2, ScreenSize.Y / 2);
position = resetPos;
rand = new Random();
isVisible = true;
hasHitBat = false;
// Everything to do with particles
List<Texture2D> textures = new List<Texture2D>();
textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/circle"));
textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/star"));
textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/diamond"));
particleEngine = new ParticleEngine(textures, new Vector2());
}
/// <summary>
/// Checks for the collision between the bat and the ball. Sends ball in the appropriate
/// direction
/// </summary>
public void BatHit(int block)
{
if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
{
hasHitBat = true;
particleEngine.EmitterLocation = new Vector2(aiBat.Position.X, aiBat.Position.Y);
switch (block)
{
case 1:
direction = MathHelper.ToRadians(200);
break;
case 2:
direction = MathHelper.ToRadians(195);
break;
................
}
}
else
{
hasHitBat = true;
particleEngine.EmitterLocation = new Vector2(bat.Position.X, bat.Position.Y);
switch (block)
{
case 1:
direction = MathHelper.ToRadians(310);
break;
case 2:
direction = MathHelper.ToRadians(345);
break;
...............
}
}
if (rand.Next(2) == 0)
{
direction += MathHelper.ToRadians(rand.Next(3));
}
else
{
direction -= MathHelper.ToRadians(rand.Next(3));
}
AudioManager.Instance.PlaySoundEffect("hit");
}
}
public class Bat
{
/// <summary>
/// Constructor for the bat
/// </summary>
public Bat(ContentManager contentManager, Vector2 screenSize, bool side)
{
moveSpeed = 7f;
turbo = 15f;
recharge = 100f;
points = 0;
interval = 5f;
leftBat = contentManager.Load<Texture2D>(@"gfx/bats/batGrey");
size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
// True means left bat, false means right bat.
if (side) Position = new Vector2(30, screenSize.Y / 2 - size.Height / 2);
else Position = new Vector2(screenSize.X - 30, screenSize.Y / 2 - size.Height / 2);
yHeight = (int)screenSize.Y;
}
/// <summary>
/// Sets thedefault starting position for the bats
/// </summary>
/// <param name="position"></param>
public void SetPosition(Vector2 position)
{
if (position.Y < 0)
{
position.Y = 0;
}
if (position.Y > yHeight - size.Height)
{
position.Y = yHeight - size.Height;
}
this.Position = position;
}
/// <summary>
/// Checks for the current position of the bat
/// </summary>
public Vector2 GetPosition()
{
return Position;
}
/// <summary>
/// Updates the position of the AI bat, in order to track the ball
/// </summary>
/// <param name="ball"></param>
public virtual void UpdatePosition(Ball ball)
{
size.X = (int)Position.X;
size.Y = (int)Position.Y;
}
/// <summary>
/// Draws the bats
/// </summary>
public virtual void Draw(SpriteBatch batch)
{
batch.Draw(leftBat, size, Color.White);
}
}
}
Class GameplayScreen
{
protected void Initialize()
{
........
ball = new Ball(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height));
SetUpSingle(); // Sets up a single player game
......
}
/// <summary>
/// Sets up a single player game, specifically the bats
/// </summary>
private void SetUpSingle()
{
rightBat = new AIBat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), false);
leftBat = new Bat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), true);
}