0

我有一个拖放游戏。用户应该选择正确的形状拖放到盒子上。我在此处遵循本教程当用户拖动错误的对象时,它不会显示错误答案。当他将它放到篮子对象上时,应该会显示警告。我试过这个:

TextView objBasket, tx, timer;
int trial = 0;
TextView obj[] = new TextView[4];
int[] images = {
        R.drawable.stage4_object1, // Correct Answer a
        R.drawable.stage4_object2,
        R.drawable.stage4_object3,
        R.drawable.stage4_object4
};

List<TextView> iv = new ArrayList<TextView>();
String[] tagList = {"a","b","c","d"}; 


    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.stage4_1);

    // I created a custom countdown timer c/o Say
            counter = new MyCount(30000,1000);
            counter.start();

    initControls();

    getCorrectObject();

}

private void getCorrectObject() {
    // TODO Auto-generated method stub

    List<Integer> objects = new ArrayList<Integer>();
    for(int arr : images){
        objects.add(arr);
    }

    // Shuffle the collection
    Collections.shuffle(objects);

    iv.add((TextView) findViewById(R.id.txtStage4_object1));
    iv.add((TextView) findViewById(R.id.txtStage4_object2));
    iv.add((TextView) findViewById(R.id.txtStage4_object3));
    iv.add((TextView) findViewById(R.id.txtStage4_object4));

    Collections.shuffle(iv);

    iv.get(0).setBackgroundResource(images[0]); 
    iv.get(1).setBackgroundResource(images[1]); 
    iv.get(2).setBackgroundResource(images[2]); 
    iv.get(3).setBackgroundResource(images[3]); 

    iv.get(0).setOnTouchListener(new ChoiceTouchListener()); 
    iv.get(1).setOnTouchListener(new ChoiceTouchListener()); 
    iv.get(2).setOnTouchListener(new ChoiceTouchListener());
    iv.get(3).setOnTouchListener(new ChoiceTouchListener());

    for (int i = 0; i < tagList.length; i++) { 
        iv.get(i).setTag(tagList[i]); 
    } 

}

    @SuppressLint("NewApi")
private class ChoiceTouchListener implements OnTouchListener {

    @SuppressLint("NewApi")
    @Override
    public boolean onTouch(View v, MotionEvent motionEvent) {
        // TODO Auto-generated method stub

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            //setup drag
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);

            //start dragging the item touched
            v.startDrag(data, shadowBuilder, v, 0);
            return true;
        }
        else {
            return false;
        }

    }
}

@SuppressLint("NewApi")
private class ChoiceDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        // TODO Auto-generated method stub               
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            //no action necessary
            break;
        case DragEvent.ACTION_DROP:
            //handle the dragged view being dropped over a drop view
            View view = (View) event.getLocalState();

            //stop displaying the view where it was before it was dragged
            view.setVisibility(View.INVISIBLE);

            //view dragged item is being dropped on
            TextView dropTarget = (TextView) v;

            //view being dragged and dropped
            TextView dropped = (TextView) view;

            for (int i = 0; i < iv.size(); i++) { 
                final int k = i; 

                    String tmp = "a"; 

                    if (tmp.equals(iv.get(k).getTag())) {   
                        Log.i("result","CORRECT ANSWER: "+ tmp); 
                        goToNextQuestion();
                    } else { 
                        Log.i("result","WRONG ANSWER: "+ iv.get(k).getTag());
                        trial++;
                        guessedWrong();
                        playWrongSound();
                    } 

            }


            break;
        case DragEvent.ACTION_DRAG_ENDED:
            //no action necessary
            break;
        default:
            break;
    }
        return true;
    }


}

但只要我选择正确的对象,它就不会进入下一个问题。我在这里缺少什么?我真的需要帮助才能完成这场比赛。提前致谢。

4

1 回答 1

0

我认为您的程序存在两个问题。

您创建了您的 ChoiceDragListener 类,但您没有将它注册到您希望拖动的任何项目中。您的 getCorrectObject 方法似乎缺少这点。

iv.get(0).setOnDragListener(new ChoiceDragListener ()); 
iv.get(1).setOnDragListener(new ChoiceDragListener ()); 
iv.get(2).setOnDragListener(new ChoiceDragListener ());
iv.get(3).setOnDragListener(new ChoiceDragListener ());

我看到的另一个问题是您的 switch 语句中的 ACTION_DROP 案例。首先,您获得了用户已删除的 TextView,但无论如何您都开始循环浏览所有文本视图。ACTION_DROP 案例中的代码应该更像:

View view = (View) event.getLocalState();
view.setVisibility(View.INVISIBLE);
TextView dropTarget = (TextView) v;
TextView dropped = (TextView) view;
String temp = "a";
if(temp.equals(view.getTag()){
   goToNextQuestion();
}else{
   guessedWrong()
}
于 2013-05-27T15:45:19.683 回答