Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)

share|improve this question
1  
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? –  Mike C May 13 '12 at 18:43
    
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. –  samy May 13 '12 at 19:18
    
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 :) –  samy May 13 '12 at 19:38
    
Now you can ;) I'm glad that worked for you. –  Mike C May 14 '12 at 0:22

1 Answer 1

up vote 1 down vote accepted

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 ;) ).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.