Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This immediately stands out to me (from CheckBounce):</p> <pre><code> movement.X *= 1; </code></pre> <p>Could be a copying error, or you forgot to put a '-'.</p> <p>Also, consider using the Rectangle.Contains/Intersects method(s) to streamline some collision code, and MathHelper.Clamp to keep your paddles in bounds. This is more just for future reference, since your methods work, but it's nice to take advantage of the helpful tools in XNA.</p> <p>Edit: Concerning those 'helpful tools':</p> <p>The Rectangle class has the methods Intersect and Contains, which can tell you if that rectangle is intersecting another rectangle or containing a certain point, respectively. You say your ball is just a top left position and a texture, but I see in your collision checking you also check for the radius of the ball. I think you'd have an easier time defining a Rectangle bounding area for your ball and using the Intersects method to check for collision. That simplifies your collision code to:</p> <pre><code>public void CheckBounce() { if (myBounds.Intersects(LeftPaddle.Bounds) || myBounds.Intersects(RightPaddle.Bounds)) movement.X *= -1; } </code></pre> <p>Fairly simple, but not entirely safe-- if the ball manages to move far enough into a paddle that one frame of movement wouldn't free it from that paddle's bounds, you'd be stuck perpetually inverting the X velocity, producing a 'stuck ball' effect. So we can add just a bit more checking code to avoid that:</p> <pre><code>public void CheckBounce() { if ((myBounds.Intersects(LeftPaddle.Bounds) &amp;&amp; movement.X &lt; 0) || (myBounds.Intersects(RightPaddle.Bounds) &amp;&amp; movement.X &gt; 0)) movement.X *= -1; } </code></pre> <p>I apologize if the inline conditionals are a little too dense. What this means is, if the ball is moving left and hits the right paddle, invert the X. Likewise, if it's moving right and hits the left paddle, invert the X. This removes the 'sticking'.</p> <p>Now, as for MathHelper.Clamp, in your situation I would use it to restrict paddle movement. MathHelper.Clamp simply clamps a value between an upper and lower bound. It's the equivalent of using Math.Min, then Math.Max.</p> <pre><code>private void CheckBorders() { //clamps a value to a min and a max MathHelper.Clamp(recPad.Y, borders.Top, borders.Bottom - recPad.Height); } </code></pre> <p>This clamps the Y position of your rectangle between the top of your borders, and the bottom of your borders minus the height of the rectangle. That last bit keeps the bottom of your rectangle from clipping the bottom of the borders by taking the height into account.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload