0
\$\begingroup\$

im trying to create simple pong game, and learning some XNA. im creating for each paddle 3 rectangles, who represnt the collision when the ball hit them, so i can dicide where to throw the ball back.

here is the function that i create the rectangles:

List<Rectangle> collisonRectanglesList = new List<Rectangle>(3);
      private void creatCollisonRectangles()
        {
 smallPaddleWidth = collisionRect.Width;
             smallPaddleHeight = collisionRect.Height / 3;
            for (int i = 0; i < collisonRectanglesList.Count; i++)
            {
                if (collisonRectanglesList[i] != null)
                    collisonRectanglesList.RemoveAt(i);
            }    
             collisonRectanglesList.Add(new Rectangle((int)position.X + smallPaddleWidth, (int)position.Y + smallPaddleHeight * 2, smallPaddleWidth, smallPaddleHeight));
             collisonRectanglesList.Add(new Rectangle((int)position.X + smallPaddleWidth, (int)position.Y + smallPaddleHeight, smallPaddleWidth, smallPaddleHeight));
             collisonRectanglesList.Add(new Rectangle((int)position.X + smallPaddleWidth, (int)position.Y , smallPaddleWidth, smallPaddleHeight));
            
        }

now i want this to follow the paddle it self so i put this function in the update method and made a simple texture so i can draw them and see where they are on the screen.

my problem is that when i call the creatCollisonRectangles() 1 of the rectangles paint it self in the middle and not in the place it should be.

any idea why?

this is when im not calling it in the Update method: this is when im not calling the function in the update methodand how it should be

and this when i do: this is when i do CALL the function and try to see if it move with the paddle

(sorry for my english)

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Just trying to make sure I understand: you're generating the collision rectangles on every update? Why not create them once then update the positions? \$\endgroup\$
    – Mike Cluck
    Commented May 13, 2012 at 18:43
  • \$\begingroup\$ i just tried that and i get an error:Error 1 Cannot modify the return value of 'System.Collections.Generic.List<Microsoft.Xna.Framework.Rectangle>.this[int]' so i have to create the rectangle every time i guess. \$\endgroup\$
    – samy
    Commented May 13, 2012 at 19:18
  • \$\begingroup\$ i read a litell about the error and use your suggustion and i just changed the List<Rectangle> to an arry and just update the position like you said and it solved my problem! i realy want to give you right answer :) \$\endgroup\$
    – samy
    Commented May 13, 2012 at 19:38
  • \$\begingroup\$ Now you can ;) I'm glad that worked for you. \$\endgroup\$
    – Mike Cluck
    Commented May 14, 2012 at 0:22

1 Answer 1

1
\$\begingroup\$

It looks like you're generating new collision rectangles every update and this probably means that you're a) overriding the rectangle you mean to draw or b) trying to access a rectangle which no longer exists. Creating the rectangles once then updating their positions would fix that (not to mention be much more efficient that allocating new rectangles every fraction of a second ;) ).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .