I created an application that has a constant stream of "stone" nodes falling from the sky in two different sizes. The player controls the third stone object and receives points depending on which of the two types of falling stone is going to. Finally, the beam object functions like moving earth. So far, I have added conflict detection in my application, which detects collisions between different stones and a beam. This is my collision code:
static const uint32_t stoneCategory = 1;
static const uint32_t beamCategory = 2;
static const uint32_t stoneCategory2 = 4;
and
-(void)didBeginContact:(SKPhysicsContact *)contact{
SKPhysicsBody *firstBody;
SKPhysicsBody *secondBody;
CGPoint contactPoint = contact.contactPoint;
float contact_x = contactPoint.x;
float contact_y = contactPoint.y;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if ((firstBody.categoryBitMask & stoneCategory) != 0)
{
SKPhysicsJointFixed *joint =
[SKPhysicsJointFixed jointWithBodyA:contact.bodyA
bodyB:contact.bodyB
anchor:CGPointMake(contact_x, contact_y)];
[self.physicsWorld addJoint:joint];
}
if ((firstBody.categoryBitMask & stoneCategory) != 0 &&
(secondBody.categoryBitMask & stoneCategory2) != 0)
{
NSLog(@"Hit");
[self enumerateChildNodesWithName:@"stone" usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];
}
, . , . , , .
: , , , ( "" ), , , .
, . : node, ? , , , .
:
-(void) addStone
{
SKSpriteNode *stone = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(12, 12)];
stone.position = CGPointMake(skRand(0, self.size.width), self.size.height+12);
stone.name = @"stone";
stone.physicsBody.categoryBitMask = stoneCategory2;
stone.physicsBody.contactTestBitMask = stoneCategory;
stone.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:stone.size];
stone.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:stone];
}
createSceneContents
SKAction *makeStone= [SKAction sequence:@[
[SKAction performSelector:@selector(addStone) onTarget:self],
[SKAction waitForDuration:0.30 withRange:0.25]
]];
[self runAction:[SKAction repeatActionForever:makeStone]];
. , .