2011-09-23 5 views
1

화면에서 이미지가 날리는 앱을 개발 중입니다.XNA에서 이미지 드래그

  1. 보류 비행 이미지의에 탭에
  2. 드래그에게 사용자의 선택의 특정 위치에 이미지를 사용자가이를 유지 시켜서 : 나는 구현해야합니다.

답변

1

, 당신이 어디 있는지 제어 할 BoundingBox의 또는 사각형 객체가 필요합니다.

휴대 전화의 XNA 앱에서 텍스처 용으로 선언 된 개체가 두 개 있어야합니다.

Texture2D texture; 
BoundingBox bBox; 
Vector2 position; 
bool selected; 

그런 다음 이미지 콘텐츠를로드 한 후에 경계 상자를 이미지의 위치로 업데이트하십시오.

bBox.Min = new Vector3(position, 1.0f); 
bBox.Max = new Vector3(position.X + texture.Width, position.Y + texture.Height, 0f); 

그런 다음 또한 업데이트 방법, 당신은 그들을 통해 터치 스크린의 입력을 처리하는 터치 모음의 위치를 ​​얻기 위해 초기화 수집, 루프를 가지고 있고 당신의 BoundingBox의 교차하는지 확인해야한다.

foreach (Vector2 pos in touchPositions) 
{ 
    BoundingBox bb = new BoundingBox(); 
    bb.Min = new Vector3(pos, 1.0f); 
    bb.Max = new Vector3(pos, 0f); 

    if (bb.Intersects(bBox) 
    { 
     if (selected) 
     { 
      //do something 
     } 
     else 
     { 
      selected = true; 
     } 
    } 
} 

거기에서 개체를 선택했는지 여부를 확인할 수 있습니다. 그런 다음 제스처 이벤트를 사용하여 텍스처 객체로 수행 할 작업을 결정하면됩니다.

3

다음은 드래그를하는 또 다른 쉬운 방법입니다. Vector2 대신 Rectangle을 기준으로 이미지 (Texture2d)를 그립니다. 이미지 변수이

Texture2d image; 
Rectangle imageRect; 

과 같아야하는 것은 그리기() 메소드에서 "imageRect"에 대한 이미지를 그립니다.

spriteBatch.Draw(image,imageRect,Color.White); 

이제 Update() 메서드에서 이미지를 단일 터치 입력으로 처리합니다.

//Move your image with your logic 

TouchCollection touchLocations = TouchPanel.GetState(); 
foreach(TouchLocation touchLocation in touchLocations) 
{ 
    Rectangle touchRect = new Rectangle 
    (touchLocation.Position.X,touchLocation.Position.Y,10,10); 
    if(touchLocation.State == TouchLocationState.Moved 
    && imageRect.Intersects(touchRect)) 
    { 
    imageRect.X = touchRect.X; 
    imageRect.Y = touchRect.Y; 
    } 
//you can bring more beauty by bringing centre point 
//of imageRect instead of initial point by adding width 
//and height to X and Y respectively and divide it by 2