This site requires JavaScript, please enable it in your browser!
Greenfoot back
Phiesel
Phiesel wrote ...

2014/8/20

Drag'n'drop border problem

Phiesel Phiesel

2014/8/20

#
I have objects that can be dragged around. Works fine. I have an area in the world where they can be dragged into so that things happen. Works fine (so far). When those objects get drag'n'dropped into the area, they get centered into the middle of the area. Works fine. When those objects don't hit the area nothing happens. Works fine. Problem is, the moment the objects aren't dragged inside enough and stick halfway in and halfway out an error pops up and everything stops. I wonder why. Either it is intersecting or it is not, no? What can I do to work around this?
public void checkVocabID()
    {
        Actor draggedWord = getOneIntersectingObject(TaskField.class);
        if (draggedWord != null && !isDragged){
            hitTaskField = true;
            World myWorld = getWorld();
            TaskWorld taskworld = (TaskWorld)myWorld;

            if(draggedWord.getClass() == TaskField.class)
            {  
                int zeigerX = this.getX();
                int zeigerY = this.getY();

                TaskField tf = 
                    (TaskField)taskworld.getObjectsAt(zeigerX, zeigerY, TaskField.class).get(0);
                this.setLocation(tf.getX(), tf.getY());

                if (this.getVocabID() == tf.getTaskFieldVocabID())
                {
                    this.setVocabStatusRight();
                }
                else {
                    this.setVocabStatusWrong();
                }
            }
            return;
        }
    }
(Error message is: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:638) at java.util.ArrayList.get(ArrayList.java:414) at Vocab.checkVocabID(Vocab.java:123) at Vocab.dragndrop(Vocab.java:103) at Vocab.act(Vocab.java:78)
danpost danpost

2014/8/20

#
Line 15 is trying to get the first element off a list that may not contain any elements. You need to ensure that the list returned using 'getObjectsAt' is not empty before trying to get the first element. I think if you use 'getOneObjectAtOffset(0, 0, TaskField.class)' in line 3 instead of 'getIntersectingObject', it may also fix your problem. Also, you do not need to check the class of the intersecting object as it must be of the class required. Line 3 will set 'draggedWord', if assigned an actor at all, to an actor of the TaskField class. Out of curiosity, what is the name of the class in which this code resides?
Phiesel Phiesel

2014/8/20

#
Changing line 3 'getIntersectingObject' into 'getOneObjectAtOffset(0, 0, TaskField.class) did the trick, thanks a lot. I also tried removing line 9. So far it seems to work just fine. ;-) The class in which this code resides is named 'vocab'.
danpost danpost

2014/8/20

#
Phiesel wrote...
The class in which this code resides is named 'vocab'.
Well, I guess I asked too quickly on this. While looking at your error trace, I should have been able to tell; though it is actually called 'Vocab' (with a capital 'V'). You will probably find that 'draggedWord' and 'tf' will contain the same object. You should be able to get away with changing all occurrences of 'draggedWord' to 'tf' and removing lines 11 through 15. Also, the 'return' statement at the end is totally unnecessary, as there is no code after it with which to not process and you are not inside a loop.
You need to login to post a reply.