Checks if the mouse is pressed 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.
var sprite = createSprite(200, 200); function draw() { background("white"); if (mousePressedOver(sprite)) { sprite.visible = false; } else { sprite.visible = true; } drawSprites(); }
Click the mouse over the green alien to earn points, click the mouse over the pink alien to lose points.
// Click the mouse over the green alien to earn points, click the 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 (mousePressedOver(alien) && which==1) count=count+1; if (mousePressedOver(alien) && which==2) count=count-1; }
mousePressedOver(sprite)
Name | Type | Required? | Description |
---|---|---|---|
sprite | Sprite | The name of the sprite you want to check if the mouse is over. |
Boolean true or false.
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