Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Aleph thanks for your suggestion. I found that it was the code which determines when to assign a new motionTarget, that was faulty, not the code i posted to begin with. When you mentioned keeping track of each nodes position, i thought of my motionTarget determination code and found the error after 2-3 hours. I ended up doing it like this:</p> <pre><code>-(void) updatePositions:(ccTime) dt { for (MSWFiend *currFiend in fiendSet) { int fiendGX,fiendGY,targetGX,targetGY,dGX,dGY; float x,y,snappedX,snappedY; BOOL snappedIntoPosition = FALSE; fiendGX = (int)round(100.0f*(currFiend.position.x/20)); fiendGY = (int)round(100.0f*(currFiend.position.y/20)); targetGX = (int)round(100.0f*(currFiend.motionTarget.x/20)); targetGY = (int)round(100.0f*(currFiend.motionTarget.y/20)); snappedX = currFiend.position.x; snappedY = currFiend.position.y; dGX = abs(fiendGX-targetGX); dGY = abs(fiendGY-targetGY); float snappingThreshold; //1 = snap when 0.1 from motionTarget. snappingThreshold = currFiend.speed/10; if (dGX &lt; snappingThreshold &amp;&amp; dGY &lt; snappingThreshold) { snappingThreshold = currFiend.motionTarget.x; snappingThreshold = currFiend.motionTarget.y; int newPathStep; newPathStep = currFiend.pathStep + 1; currFiend.pathStep = newPathStep; } int gX,gY; gX = [[currFiend.path objectAtIndex:currFiend.pathStep] nodeX]; gY = (tileMap.mapSize.height-[[currFiend.path objectAtIndex:currFiend.pathStep] nodeY])-1; currFiend.motionTarget = ccp(gX*20,gY*20); //Assign motion target to the next A* node. This is later used by the position updater. if (currFiend.motionTarget.x != -1 &amp;&amp; currFiend.motionTarget.y != -1) { x = currFiend.motionTarget.x; y = currFiend.motionTarget.y; if ((int)floor(currFiend.position.x) &lt; (int)floor(currFiend.motionTarget.x)) { //Move right x = snappedX+(currFiend.speed*dt); if (x &gt; currFiend.motionTarget.x) { x = currFiend.motionTarget.x; } y = snappedY; } if ((int)floor(currFiend.position.x) &gt; (int)floor(currFiend.motionTarget.x)) { //Move left x = snappedX-(currFiend.speed*dt); if (x &lt; currFiend.motionTarget.x) { x = currFiend.motionTarget.x; } y = snappedY; } if ((int)floor(currFiend.position.y) &lt; (int)floor(currFiend.motionTarget.y)) { //Move up y = snappedY+(currFiend.speed*dt); if (y &gt; currFiend.motionTarget.y) { y = currFiend.motionTarget.y; } x = snappedX; } if ((int)floor(currFiend.position.y) &gt; (int)floor(currFiend.motionTarget.y)) { //Move down y = snappedY-(currFiend.speed*dt); if (y &lt; currFiend.motionTarget.y) { y = currFiend.motionTarget.y; } x = snappedX; } } currFiend.position = ccp(x,y); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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