mouseIsOver()

Category:Game Lab

Checks if the mouse is over the sprite specified.

Some interactive games use the mouse for the user to control the game. All sprites have a collider area that defines the active area to detect collisions with other sprites and mouse interactions. Use setCollider to change the default collider area, a rectangle, for a sprite.

Examples

var sprite = createSprite(200, 200);
function draw() {
  background("white");
  if (mouseIsOver(sprite)) {
    sprite.visible = false;
  } else {
    sprite.visible = true;
  }
  drawSprites();
}

Green is Good

Mouse over the green alien to earn points, mouse over the pink alien to lose points.

                            // Mouse over the green alien to earn points, mouse over the pink alien to lose points.
var alien = createSprite(randomNumber(0,400), randomNumber(0,400));
var which;
World.frameRate=1;
var count=0;
function draw() {
  which=randomNumber(1,2);
  alien.setAnimation("alien"+which);
  alien.x=randomNumber(0,400);
  alien.y=randomNumber(0,400);  
  background("white");
  drawSprites();
  text("score="+count, 0, 15);  
  if (mouseIsOver(alien) && which==1) count=count+1;
  if (mouseIsOver(alien) && which==2) count=count-1;
}
                        

Syntax

mouseIsOver(sprite)

Parameters

NameTypeRequired?Description
sprite Sprite The name of the sprite you want to check if the mouse is over.

Returns

Boolean true or false.

Tips

  • When testing your games that use keyboard or mouse input make sure you click in the display area before you run, otherwise the Workspace will record your keyboard and mouse actions.
  • To fine tune your mouse and sprite interactions use setCollider to change the shape and size of the collider area and set debug to true for the sprite.

Found a bug in the documentation? Let us know at documentation@code.org