Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that the issue is caused by some changes you make to <code>speedRight</code> or to <code>playerRect</code> somewhere in your game. So if you're using VisualStudio just set a breakpoint and check the <code>speedRight</code> and <code>playerRect</code> values.</p> <p>Although there are a few changes you should make to your game. Firstly you should create a public field into the <code>Player</code> class of type <code>Texture</code> as for the <code>Spike</code> class, then create another field of type <code>Vector2</code> which indicates the speed and the direction :</p> <pre><code>public class Player{ public Vector2 DirectionSpeed; public Texture2D Texture; //... public Player(){ //initialize your fields } } </code></pre> <p>Then you can handle the collision by using the method <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.rectangle.aspx" rel="nofollow"><code>Intersects()</code></a>:</p> <pre><code>if(player.Texture.Bounds.Intersects(spike.Bounds)){ player.DirectionSpeed = Vector.Zero; } </code></pre> <p>If you are trying to handle the collisions with more than one object of type <code>Spike</code>, you have to create a <code>List&lt;Spike&gt;</code> and then iterate with a cicle the entire list:</p> <pre><code>foreach(Spike s in listSpikes){ if(player.Texture.Bounds.Intersects(s.Bounds)){ player.DirectionSpeed = Vector.Zero; } } </code></pre> <hr> <p><strong>EDIT:</strong></p> <p>Moreover if <code>speedRight</code> doesn't equal 1 or a submultiple of <code>spikesRect.Left - 1</code> it's obvious that by increasing the position with it <code>playerRect.Right</code> exceeds <code>spikesRect.Left - 1</code>. A solution could be:</p> <pre><code>if (playerRect.Right &gt; spikesRect.Left - 1) { playerRect.Location = spikesRect.Left - playerRect.Width - 1; speedRight = 0; } </code></pre>
    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. This table or related slice is empty.
    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