user11740974
user11740974

Reputation:

How to detect exact location of mouse click on sprite in scratch

I would like to detect the exact location of the mouse click within the 3x3 grid displayed on the screen. How can this be done in MIT scratch? Any suggestions?

Upvotes: 1

Views: 2131

Answers (3)

Aetinx
Aetinx

Reputation: 51

when greenFlag pressed
forever
  if <mouse down?> then
    set (lastMouseClickX) to (mouse x)
    set (lastMouseClickY) to (mouse y)
  end
  wait until <not<mouse down?>>
end```

Upvotes: 1

Mr PizzaGuy
Mr PizzaGuy

Reputation: 450

This is pretty simple if you want to involve 2 variables in this: All you really need to do is to set the mousex position to a variable and the mousey position to a variable after the sprite detects a click on it. Here is an example:https://i.sstatic.net/9LAvM.png

Upvotes: 0

CatPerson
CatPerson

Reputation: 157

There are two ways to do this.
You could create 9 sprites, hide, and use the When this sprite clicked event... ...but it would be a whole lot of unnecessary sprites.
Or you can do the following:
As @PullJosh said, you can use the mouse x and y blocks. Just do some math:
You know that the stage goes from X: -240 to 240, Y: -180 to 180.
Just put that into some code to detect ranges, below is a link to a project that is an example of this:
This project
(Note: This assumes the grid boxes are the same size.)

Upvotes: 2

Related Questions