Mouse x-y__________________

One of my students is creating an app that needs to capture the x-y mouse position when the mouse is clicked. I thought there would be an event handler for this built into app lab, but I’m not having any luck finding this. Any advice how to do this? He is trying to make a basketball shot chart tracker that shows where shots were taken from…

Hi Sean,
I think what you are looking for is built into App Lab. This example app reports to the console the X and Y coordinates of the mouse pointer when “screen1” is clicked.


Does this help?
Mike

4 Likes

WOW! You’re a prince!

1 Like

This might be a dumb question, but how did you find this?

1 Like

@sean.boston

The documentation for event.x and event.y is located with the documentation for the onEvent block. Hover over the block and click ‘see examples’.

Andrea

2 Likes

I see. It really was there the whole time! Got it. Thanks.

I found this code example, but the variable block “event.x” doesn’t exist. Where could I find that?

You could be using game lab and not app lab, if so use this as an example:

var sprite = createSprite(200, 200);
sprite.setAnimation("sprite");
sprite.scale = 4;

function draw() {
  drawSprites(); // Draw
  if (mouseIsOver(sprite) ) {   // Mechanism
    console.log("x: "+ World.mouseX + " | y: " + World.mouseY);
  }
}

Or just go to my link: Game Lab - Code.org