在Android中拖放游戏

时间:2013-05-27 15:17:13

标签: java android

我有一个拖放游戏。用户应选择正确的形状以拖放到框中。我按照本教程here当用户拖动了错误的对象时,它还没有显示错误的答案。当他将它放到篮子对象上时,会显示警告。我试过这个:

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;
    }


}

但是每当我选择正确的对象时,它都不会进入下一个问题。我在这里想念的是什么?我真的需要帮助来完成这个游戏。提前谢谢。

1 个答案:

答案 0 :(得分: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()
}