2011-04-13 15 views
0

嗨跳的身體我想創建像鬥球比賽,拖動指針和Box2D的iphone

我使用Box2D的在iPhone與UIView的物理引擎, 我創造的世界和對象添加到它。 設置好每件事情。

但我不知道如何繪製上漿和彈跳時用戶釋放上漿。 (查看原始圖)

如果有任何人有解決辦法請。

謝謝。 Here is Screen Short

回答

1

我通過下面的代碼解決了這個問題。聲明可變數組。

NSMutableArray *arrTouches; 

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if([[touches allObjects] count] == 1) 
    { 
     if([arrTouches count]==2) 
     { 
      UITouch *touch = [[touches allObjects] objectAtIndex:0]; 
      CGPoint location = [touch locationInView: [touch view]]; 
      location = [[CCDirector sharedDirector] convertToGL: location]; 
      [arrTouches replaceObjectAtIndex:1 withObject:NSStringFromCGPoint(location)]; 
      [self jumpBall]; 
     } 
    } 
} 

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if([[touches allObjects] count] == 1) 
    { 
     if([arrTouches count] == 1) 
     { 
      UITouch *touch = [[touches allObjects] objectAtIndex:0]; 
      CGPoint location = [touch locationInView: [touch view]]; 
      location = [[CCDirector sharedDirector] convertToGL: location]; 
      [arrTouches addObject:NSStringFromCGPoint(location)]; 
     } 
     else if([arrTouches count] == 2) 
     { 
      UITouch *touch = [[touches allObjects] objectAtIndex:0]; 
      CGPoint location = [touch locationInView: [touch view]]; 
      location = [[CCDirector sharedDirector] convertToGL: location]; 
      [arrTouches replaceObjectAtIndex:1 withObject:NSStringFromCGPoint(location)]; 
     } 
    } 
} 

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[touches allObjects] objectAtIndex:0]; 
    CGPoint location = [touch locationInView: [touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL: location]; 

    if([[touches allObjects] count] == 1 && CGRectContainsPoint(_ball.boundingBox, location)) 
    { 
     NSLog(@"Touch Begin : X: %f Y: %f",location.x,location.y); 
     [arrTouches addObject:NSStringFromCGPoint(location)]; 
    } 
} 

-(void)jumpBall 
{ 
    CGPoint diff = ccpSub(CGPointFromString([arrTouches objectAtIndex:0]), CGPointFromString([arrTouches objectAtIndex:1])); 

    CGPoint oldP = CGPointFromString([arrTouches objectAtIndex:0]); 
    CGPoint newP = CGPointFromString([arrTouches objectAtIndex:1]); 

    CGPoint p = CGPointFromString([arrTouches objectAtIndex:1]); 

    float floatX = p.x/PTM_RATIO; 
    float floatY = p.y/PTM_RATIO; 

    _body->ApplyForce(b2Vec2(-5.0f*diff.x,5.0f*newP.y), _body->GetPosition()); 

    [arrTouches removeAllObjects]; 

}