ios - I'm having an issue detecting multiple different collisions in SpriteKit (Obj-C) -


i'm building game using objective-c , spritekit. in game, there asteroids come down @ player, has avoid them. asteroids spawned every 0.5 seconds. there type of asteroid, "gold asteroids" use different variable in-game, , can shot @ coins.

another part of game involves buying new "ships" navigate through asteroids. 1 ship has special ability of being able shoot @ regular asteroids destroy them golden ones. problem begins.

this code detects collisions between regular asteroids , bullets. lot of code comes sangony's answer on this stackoverflow post.

nsstring *playerimage = [[nsuserdefaults standarduserdefaults]                          stringforkey:@"playerimage"];  if ([playerimage  isequal: @"crazyben.png"]) //make sure ship 1 special ability destroy regular asteroids, not issue {     nslog(@"crazy ben enabled");     uint32_t collision3 = (contact.bodya.categorybitmask | contact.bodyb.categorybitmask);      if (collision3 == (bulletcategory | asteroidcategory))     {         nslog(@"collision3");         (skspritenode * object in self.asteroidarray);         {             nslog(@"for statement");             if ([contact.bodya.node.name                   isequaltostring:[nsstring stringwithformat:@"asteroid-%i", self.asteroidcounter]] ||[contact.bodyb.node.name isequaltostring:[nsstring stringwithformat:@"asteroid-%i", self.asteroidcounter]]) //broken here, allows detect collision between bullet , asteroid matches "self.asteroidcounter" int             {                 nslog(@"crazyhit");                 [self runaction:[skaction playsoundfilenamed:@"gunshot.mp3" waitforcompletion:no]];                 [self.bullet removefromparent];                 [self.asteroid removefromparent];             }         }      } } 

the part broken part when it's time detect collisions. using code, able destroy asteroid corresponds self.asteroidcounter integer, spawned asteroid. if remove line

nslog(@"for statement");             if ([contact.bodya.node.name                   isequaltostring:[nsstring stringwithformat:@"asteroid-%i", self.asteroidcounter]] ||[contact.bodyb.node.name isequaltostring:[nsstring stringwithformat:@"asteroid-%i", self.asteroidcounter]]) 

to allow destroy asteroid regardless of whether or not has same number self.asteroidcounter, instead of destroying specific asteroid hit, appears destroy random asteroid on screen instead.

what want asteroid hit removed parent.

thanks in advance, , if there information missing or if clarification, gladly add or clarify it.

edit: edited code, , added variables asteroidcounter b through j. asteroidcounterb = asteroidcountera - 1, asteroidcounterc = asteroidcounterb - 1 , on. this, behaviour identical when treating them alike, same name, rather identifying them using asteroidcounter variables. believe issue in code says

[self.asteroid removefromparent]; 

when that, not remove specific asteroid parent, removes 1 of them parent. if remove asteroidcounter variables (apart original), there way this?

[self.asteroid-%i removefromparent]; 

obviously, doesn't work, great if there way that.

if going use unique name each node, easy way add number @ end of name.

self.objectid++; mynode.name = [nsstring stringwithformat:@"mynode-%lu",self.objectid]; 

self.object being property @property (nonatomic) long objectid;

to check of 'mynode' objects involved collision can this:

if (collision == (bulletcategory | asteroidcategory)) {      for(skspritenode *object in self.asteroidarray) {         if(([object.name isequaltostring:contact.bodyb.node.name]) || ([object.name isequaltostring:contact.bodya.node.name])) {                 // code         }     } } 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -