5.6.5 Coordinates, Answer in Comments (Codehs)

The prompt of the assignment is "Write a program that shows the X and Y coordinates of the mouse in a label on the top left of the screen. You should update the values of the coordinates whenever the mouse moves." The basic code I have laid out is:

var pos;

function start(){
    mouseMoveMethod(mousePos);
}

function mousePos(e){
    pos = new Text("((" + e.getX() + "," + e.getY() + ")");
    pos.setPosition(75, 75)
    add(pos);
}

It "works" but it just keeps adding text over top of the new text instead of updating the existing text. I know I'm missing something, but I just cannot figure out what to implement to make it work the way it needs to. I've been stuck for days :(

1 Answer

I got it!!! I just needed to think smarter LOL

var pos;

function start(){
    pos = new Text(" ");
    pos.setPosition(75, 75)
    add(pos);
    mouseMoveMethod(mousePos);
}

function mousePos(e){
    pos.setText("(" + e.getX() + "," + e.getY() + ")");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest