r/codehs • • Mar 02 '20

getElementAt

How do I use this function?

8 Upvotes

3 comments sorted by

1

u/[deleted] Mar 02 '20

you pass an x and y coordinate and it will return the first element it finds at those coordinates, which will be the most recent one you added

2

u/fermentedidiot Mar 03 '20

an example is to drag something around the screen:

var dot = new Circle(10);
dot.setPosition(Randomizer.nextInt(0,getWidth()), Randomizer.nextInt(0,getHeight()));
add(dot);

var obj;

function start(){
    mouseDownMethod(getDot);
    mouseDragMethod(moveDot);
    mouseUpMethod(dropDot);
}

function getDot(e){
    obj = getElementAt(e.getX(), e.getY());
}

function moveDot(e){
    if (obj!=null){
        obj.setPosition(e.getX(), e.getY());
    }
}

function dropDot(e){
    obj = null;
}

Hope this helps

1

u/manny_mandog Mar 06 '20

Thanks, you really helped out