使用OnTouchEvent选择多个位图

时间:2018-09-20 16:18:52

标签: android bitmap xamarin.android surfaceview ontouchevent

我正在开发一种基于纸牌的游戏,在该游戏中,用户可以从其卡组中选择一张或多张卡,然后将其替换为其他卡组的卡。我正在使用onTouchEvent检测用户是否选择了任何位图,并将该位图替换为其他卡座的位图。但是OnTouchEvent只允许我一次选择一个位图。如何从卡座中选择多个位图?我也想知道是否有比我使用的更好的方法来交换位图。

线程类:

 class MySurfaceViewThread:BaseThread
{
    private MySurfaceView mysurfaceview;
    private ISurfaceHolder myThreadSurfaceHolder;
    bool running;

    public MySurfaceViewThread(ISurfaceHolder paramSurfaceHolder, MySurfaceView paramSurfaceView)
    {
        mysurfaceview = paramSurfaceView;
        myThreadSurfaceHolder = paramSurfaceHolder;

    }
    public override void RunThread()
    {

        Canvas c;
        while (running)
        {
            c = null;
            try
            {
              c = myThreadSurfaceHolder.LockCanvas(null);
                 mysurfaceview.Render(c);
           //  mysurfaceview.PostInvalidate();

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
            finally
            {
                if (c != null)
                {
                    myThreadSurfaceHolder.UnlockCanvasAndPost(c);   
                }
             //   running = false;
            }

        }

    }
    public override void SetRunning(bool paramBoolean)
    {
        running = paramBoolean;
    }
}

OnTouchEvent:

 public override bool OnTouchEvent(MotionEvent e)
    {
        Log.Debug(Tag, "Inside" + System.Reflection.MethodBase.GetCurrentMethod().Name + "Method");
        float lasttouched_X, lasttouched_Y;
        Cards localcard;
        int index=-1;

        //switch (e.Action) {

        //    case MotionEventActions.Up: Log.Info(Tag, "Up Event is Triggered");
        //        break;
        //    case MotionEventActions.Move: Log.Info(Tag, "Move Event is Triggered");
        //        break;
        //    case MotionEventActions.Down: Log.Info(Tag, "Down Event is triggered");

        //        break;

        //}
        MotionEventActions action = e.Action;
        if (e.Action == MotionEventActions.Down || e.Action== MotionEventActions.Move)
        {
            Log.Info(Tag, "Down Event is triggered");
            lasttouched_X = e.GetX();
            lasttouched_Y = e.GetY();
            if (TouchedCard == null)
            {
                index = CardTouched((int)lasttouched_X, (int)lasttouched_Y);
                TouchedCard = MainPlayer.getCard(index);
                cardindex = index;
            }
            else
            {
                if (TouchedCard != null && ((lasttouched_X >= DiscardDeck_CurrentX && lasttouched_X < (DiscardDeck_CurrentX + DiscardedDeck.getCard().Image.Width)) && (lasttouched_Y >= DiscardDeck_CurrentY && lasttouched_Y < (DiscardDeck_CurrentY + DiscardedDeck.getCard().Image.Width))))
                {
                    ReplacedCard = DiscardedDeck.getCard();
                }

                if (ReplacedCard != null)
                    SwapCards(ReplacedCard, MainPlayer, cardindex);
            }

        }
        return true;
    }

    private void SwapCards(Cards replacedCard, Deck mainPlayer, int index)
    {

        Cards temp = mainPlayer.getCard(index);
        mainPlayer.SetCurrentCard(replacedCard, index);
        DiscardedDeck.SetCurrentCard(temp, DiscardedDeck.CardsLeft()-1);
        TouchedCard = null;
        cardindex = -1;

    }

    private int CardTouched(int lasttouched_X, int lasttouched_Y)
    {
        int index = 0;
        Cards localcard = null;
        while (index < MainPlayer.CardsLeft())
        {
            localcard = MainPlayer.getCard(index);
            if (lasttouched_X >= localcard.current_X && lasttouched_X < (localcard.current_X + localcard.Image.Width) && (lasttouched_Y>=localcard.current_Y && lasttouched_Y<(localcard.current_Y+localcard.Image.Width)))
                return index;
            index++;
        }

        return -1;
    }

0 个答案:

没有答案