ios - How to detect same types of nodes touching each other consecutively (SpriteKit) -


actually rather algorithm , implementation problem.

i have array of nodes, these nodes have 3 types. distinctive speciality color.

i want detect if couple of same nodes touching consecutively create joint or it.

for example lets assume have bunch of balls in array can 3 colors red, blue, green. want find joints has 5 elements.

i tried recursive way of iterating through array. works doesn't. think way i'm doing has flaws.

- (void) findadjacentnode:(ballspritenode*) currentnode from:(nsmutablearray*)currentnodes{      (ballspritenode *node in currentnodes) {         float distance = [self getdistancebetween:node.position and:currentnode.position];          if ( distance - (node.size.width/2 + currentnode.size.width/2 ) <= 0             && [node.currentcolor isequal:((ballspritenode*)currentnode).currentcolor]             && ![node isequal:currentnode]             && ![samecolornodes containsobject:node])         {             [samecolornodes addobject:node];             [self findadjacentnode:node from:currentnodes];         }     }      if (samecolornodes.count >=5) {         [self removeselectednodesfromarray:samecolornodes];         return;     } } 

appreciated kind of , lead.

i've reached solve problem still little problem appears randomly. skspritenode subclass has isremoved variable track if node removed. see problem occurs removing same nodes again , again, infinite loop.


edit: changing implementation below solved main problem , it's good.

if (samecolornodes.count >=5) {     bool iseligibletoremove = yes;     (ballspritenode *node in samecolornodes) {         if (node.isremoved) {             iseligibletoremove = no;             break;         }     }      if(iseligibletoremove){         nslog(@"%@",samecolornodes);         [self removeselectednodesfromarray:samecolornodes];     } } 

right i'm dealing problem happens randomly , hard catch. method fails add nodes samecolornodes array should in it. example if 9 of them forming group takes 8 of them in it. works right every time if has 5 nodes.


edit 2:

this final version of algorithm. tested , works fine. if call method nodes in array. find nodes same colour , creating path touching each other.

- (void) findadjacentnode:(ballspritenode*)currentnode from:(nsmutablearray*)currentnodes{      recursivecount ++;      if(![samecolornodes containsobject:currentnode]){         [samecolornodes addobject:currentnode];     }      (ballspritenode *node in currentnodes) {         float distance = [self getdistancebetween:node.position and:currentnode.position];          if (node.balltype == brickball) {             continue;         }          if (distance - (node.size.width/2 + currentnode.size.width/2) <= 0             && [node.currentcolor isequal:((ballspritenode*)currentnode).currentcolor]             && ![node isequal:currentnode]             && ![samecolornodes containsobject:node])         {             [samecolornodes addobject:node];             [self findadjacentnode:node from:currentnodes];         }     }      recursivecount --;      if (samecolornodes.count >=5 && recursivecount == 0) {         bool iseligibletoremove = yes;         (ballspritenode *node in samecolornodes) {             if (node.isremoved) {                 iseligibletoremove = no;                 break;             }         }          if(iseligibletoremove){             nslog(@"%@",samecolornodes);             [self removeselectednodesfromarray:samecolornodes];                 }     } } 

update:

the following groups sprites close in proximity , have same currentcolor property. creates dictionary contains sprite's index key , array of matching sprites object:

- (cgfloat) getdistancebetween:(cgpoint)point1 and:(cgpoint)point2 {     cgfloat dx = point1.x - point2.x;     cgfloat dy = point1.y - point2.y;     return sqrt(dx*dx+dy*dy); }  - (void) countmatches:(nsmutablearray*)currentnodes {     nsmutabledictionary *colorcount = [nsmutabledictionary dictionary];     nsmutablearray *nodes = [currentnodes mutablecopy];     (nsuinteger i=0;i<nodes.count;i++) {         ballspritenode *ball1 = [nodes objectatindex:i];         nsnumber *key = [nsnumber numberwithunsignedinteger:i];         (nsuinteger j=nodes.count-1;j>i;j--) {             ballspritenode *ball2 = [nodes objectatindex:j];             float distance = [self getdistancebetween:ball1.position and:ball2.position];             if (distance - (ball2.size.width/2 + ball1.size.width/2) <= 0                 && [ball2.currentcolor isequal:ball1.currentcolor]) {                 nsmutablearray *nodearray = [colorcount objectforkey:key];                 if (!nodearray) {                     nodearray = [nsmutablearray arraywithcapacity:5];                 }                 [nodearray addobject:ball2];                 [colorcount setobject:nodearray forkey:key];                 [nodes removeobjectatindex:j];             }         }         nsmutablearray *nodearray = [colorcount objectforkey:key];         if (nodearray) {             [nodearray addobject:ball1];             [colorcount setobject:nodearray forkey:key];         }     }      [colorcount enumeratekeysandobjectsusingblock:^(id key, id obj, bool *stop) {         nsarray *array = obj;         if (array.count >= 5) {             // add code join nodes here             nslog(@"color: %@ count: %ld", key, array.count);         }     }]; } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -