我有一个拖放游戏。用户应该选择正确的形状拖放到盒子上。我在此处遵循本教程当用户拖动错误的对象时,它不会显示错误答案。当他将它放到篮子对象上时,应该会显示警告。我试过这个:
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;
}
}
但只要我选择正确的对象,它就不会进入下一个问题。我在这里缺少什么?我真的需要帮助才能完成这场比赛。提前致谢。