ios - Accessing individual objects that all have the same categoryBitMask -
i have several game world objects player needs interact individually upon physicsbody.categorybitmask contacting them. instead of using separate categorybitmasks each individual object (object count surpasses categorybitmask's limit, 32) use 1 categorybitmask , gave objects individual names. here's how looks in code:
-(void)createcollisionareas { if (_tilemap) { tmxobjectgroup *group = [_tilemap groupnamed:@"contactzone"]; //layer's name. //province gateway. nsdictionary *singularobject = [group objectnamed:@"msgdifferentprovince"]; if (singularobject) { cgfloat x = [singularobject[@"x"] floatvalue]; cgfloat y = [singularobject[@"y"] floatvalue]; cgfloat w = [singularobject[@"width"] floatvalue]; cgfloat h = [singularobject[@"height"] floatvalue]; skspritenode *object = [skspritenode spritenodewithcolor:[skcolor redcolor] size:cgsizemake(w, h)]; object.name = @"provincegateway"; object.position = cgpointmake(x + w/2, y + h/2); object.physicsbody = [skphysicsbody bodywithrectangleofsize:cgsizemake(w, h)]; object.physicsbody.categorybitmask = terraincategory; object.physicsbody.contacttestbitmask = playercategory; object.physicsbody.collisionbitmask = 0; object.physicsbody.dynamic = no; object.physicsbody.friction = 0; object.hidden = yes; [_backgroundlayer addchild:object]; } /*more code written below. lazy copy & paste all*/ } -(void)didbegincontact:(skphysicscontact *)contact { skphysicsbody *firstbody, *secondbody; //create 2 placeholder reference's contacting objects. if (contact.bodya.categorybitmask < contact.bodyb.categorybitmask) //if bodya has smallest of 2 bits... { firstbody = contact.bodya; //...it firstbody reference [smallest of 2 (category) bits.]. secondbody = contact.bodyb; //...and bodyb secondbody reference [largest of 2 bits.]. } else //this reverse of above code (just in case know what's what). { firstbody = contact.bodyb; secondbody = contact.bodya; } /**boundary contacts*/ if ((firstbody.categorybitmask == nogocategory) && (secondbody.categorybitmask == playercategory)) { //boundary contacted player. if ([_backgroundlayer childnodewithname:@"bounds"]) { nslog(@"player contacted map bounds."); } if ([_backgroundlayer childnodewithname:@"nogo"]) { nslog(@"player can't go further."); } if ([_backgroundlayer childnodewithname:@"provincegateway"]) { nslog(@"another province ahead. can't go further."); } if ([_backgroundlayer childnodewithname:@"slope"]) { nslog(@"player contacted slope."); } }
the problem is, in didbegincontact method, when player contacts object code gets executed. code objects player hasn't contacted yet. means if statements, such if ([_backgroundlayer childnodewithname:@"slope"]), not complete. can tell me how write out if statements individual object contact? following if statement, inside didbegincontact, doesn't work either:
if ([_player intersectsnode:[_backgroundlayer childnodewithname:@"slope"]])
your if
statements checking if child exists, seems do. think want check if collision node has name looking for, change:
if ([_backgroundlayer childnodewithname:@"bounds"])
to:
if ([firstbody.node.name isequaltostring:@"bounds"])
Comments
Post a Comment