Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://jsbin.com/ubifuf/2/edit" rel="nofollow noreferrer">This JSFiddle (which generates the table below)</a> shows that your loop doesn't behave how it's supposed to. Starting with hypothetical x positions of <code>[50, 40, 30, 20, 10, 0]</code>, this is what it looks like after 10 iterations:</p> <pre><code>moves X[0] X[1] X[2] X[3] X[4] X[5] ---------------------------------------------------- 0 50 40 30 20 10 0 1 51 41 40 30 20 10 2 52 42 31 40 30 20 3 53 43 32 41 40 30 4 54 44 33 42 31 40 5 55 45 34 43 32 41 6 56 46 35 44 33 42 7 57 47 36 45 34 43 8 58 48 37 46 35 44 9 59 49 38 47 36 45 10 60 50 39 48 37 46 </code></pre> <p>I've been playing around with it but can't think of a good way to fix this code to move the pieces the way they should. I think it's a little bit more complicated than you can do with just math or just a single for loop. One possibility would be to store which direction each block is moving.</p> <p>You could also figure out each block's destination (i.e., where will it be in 10 steps?) then interpolate to make it move smoothly. This method has the advantage that you can ensure the blocks are getting to the right place before you work on the animation - making sure the game logic works before worrying about how nice it looks.</p> <hr> <p>I <em>think</em> <a href="http://jsbin.com/ubifuf/6/edit" rel="nofollow noreferrer">this is closer</a> to what you're trying to do (rows without a number of moves are the destination array):</p> <pre><code>moves X[0] X[1] X[2] X[3] X[4] X[5] X[6] X[7] X[8] X[9] X[10] -------------------------------------------------------------------------------------------- 0 50 40 30 30 30 20 20 20 30 40 50 50 40 30 30 30 20 20 20 30 40 50 1 51 41 31 30 30 21 20 20 29 39 49 2 52 42 32 30 30 22 20 20 28 38 48 3 53 43 33 30 30 23 20 20 27 37 47 4 54 44 34 30 30 24 20 20 26 36 46 5 55 45 35 30 30 25 20 20 25 35 45 6 56 46 36 30 30 26 20 20 24 34 44 7 57 47 37 30 30 27 20 20 23 33 43 8 58 48 38 30 30 28 20 20 22 32 42 9 59 49 39 30 30 29 20 20 21 31 41 10 60 50 40 30 30 30 20 20 20 30 40 60 50 40 30 30 30 20 20 20 30 40 11 61 51 41 31 30 30 21 20 20 29 39 12 62 52 42 32 30 30 22 20 20 28 38 13 63 53 43 33 30 30 23 20 20 27 37 14 64 54 44 34 30 30 24 20 20 26 36 15 65 55 45 35 30 30 25 20 20 25 35 16 66 56 46 36 30 30 26 20 20 24 34 17 67 57 47 37 30 30 27 20 20 23 33 18 68 58 48 38 30 30 28 20 20 22 32 19 69 59 49 39 30 30 29 20 20 21 31 20 70 60 50 40 30 30 30 20 20 20 30 70 60 50 40 30 30 30 20 20 20 30 21 71 61 51 41 31 30 30 21 20 20 29 </code></pre> <p>Or graphically:</p> <p><img src="https://i.stack.imgur.com/uJmF6.png" alt="enter image description here"></p> <p>I'll try to translate it into Java, but my Java's rusty and I can't plug it into your code, so I can't guarantee it will be perfect. The important part is the logic. Also note that it is just for x values, but it should be trivial to add y values:</p> <p>You need a destination array that is a copy of <code>X</code>:</p> <p><code>int[] dests = (int[])X.clone();</code></p> <p>Then, inside <code>MovePlayer</code>, set it based on the direction of movement only if the blocks have reached their destinations (and thus need new ones):</p> <pre><code>if(IsMoving() &amp;&amp; X.equals(dests)) { for(int i = dests.length - 1; i &gt; 0; i--) { if(X[i] != X[i-1]) dests[i] = X[i-1]; } if(left) { dests[0] = X[0] - 10; } else if(right) { dests[0] = X[0] + 10; } } </code></pre> <p>Finally, inch the blocks towards their destinations:</p> <pre><code>for(var i = 0; i &lt; X.length; i++) { X[i] += Integer.signum(dests[i] - X[i]); } </code></pre> <p><code>signum</code> returns one of <code>[-1, 0, 1]</code> depending on the sign of the argument.</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