简体   繁体   English

如何在游戏中实现拖放?

[英]How do I implement drag and drop in my game?

I have been looking all over SOF and online tutorials, but for some reason I still can't get it to work. 我一直在寻找SOF和在线教程,但是由于某些原因,我仍然无法使它正常工作。 I want to implement a drag and drop functionality in my game. 我想在游戏中实现拖放功能。 Here is the activity: 这是活动: 游戏

I want to be able to drag and drop the 4 shapes in the bottom. 我希望能够将4个形状拖放到底部。 If the correct shape fits, I want the shape with the "?" 如果正确的形状合适,我想要带有“?”的形状 to change into the correct shape. 更改为正确的形状。 Can someone show me how I can do this? 有人可以告诉我我该怎么做吗?

Here is my code: 这是我的代码:

public class SecondActivity extends AppCompatActivity {

int n;
ImageView shape1, shape2, shape3, shape4, guessShape;
ImageButton exit;
private android.widget.RelativeLayout.LayoutParams layoutParams;
Random rand = new Random();
ImageView[] shapes = new ImageView[4];


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    //declare each imageview
    shape1 = (ImageView) findViewById(R.id.shape1);
    shape2 = (ImageView) findViewById(R.id.shape2);
    shape3 = (ImageView) findViewById(R.id.shape3);
    shape4 = (ImageView) findViewById(R.id.shape4);
    guessShape = (ImageView) findViewById(R.id.guessShape);
    exit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            System.exit(0);
        }
    });

    //add each imageView to the shapes[] array
    shapes[0] = shape1;
    shapes[1] = shape2;
    shapes[2] = shape3;
    shapes[3] = shape4;

    //store all the shapes in an array
    int[] images = new int[]{R.drawable.img_0, R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
            R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9, R.drawable.img_10,
            R.drawable.img_11, R.drawable.img_12, R.drawable.img_13, R.drawable.img_14, R.drawable.img_15, R.drawable.img_16,
            R.drawable.img_17};

    //store all the guessShapes in an array
    int[] outlines = new int[]{R.drawable.outline_0, R.drawable.outline_1, R.drawable.outline_2,
            R.drawable.outline_3, R.drawable.outline_4, R.drawable.outline_5, R.drawable.outline_6,
            R.drawable.outline_7, R.drawable.outline_8, R.drawable.outline_9, R.drawable.outline_10,
            R.drawable.outline_11, R.drawable.outline_12, R.drawable.outline_13, R.drawable.outline_14,
            R.drawable.outline_15, R.drawable.outline_16, R.drawable.outline_17};

    //generate 4 random images from the array's and ensure that they don't match each other
    ArrayList<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < 18; i++) {
        list.add(new Integer(i));
    }
    Collections.shuffle(list);
    int whichImg = (int) Math.round((Math.random() * 4));
    int img1 = list.get(0);
    int img2 = list.get(1);
    int img3 = list.get(2);
    int img4 = list.get(3);

    if (whichImg == 1) {
        whichImg = img1;
    } else if (whichImg == 2) {
        whichImg = img2;
    } else if (whichImg == 3) {
        whichImg = img3;
    } else {
        whichImg = img4;
    }

    int outlineID = outlines[whichImg];

    //set the shape in each imageview
    guessShape.setBackgroundResource(outlineID);
    shape1.setBackgroundResource(images[img1]);
    shape2.setBackgroundResource(images[img2]);
    shape3.setBackgroundResource(images[img3]);
    shape4.setBackgroundResource(images[img4]);

    //ensures that 1/4 shape has the guess shape correspondence
    final Object currentBackground = guessShape.getBackground().getConstantState();

    //for loop to have the guess shape and 1/4 shapes to match
    for (int i = 0; i < 18; i++) {
        if (currentBackground.equals(getResourceID("outline_" + i, "drawable", getApplicationContext()))) {
            int random = new Random().nextInt(shapes.length);
            shapes[random].setBackgroundResource(getResourceID("img_" + i, "drawable", getApplicationContext()));
        }

        //set tags for each view
        guessShape.setTag("gShape");
        shape1.setTag("S_1");
        shape2.setTag("S_2");
        shape3.setTag("S_3");
        shape4.setTag("S_4");

    }
}


//method to get the ID of an image in drawable folder
protected final static int getResourceID(final String resName, final String resType, final Context ctx)
{
    final int ResourceID =
            ctx.getResources().getIdentifier(resName, resType,
                    ctx.getApplicationInfo().packageName);
    if (ResourceID == 0)
    {
        throw new IllegalArgumentException
                (
                        "No resource string found with name " + resName
                );
    }
    else
    {
        return ResourceID;
    }
}

} }

Although you were not clear enough. 虽然您还不够清楚。

Try this, First make a class that implements onTouchListener 尝试一下,首先创建一个实现onTouchListener的类

    private final class MyTouchListener implements OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            } else {
            return false;
            }
        }
    }

Then define a drag listener 然后定义一个拖动监听器

class MyDragListener implements OnDragListener {
    Drawable enterShape = getResources().getDrawable(
            R.drawable.shape_droptarget);
    Drawable normalShape = getResources().getDrawable(R.drawable.shape);

    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
        // do nothing
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            v.setBackgroundDrawable(enterShape);
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            v.setBackgroundDrawable(normalShape);
            break;
        case DragEvent.ACTION_DROP:
            // Dropped, reassign View to ViewGroup
            View view = (View) event.getLocalState();
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            v.setBackgroundDrawable(normalShape);
            default:
            break;
        }
        return true;
    }
}

Now simply use these lines 现在只需使用这些行

findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());

and

findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());

Here are some tutorials that might help you 以下是一些可能对您有帮助的教程

Tutorialpoint 指导点

Link 2 连结2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM