Code Help - Pong

Started by
8 comments, last by mistervirtue 11 years, 5 months ago
I think my errors have to do with with several of my SpriteManager (Which is a GameComponent) Fields

Vector2 position;
Vector2 speed;
Rectangle screenRectangle;
Vector2 direction;


all have the same error, "is never assigned to and will always have its default value". But I do think I am assigning them each in my classes.


//This is My Computer Class Constructor Which Inherits from the BasePaddle Class
public ComputerPaddle(Texture2D texture, Rectangle screenBounds, Vector2 direction, Vector2 position, SpriteManager spriteManager)
: base(texture, screenBounds, direction, position)
{
this.texture = texture;
this.screenBounds = screenBounds;
this.direction = direction;
this.position = position;
this.spriteManager = spriteManager;
}


//This is my Player Paddle Class Constructor Which Inherits from the BasePaddle Class
public PlayerPaddle(Texture2D texture, Rectangle screenBounds, Vector2 direction, Vector2 position)
: base(texture, screenBounds, direction, position)
{
this.texture = texture;
this.screenBounds = screenBounds;
this.direction = direction;
this.position = position;
}



So what I am i doing wrong? I don't know why my SpriteManager has null fields when I (I think) i am assigning them, I am sorry that I am asking so many questions, I don't have anyone other than you to ask.

So my question is why are my SpriteManager fields null? I don't understand what I am doing wrong/ I don't know what I didn't do.
Advertisement
First question. Why are the private variables for the cpupaddle and the playerpaddle different? Secondly, we'll have to see how you are calling the two constructors to know why they are showing up null.

Beginner in Game Development?  Read here. And read here.

 

First Answer. The Only diffrence is that CPUpaddle uses the spriteManager to find the ball position.


protected override void LoadContent()
{
spriteBatch = new SpriteBatch(Game.GraphicsDevice);

Texture2D playerPaddleTexture = Game.Content.Load<Texture2D>("JongPaddle");
playerPaddle = new PlayerPaddle(playerPaddleTexture, screenRectangle, direction, position);

Texture2D computerPaddleTexture = Game.Content.Load<Texture2D>("JongPaddle");
computerPaddle = new ComputerPaddle(computerPaddleTexture, screenRectangle, direction, position, this);

Texture2D ballTexture = Game.Content.Load<Texture2D>("ball");
ball = new Ball(ballTexture, screenRectangle);

StartGame();
base.LoadContent();
}


And that is how I call all of my classes.
Are the JongPaddle images actually being found? IE. playerPaddleTexture != null? Also, where are you assigning or initializing screenRectangle, direction, and position?

Beginner in Game Development?  Read here. And read here.

 

The JongPaddles are being found, but nothing else is. So I don't know what the issue there is. Also to answer your other question I think I am doing the assigning or intializing in the basePaddle Class.


namespace JONG_PongClone_
{
public abstract class BasePaddle
{
//Texture For the Block
protected Texture2D texture;
//position of the Block
protected Vector2 position;
//Directoin of the Block
protected Vector2 direction;
//Collision Rectangle for the block
protected Rectangle screenBounds;
All I see there are variable declarations. No initializations or assignments.

Show the code where you are actually setting the values for screenRectangle, position, and direction.

Beginner in Game Development?  Read here. And read here.

 

That is my problem right there. I just realized I never assigned any value to my variables. That is problem I think. Thanks for kind of walking me through this.
Feel free to hit the Up arrow on all my posts :).

That's how we show thanks around here now :)

Beginner in Game Development?  Read here. And read here.

 

We are not done quite yet. I still need to try it out.

All I see there are variable declarations. No initializations or assignments.

Show the code where you are actually setting the values for screenRectangle, position, and direction.


Okay I can't declare these values right. My screenRectangle is supposed to be my Window diminsions, my positions is supposed to keep track of the paddle location.

This topic is closed to new replies.

Advertisement