2015-10-16 61 views
3

我有5個圖像堆積在屏幕的底部。我的遊戲的目標是拖動這些圖像,並將它們連接在一定條件下(排序拼圖) 我用下面的代碼Android遊戲:從一組圖像一次拖拽一個圖像到屏幕

var touchListener = new CCEventListenerTouchAllAtOnce(); 
touchListener.OnTouchesEnded = OnTouchesEnded; 
touchListener.OnTouchesMoved = HandleTouchesMoved; 
AddEventListener (touchListener, this); 
void HandleTouchesMoved (List touches, CCEvent touchEvent) 
{ 
    foreach(var tap in touches) 
    { 
     var locationOnScreen = tap.Location; 
     alarmicSprite.PositionY = locationOnScreen.Y; 
     alarmicSprite.PositionX = locationOnScreen.X; 
     pressSwitchSprite.PositionY = locationOnScreen.Y; 
     pressSwitchSprite.PositionX = locationOnScreen.X; 
    } 
} 

該代碼同時將所有圖像到觸摸的座標。我的要求是一次只能拖動一個圖像,而不是一次拖動一次。我腦海中Xamarin和Github給出的Cocossharp API和教程並不是那麼有用。 有沒有一種方法可以在一個觸摸實例上拖動一個圖像? 幫助讚賞

+0

當你說 「堆疊」,是否意味着你有精神上的另一個? – jaybers

回答

5

這裏是一個例子,它創建兩個精靈並讓你單獨拖動它們。

設計

  • 您需要檢測哪個精靈被觸摸,然後只能移動精靈。
  • 拯救精靈被觸摸的OnTouchesBegan
  • 將當前的觸摸精靈在OnTouchesMoved

注意

  • 的OnTouchesBegan爲EVERY精靈,其登記爲被調用一次事件。因此,如果20個精靈添加偵聽器,則OnTouchesBegan事件被調用20次(除非被吞噬,見下)
  • 以編程方式確定觸摸位置是否在調用OnTouchesBegan的精靈的邊界框內。然而,「吞嚥」觸摸將停止任何剩餘的排隊等待呼叫。要吞嚥,只需返回true。
  • 一旦找到感興趣的精靈,您將返回true以「吞下」觸摸事件,並停止其他精靈回撥。這可以節省CPU並更快地執行您的OnTouchesMoved。在完全處理OnTouchesBegan之前,系統不會調用OnTouchesMoved。

    CCSprite currentSpriteTouched; 
    CCSprite Sprite1; 
    CCSprite Sprite2; 
    protected override void AddedToScene() 
    { 
        base.AddedToScene(); 
    
        // Use the bounds to layout the positioning of our drawable assets 
        CCRect bounds = VisibleBoundsWorldspace; 
    
        Sprite1 = new CCSprite("redball.png"); 
        Sprite2 = new CCSprite("blueball.png"); 
    
        Sprite1.Position = bounds.Center; 
        Sprite2.Position = bounds.Center; 
        AddChild(Sprite1); 
        AddChild(Sprite2); 
    
        // Register for touch events 
        var touchListener = new CCEventListenerTouchOneByOne(); 
        touchListener.IsSwallowTouches = true; 
        touchListener.OnTouchBegan = this.OnTouchesBegan; 
        touchListener.OnTouchMoved = this.OnTouchesMoved; 
    
        AddEventListener(touchListener, Sprite2); 
        AddEventListener(touchListener.Copy(), Sprite1); 
    
    } 
    
    
    void OnTouchesMoved(CCTouch touch, CCEvent touchEvent) 
    { 
        if (currentSpriteTouched != null) 
        { 
         currentSpriteTouched.Position = touch.Location; 
        } 
    } 
    
    bool OnTouchesBegan(CCTouch touch, CCEvent touchEvent) 
    { 
        // This is called once for each sprite 
        // To stop the remaining sprites from calling, 
        // "swallow" the touch by returning true 
        CCSprite caller = touchEvent.CurrentTarget as CCSprite; 
        currentSpriteTouched = null; 
        if (caller == Sprite1) 
        { 
         if (Sprite1.BoundingBoxTransformedToWorld.ContainsPoint(touch.Location)) 
         { 
          System.Diagnostics.Debug.WriteLine("Sprite 1 touched "); 
          currentSpriteTouched = Sprite1; 
          return true; // swallow  
         } 
         else 
         { 
          return false; // do not swallow and try the next caller 
         } 
    
        } 
        else if (caller == Sprite2) 
        { 
         if (Sprite2.BoundingBoxTransformedToWorld.ContainsPoint(touch.Location)) 
         { 
          currentSpriteTouched = Sprite2; 
          System.Diagnostics.Debug.WriteLine("Sprite 2 touched "); 
          return true; // swallow  
         } 
         else 
         { 
          return false; // do not swallow and try the next caller 
         } 
        } 
        else 
        { 
         // something else touched 
         System.Diagnostics.Debug.WriteLine("Something else was touched"); 
         return false; // Do not swallow 
        } 
    } 
    
0

我跟着這個鏈接TouchableSpriteTest並取得我需要的功能(從jaybers幫助)

示例代碼

protected override void AddedToScene() 
     { 
      -------------------------- 
      -------------------------- 
      // Register for touch events 
     var touchListener = new CCEventListenerTouchOneByOne(); 
     touchListener.IsSwallowTouches = true; 
     touchListener.OnTouchBegan = OnTouchBegan; 
     touchListener.OnTouchEnded = OnTouchesEnded;    
     touchListener.OnTouchMoved = HandleTouchesMoved; 
     batterySprite.AddEventListener (touchListener); 
     pressSwitchSprite.AddEventListener (touchListener.Copy()); 
     wireSprite3.AddEventListener (touchListener.Copy()); 
     lampSprite.AddEventListener (touchListener.Copy()); 
     wireSprite2.AddEventListener (touchListener.Copy()); 
     } 

     bool OnTouchBegan (CCTouch touch, CCEvent touchEvent) 
     {  
      var target = (CCSprite)touchEvent.CurrentTarget; 

      var locationInNode = touch.Location; 
      var s = target.ContentSize; 
      CCRect rect = target.BoundingBoxTransformedToWorld; 

      if (rect.ContainsPoint (locationInNode)) {    
       target.Opacity = 180; 
       return true; 
      } 
      return false; 
     } 

     void HandleTouchesMoved (CCTouch touch, CCEvent touchEvent) 
     {  

      var target = (CCSprite)touchEvent.CurrentTarget; 
      CCPoint pt = touch.PreviousLocation + touch.Delta; 
      target.Position = target.WorldToParentspace (pt); 
     } 
相關問題