Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. @implementation MainPlayScene
  2. {
  3. CCPhysicsNode *_physics;
  4. CCNode *MyPhysicsBody;
  5. CCNode *bottomBody;
  6.  
  7. }
  8.  
  9. + (instancetype)scene
  10. {
  11. return [[self alloc] init];
  12. }
  13.  
  14. - (instancetype)init
  15. {
  16. // Apple recommend assigning self with supers return value, and handling self not created
  17. self = [super init];
  18. if (!self) return(nil);
  19.  
  20. _physics = [CCPhysicsNode node];
  21. _physics.debugDraw = YES;
  22. [self addChild:_physics z:1];
  23.  
  24. /// BOTTOM
  25. CGRect bottomRect = CGRectMake(0, 0, [CCDirector sharedDirector].viewSize.width, 10);
  26. bottomBody = [CCNode node];
  27. bottomBody.physicsBody = [CCPhysicsBody bodyWithPolylineFromRect:bottomRect cornerRadius:0];
  28. bottomBody.physicsBody.collisionCategories = @[@"Bottom"];
  29. bottomBody.physicsBody.type = CCPhysicsBodyTypeStatic;
  30. [_physics addChild:bottomBody];
  31.  
  32. /// MyBody to bounce around
  33. MyPhysicsBody = [CCSprite spriteWithImageNamed:@"MyBody-64x64-24.png"];
  34. MyPhysicsBody.position = ccp((self.contentSize.width/2),(self.contentSize.height/2));
  35. MyPhysicsBody = [CCNode node];
  36. MyPhysicsBody.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, MyPhysicsBody.contentSize.height,MyPhysicsBody.contentSize.width} cornerRadius:0];
  37. MyPhysicsBody.physicsBody.collisionCategories = @[@"MyBody"];
  38. [_physics addChild:MyPhysicsBody z:150];
  39.  
  40. self.userInteractionEnabled = YES;
  41. return self;
  42. }
  43.  
  44. - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
  45. {
  46. CCLOG(@"Touch Detected");
  47. [MyPhysicsBody.physicsBody applyImpulse:ccp(0, 300.f)];
  48. }
  49.  
  50. /// try onCollisionEnter first ... nothing
  51. -(void)onCollisionEnter:(CCNode *)entity collisionPair:(CCPhysicsCollisionPair *)pair
  52. {
  53. if ([entity.physicsBody.collisionCategories isEqual: @"Bottom"]) {
  54. CCLOG(@"Hit bottomBody");
  55. }
  56.  
  57. }
  58.  
  59. /// try ccPhysicsCollisionBegin pair ... nothing
  60. -(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair MyBody:(CCNode *) MyBody Botton:(CCNode *)Bottom
  61. {
  62. CCLOG(@"Hit bottomBody");
  63. return TRUE;
  64. }
  65.  
  66. _arrow.physicsBody.collisionType = @"arrow";
  67. _obstacle.physicsBody.collisionType = @"obstacle";
  68.  
  69. -(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair arrow:(CCNode *)arrow obstacle:(CCNode *)obstacle
  70. {
  71. // Do some cool stuff
  72. return TRUE;
  73. }
  74.  
  75. _physics.collisionDelegate = self;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement