Saturday, October 13, 2018

Select multiple bitmaps using OnTouchEvent

Leave a Comment

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.

Thread Class:

 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 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment