Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your problem is that you're moving the player, <em>then</em> checking to see if the space he moved to is blocked. Instead, you want to figure out the position that he's going to want to move to, see if it's blocked, and then only move him if it isn't blocked. Also, you can always add in a clause to exclude his current space. i.e. as long as your character isn't changing grid spaces, he can <em>always</em> move, but once he is about to change grid spaces you should check to see if he will collide with something.</p> <p>Here is your code below:</p> <pre><code>if(moveDirection == 1 &amp;&amp; !doesNeedShiftWorld) { heroY += _playerSpeed; [self checkBlocked:1]; _currentAnimation = _upAnimation; _moving = YES; } </code></pre> <p>It should be something like this:</p> <pre><code>if(moveDirection == 1 &amp;&amp; !doesNeedShiftWorld) { //Figure out if the player is changing grid spaces. BOOL isChangingSpaces = ((int)((heroY + _playerSpeed) / myGridSizeVariable) != (int)(heroY / myGridSizeVariable)); //The player should be able to move either if he isn't //changing grid spaces or if his destination space is free. if ( !isChangingSpaces || (spaceIsOpenAtX:heroX andY:heroY+_playerSpeed) ) { heroY += _playerSpeed; _currentAnimation = _upAnimation; _moving = YES; } } </code></pre> <p>And you really should try to make your code <em>much</em> more object-oriented. At this point, it seems to be completely procedural and all your variables are globals, which is definitely not a good way to go. "checkBlocked" should be rewritten as "spaceIsOpenAtX: andY:" because then you can put any X and Y coordinate in that you want in order to see if that position is blocked. As you've done it now, your code is too abstract (passing around integers with no indication of what they <em>mean</em> other than your actual if statement checks) and it can't be applied for any situation whatsoever aside from the single use you gave it.</p> <p>Really, you should have player.speed and player.animation and [maze spaceIsOpen], etc. You're using Objective-C, not just C. And even if you were using C it would be a better idea to do it in an OO way.</p>
 

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