开发者

How do I draw two objects from a Single Class in C#/Csharp and XNA?

开发者 https://www.devze.com 2023-03-01 09:47 出处:网络
Here\'s my issue. I have a paddle class and a ball class. Both have update, initialize, and draw methods. The issue is with my paddle class. I want to draw two paddles on the screen (each a different

Here's my issue. I have a paddle class and a ball class. Both have update, initialize, and draw methods. The issue is with my paddle class. I want to draw two paddles on the screen (each a different color), 开发者_C百科but the code I currently have doesn't work correctly. The result is 4 paddles instead of two (basically two paddles, with two more underneath).

Here's the code - http://paste2.org/p/1390842

And here's a screenshot of the problem - http://img651.imageshack.us/img651/9092/pongshot.png

I'm unsure of what I'm doing wrong. Perhaps it's a case of not being able to draw two different paddles? Should I make a second paddle class instead?

And here's my Game1.cs file - http://paste2.org/p/1390854

And my Ball class - http://paste2.org/p/1390856


Inside your Intialize/Draw methods you are drawing two Paddles for one object. Change these to this:

       public void Initialize(Texture2D texture, Vector2 position)
       {
            pongPaddle1 = texture;

            //Set Paddle position
            paddle1Position = position;
       }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(pongPaddle1, paddle1Position, null, Color.DarkSlateBlue, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
        }

Basically get rid of Paddle2 from the class. Since you are creating two instances of Paddle in Game1.cs you don't need to have two draws inside paddle. That kind of defeats the purpose of data objects. Each instance that is inside your Game1.cs will call the Draw() method and they will draw themselves.


A single paddle class to represent BOTH paddles probably isn't the best design as it breaks the idea of seperation of concerns. It is concerned about TWO things instead of one. Simply having 2 instances of a single paddle class will make your life a whole lot easier.

If you decide not to go that route, your problem is in the Game1 file. You have 2 instances of a class that deals with both paddles. See initiliazation(). You should only instantiate ONE if you're going to do it this way.

In the Draw() method, you are also drawing twice. A Paddle class represents 2 paddles, not one. So you should only have one instance of paddle again if you are not going to refactor the paddle class.


There are a few things happening here that are causing your problem:

  1. Your paddle class actually appears to handle to separate paddles. You're declaring two instances of this class so, essentially, you've got four paddles
  2. When you instantiate a new instance of paddle, you set pongPaddle1 and pongPaddle2 to the same coordinates. However, you only update on of these sets of coordinates for each paddle object. paddle1 only updates pongPaddle1 and likewise for paddle2. pongPaddle2 and pongPaddle1 are never changed from their initial positions for each object respectively.
  3. When you go to draw, it draws what's at pongPaddle1 and pongPaddle2 for each object.

My suggestion would be to either change your paddle class to have a singular position property (instead of pongPaddle1 and pongPaddle2) and keep one separate object for each paddle. Or, you can have the paddle class handle both paddles but only one instantiated object in the main code. So, instead of paddle1 and paddle2 you could have paddles and then update pongPaddle1 and pongPaddle2 for each paddle respectively.

Also, XNA doesn't care about what your object situation for drawing is. If you call the Draw method, it's just copying that image data to the screen buffer where you tell it to. You can call this as many times as you want with different parameters from the same class and it'll show each and every one.


Why not only have one paddle drawn for each instance of the paddle class and just have two instances of the class?

class paddle
{
    public Vector2 paddlePosition;
    public Texture2D pongPaddle; 

    // Getting Paddle Height and Width

    public int Width
    {
        get { return pongPaddle.Width; }
    }


    public int Height
    {
        get { return pongPaddle.Height; }
    }


    public paddle(Texture2D texture, Vector2 position)
    {
        pongPaddle = texture;

        //Set Paddle position
        paddlePosition = position;
    }

    public void Update()
    {
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(pongPaddle, paddlePosition, null, Color.DarkSlateBlue, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
    }
}

Initialize is like so

Paddle p1 = new Paddle(texture1, location1);
Paddle p2 = new Paddle(texture2, location2);

And then call draw

p1.Draw(spriteBatch);
p2.Draw(spriteBatch);

You don't seem to have a very strong understanding of OO principles. If you wan't to work with C# and XNA I would suggest you practice OO some more.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号