2012-10-11 67 views
0

我是Cocos2d和Box2d的新手,我對你有一些問題。 在開發或嘗試玩遊戲時,我將動態身體拖到邊界(到我的ipone的框架),並且發生了一些事情,並且我動態身體的一半超出了框架!我不知道爲什麼?請儘快幫助我!動態身體超越框架

+0

你是否在box2d中放置了屏幕邊界? (多段線,請參閱cocos2d + box2d示例)。我可以想象,如果您手動調用身體上的SetTransform,這仍然會發生。使用鼠標關節拖動一個物體。 – LearnCocos2D

+0

感謝LearnCocos2d –

回答

0

我解決了它。我將下面的代碼寫入了ccTouchesMoved函數。

CGSize s = [[CCDirector sharedDirector] winSize]; 
      // Update if it is moved 
      CGPoint location = [tj.touch locationInView:tj.touch.view]; 
      location = [[CCDirector sharedDirector] convertToGL:location]; 
      int deger=50; // mydynamicbody width 
      float xcordinate=location.x; 
      float ycordinate=location.y; 
      if (xcordinate>(s.width-deger)) 
      { 
       xcordinate=s.width-deger; 
      } 
      if (ycordinate>(s.height-deger)) 
      { 
       ycordinate=s.height-deger; 
      } 
      if (xcordinate<deger) 
      { 
       xcordinate=deger; 
      } 
      if(ycordinate<deger) 
      { 
       ycordinate=deger; 
      } 
      // NSLog(@"cordinates %f , %f %g %g",xcordinate,ycordinate,s.width,s.height); 
      b2Vec2 worldLocation = b2Vec2(ptm(xcordinate),ptm(ycordinate)); 
      //b2Vec2 worldLocation = b2Vec2(200, 200); 
      tj.mouseJoint->SetTarget(worldLocation);