Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 2.39 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Drag pointer and jump body in box2d iphone
  2. NSMutableArray *arrTouches;
  3.  
  4. - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  5. {
  6.     if([[touches allObjects] count] == 1)
  7.     {
  8.         if([arrTouches count]==2)
  9.         {
  10.             UITouch *touch = [[touches allObjects] objectAtIndex:0];
  11.             CGPoint location = [touch locationInView: [touch view]];
  12.             location = [[CCDirector sharedDirector] convertToGL: location];
  13.             [arrTouches replaceObjectAtIndex:1 withObject:NSStringFromCGPoint(location)];
  14.             [self jumpBall];
  15.         }
  16.     }
  17. }
  18.  
  19. - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  20. {
  21.     if([[touches allObjects] count] == 1)
  22.     {
  23.         if([arrTouches count] == 1)
  24.         {
  25.             UITouch *touch = [[touches allObjects] objectAtIndex:0];
  26.             CGPoint location = [touch locationInView: [touch view]];
  27.             location = [[CCDirector sharedDirector] convertToGL: location];
  28.             [arrTouches addObject:NSStringFromCGPoint(location)];
  29.         }
  30.         else if([arrTouches count] == 2)
  31.         {
  32.             UITouch *touch = [[touches allObjects] objectAtIndex:0];
  33.             CGPoint location = [touch locationInView: [touch view]];
  34.             location = [[CCDirector sharedDirector] convertToGL: location];
  35.             [arrTouches replaceObjectAtIndex:1 withObject:NSStringFromCGPoint(location)];
  36.         }
  37.     }
  38. }
  39.  
  40. - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  41. {
  42.     UITouch *touch = [[touches allObjects] objectAtIndex:0];
  43.     CGPoint location = [touch locationInView: [touch view]];
  44.     location = [[CCDirector sharedDirector] convertToGL: location];
  45.  
  46.     if([[touches allObjects] count] == 1 && CGRectContainsPoint(_ball.boundingBox, location))
  47.     {
  48.         NSLog(@"Touch Begin : X: %f Y: %f",location.x,location.y);
  49.         [arrTouches addObject:NSStringFromCGPoint(location)];
  50.     }
  51. }
  52.  
  53. -(void)jumpBall
  54. {
  55.     CGPoint diff = ccpSub(CGPointFromString([arrTouches objectAtIndex:0]), CGPointFromString([arrTouches objectAtIndex:1]));
  56.  
  57.     CGPoint oldP = CGPointFromString([arrTouches objectAtIndex:0]);
  58.     CGPoint newP = CGPointFromString([arrTouches objectAtIndex:1]);
  59.  
  60.     CGPoint p = CGPointFromString([arrTouches objectAtIndex:1]);
  61.  
  62.     float floatX = p.x/PTM_RATIO;
  63.     float floatY = p.y/PTM_RATIO;
  64.  
  65.     _body->ApplyForce(b2Vec2(-5.0f*diff.x,5.0f*newP.y), _body->GetPosition());
  66.  
  67.     [arrTouches removeAllObjects];
  68.  
  69. }