2013-04-06 53 views
1

我有一個應用程序,我在屏幕上拖動一個太空船避免小行星,並希望一旦它們的矩形相撞,船隻會返回到它的原始位置,但這不會發生。碰撞的精靈不會移動到新的位置

我不能確定什麼是錯的......

代碼:

//Drag Ship 


TouchCollection touchLocations = TouchPanel.GetState(); 

       foreach (TouchLocation touchLocation in touchLocations) 
       { 
        Rectangle touchRect = new Rectangle 
        ((int)touchLocation.Position.X, (int)touchLocation.Position.Y, shipRect.Width, shipRect.Height); 
        if (touchLocation.State == TouchLocationState.Pressed 
        && shipRect.Intersects(touchRect)) 
        { 
         shipPressed = true; 

        } 
        else if (touchLocation.State == TouchLocationState.Moved && shipPressed) 
        { 
         shipRect.X = touchRect.X - touchRect.Width/2; 
         shipRect.Y = touchRect.Y - touchRect.Height/2; 
        } 
        else if (touchLocation.State == TouchLocationState.Released) 
        { 
         shipPressed = false; 
        } 
        else if (lnBtnPlay.Tapped == true) 
        { 

        } 


       } 

代碼2:

 if (shipRect.Intersects(asteroid1Rect)) 
       { 
        shipPosition = new Vector2(10, 400); 
       } 

回答

1

shipPressed永遠不會被設置爲false,除非你真正釋放你手指。因此,儘管你可能正在與小行星相交,但這艘船將不斷迴歸你的觸覺。

總的來說,一定要記住在「死亡」時重置一些事情。碰撞時清除shipPressedfalse

如果即使您的手指未觸摸屏幕,船舶仍未返回,您的shipPosition未與您的shipRect同步。確保你始終保持這兩個同步。總是將它們更新到一起,或者更簡潔地說,只保留位置,並存儲寬度和高度;您可以通過屬性/方法調用輕鬆地從中輕鬆生成矩形。