2013-10-22 4 views
0

본문에 첨부 된 CCSpriteb2world입니다. 누군가가 터치하면 터치 위치로 이동하고 싶습니다. 정상적으로 작동하는 일반 스프라이트를 사용하지만 스프라이트에는 본문이 있지만 그렇지 않습니다. 그것은 접촉을 얻지 만 스프라이트를 움직이지 않습니다 (몸체가 스프라이트 또는 그 반대편을 따르는가?) 어떻게해야합니까? 터치에 상대적으로 힘을 가하는 것이 문제입니다.본문에 첨부 된 스프라이트 끌기

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPosition = [touch locationInView: [touch view]]; 


    //detect a touch ont the button 
    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) 
    { 

     CGPoint location=[[CCDirector sharedDirector] convertToGL: currentPosition]; 

     CCSprite *tempSprite = (CCSprite *) b->GetUserData(); 
     if(tempSprite.tag==2 ) 
     { 

      if(CGRectContainsPoint([tempSprite boundingBox], location)) 
      { 
       tempSprite.position=location; 
       NSLog(@"touched"); 
      } 
     } 

    } 

} 
+0

마우스 조인트가 가장 좋습니다. – Guru

답변

1

SetTransform 기능을 사용하여 본문의 위치를 ​​변경해보십시오. 이 자세는 운동 학적 또는 동적 신체 유형을 사용해야합니다 힘이나 설정 선 속도를 적용 변경하려는 경우

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint currentPosition = [touch locationInView: [touch view]]; 


//detect a touch ont the button 
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) 
{ 

    CGPoint location=[[CCDirector sharedDirector] convertToGL: currentPosition]; 

    CCSprite *tempSprite = (CCSprite *) b->GetUserData(); 
    if(tempSprite.tag==2 ) 
    { 

     if(CGRectContainsPoint([tempSprite boundingBox], location)) 
     { 
      b->SetTransform(b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO), 0); //change position of the body 
      NSLog(@"touched"); 
     } 
    } 

} 

}

은 잊지 마세요 : 나는 뭔가를 찾고 생각합니다.

관련 문제