Android游戏:将一个图像一次拖动到一组图像的屏幕中

Bin*_*noy 3 android xamarin cocossharp

我的屏幕底部有5张图像.我的游戏的目的是拖动这些图像并在特定条件下连接它们.(排序拼图)我使用下面的代码

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;
     }
}
Run Code Online (Sandbox Code Playgroud)

此代码将所有图像一次移动到触摸的坐标.我的要求是一次拖动一个图像不同于一次.在我看来,Xamarin和Github中给出的Cocossharp API和教程没那么有用.有没有一种方法可以在一个触摸实例上拖动一个图像?帮助赞赏

jay*_*ers 5

下面是一个创建两个精灵的示例,您可以单独拖动它们.

设计:

  • 您需要检测正在触摸的精灵,然后才移动该精灵.
  • 保存OnTouchesBegan中触摸的精灵
  • 在OnTouchesMoved中移动当前触摸的精灵

备注:

  • 对于注册事件的每个精灵,都会调用一次OnTouchesBegan .因此,如果20个精灵添加了监听器,则OnTouchesBegan事件被调用20次(除非被吞下,见下文)
  • 以编程方式确定触摸位置是否在调用OnTouchesBegan的精灵的边界框内.然而,"吞咽"触摸将停止任何剩余的排队呼叫.吞下去,就回归真.
  • 一旦找到感兴趣的精灵,就会返回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
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)