Skip to content

Commit

Permalink
Remove sad macs when the bounce off screen
Browse files Browse the repository at this point in the history
  • Loading branch information
st3fan committed Apr 24, 2018
1 parent 954e707 commit 34bce96
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SadMac/SadMacScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#import <SpriteKit/SpriteKit.h>

@interface SadMacScene: SKScene {
@interface SadMacScene: SKScene <SKPhysicsContactDelegate> {
BOOL _isPreview;
}
- (instancetype) initWithSize:(CGSize)size isPreview: (BOOL) isPreview;
Expand Down
16 changes: 16 additions & 0 deletions SadMac/SadMacScene.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ - (SKNode*) createSadMacAtPosition: (CGPoint) position {
node.physicsBody.density = 0.5;
node.physicsBody.angularVelocity = RandomAngularVelocity();
node.physicsBody.velocity = RandomVelocity();
node.physicsBody.categoryBitMask = 2;
node.physicsBody.contactTestBitMask = 1;
}

return node;
Expand All @@ -67,6 +69,11 @@ - (SKNode*) createSadMacAtPosition: (CGPoint) position {
- (void)sceneDidLoad {
self.backgroundColor = [NSColor blackColor];

self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect: NSInsetRect(self.frame, -400, -400)];
self.physicsBody.categoryBitMask = 1;
self.physicsBody.contactTestBitMask = 2;
self.physicsWorld.contactDelegate = self;

SKNode *floor = [self createFloor];
[self addChild: floor];

Expand Down Expand Up @@ -98,4 +105,13 @@ - (void)sceneDidLoad {
[node runAction: [SKAction sequence: actions]];
}

- (void)didBeginContact:(SKPhysicsContact *)contact {
if (contact.bodyA.categoryBitMask == 2 && contact.bodyB.categoryBitMask == 1) {
[contact.bodyA.node removeFromParent];
}
if (contact.bodyB.categoryBitMask == 2 && contact.bodyA.categoryBitMask == 1) {
[contact.bodyB.node removeFromParent];
}
}

@end

0 comments on commit 34bce96

Please sign in to comment.