ios - creating physics body for a SKSpriteNode in order to detect contact -


as title suggest, have issue creating contact between enemies sprite , hero laser(bullet). create enemies trough following method , adding them view.

-(void)enemies{ //not come int goornot = [self getrandomnumberbetween:0 to:1];  if(goornot == 1){      skspritenode *enemy;      int randomenemy = [self getrandomnumberbetween:0 to:1];     if(randomenemy == 0)         enemy = [skspritenode spritenodewithimagenamed:@"enemy_spaceship.png"];     else         enemy = [skspritenode spritenodewithimagenamed:@"spaceship15.png"];       enemy.scale = 0.4;      enemy.position = cgpointmake(450, 0);     enemy.hidden = no;      cgpoint location = cgpointmake(450, 320);      skaction *moveaction = [skaction moveto:location duration:2.5];     skaction *doneaction = [skaction runblock:(dispatch_block_t)^() {         //nslog(@"animation completed");         enemy.hidden = yes;     }];      skaction *moveasteroidactionwithdone = [skaction sequence:@[moveaction,doneaction ]];      [enemy runaction:moveasteroidactionwithdone withkey:@"asteroidmoving"];     [self addchild:enemy];      _enemybullets = [[nsmutablearray alloc] initwithcapacity:knumenemybullet];     (int = 0; < knumenemybullet; ++i) {         skspritenode *enemybullets = [skspritenode spritenodewithimagenamed:@"rocket"];         enemybullets.hidden = yes;         [enemybullets setxscale:0.5];         [enemybullets setyscale:0.5];         [_enemybullets addobject:enemybullets];         [enemy addchild:enemybullets];     }  }  } 

then add bullets through mutable array , adding bullets enemies sprites child. part works. can create contact between hero , enemy bullet , gets detected. have issue hero's laser not create contact enemy can't make enemy take hit laser. tries adding physics body method using throws other sprites hell , don't respond anymore.

the following code collision code using in update method:

for (skspritenode *enemybullets in _enemybullets) {         if (enemybullets.hidden) {             continue;         }         if ([_ship intersectsnode:enemybullets]) {             enemybullets.hidden = yes;             skaction *blink = [skaction sequence:@[[skaction fadeoutwithduration:0.1],                                                    [skaction fadeinwithduration:0.1]]];             skaction *blinkfortime = [skaction repeataction:blink count:4];             skaction *shipexplosionsound = [skaction playsoundfilenamed:@"explosion_large.caf" waitforcompletion:no];             [_ship runaction:[skaction sequence:@[shipexplosionsound,blinkfortime]]];             _lives--;             _liveslabel.text = [nsstring stringwithformat:@"%d lives", _lives];             nslog(@"your ship has been hit!");         }     } 

is there way can use create physics body enemies can create contact between hero laser , enemies sprite same structure have ? amazingly appreciated.

you can create physics body enemies. right after code enemy = [skspritenode spritenodewithimagenamed:@"enemy_spaceship.png"]; can add physics body this:

enemy.physicsbody = [skphysicsbody bodywithrectangleofsize:self.size]; enemy.physicsbody.categorybitmask = categoryenemy; // or whatever need enemy.physicsbody.collisionbitmask = categoryrock; // or whatever need enemy.physicsbody.contacttestbitmask = categorybullet // or whatever need enemy.physicsbody.dynamic = no; // or yes enemy.physicsbody.allowsrotation = no; // or yes enemy.physicsbody.restitution = 0; // or value between 0 1 enemy.physicsbody.friction = 0.0; // or value between 0 1 

there many ways create physics body. can rectangle shape, circle shape, outline texture or draw own.

you should read apple docs on subject better understanding of can , properties available.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -