0
\$\begingroup\$

In "match three" types of games like Crazy Kitchen, when the user touches an image, he can drag his finger over multiple images and create a change in each image he touches in a single gesture. You might drag your finger across 5 images and each image highlights as you drag across it.

I want to create a similar affect in my game but I can only get the first image to change in a single drag or touch event. I have a GridLayout with each cell containing a custom LinearLayout with an OnTouchListener attached. I want to have the user drag across multiple cells and have each view the user drags across change its image to a "highlighted" image.

How do I get more than the first LinearLayout to be affected by the touch event?

Here's my onTouch code:

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        FloorTile singleTile = (FloorTile) view;
        singleTile.getParent().requestDisallowInterceptTouchEvent(true);
        switch(action) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TE, "MotionEvent = DOWN");
                //assign the new Arrow type to the FloorTile
               swapImage(singleTile);
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TE, "MotionEvent = MOVE");
                singleTile.swapImage();
                break;
        }
        return true;
    }

public void swapImage(FloorTile mTile) {
        mTile.setBackground(this.getResources().getDrawable(R.drawable.highlight));
//the actual game will pull a Drawable ID from an enum that matches up the original image with an //appropriate highlighted version.
    }
\$\endgroup\$
  • \$\begingroup\$ Your issue seems to come from the fact that 'onTouch' means 'screen not touched to screen touched', i.e. relevant to the screen and not to the grid cell. You might want to track the touch to the whole grid and transmit fake touch events to the grid cells as you drag over them. \$\endgroup\$ – Vaillancourt Mar 19 '15 at 18:12
  • \$\begingroup\$ @AlexandreVaillancourt - I'm not exactly sure how I would transmit fake touch events to the grid cells, though I like the idea. Are you suggesting that I have an OnTouchListener on the grid as well as the individual grid cells, then somehow dispatch touch events to the listeners on the grid cells? Could you suggest some code for sending those fake touch events? Thank you. \$\endgroup\$ – Chamatake-san Mar 19 '15 at 18:16
  • \$\begingroup\$ Yes, it is what I'm suggesting. No, unfortunately, I can't suggest code. \$\endgroup\$ – Vaillancourt Mar 19 '15 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.