Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a card based game, in which user can select one or multiple cards from its deck and replace the cards with cards of other decks. I am using onTouchEvent to detect whether the user has selected any bitmap or not and replace that Bitmap with Bitmap of other deck. However OnTouchEvent only allows me to select one bitmap at a time. How can i select multiple bitmaps from my deck. Also i want to know if there is any better approach to swap bitmaps other than the one i used.

Following are my code :

Thread Class


What I have tried:

C#
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;
    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900