Mouse Hover Events
CreatiCode last edited by info-creaticode
To allow the user to interact with objects in the scene, we can enable the user to “hover” the mouse pointer over any object without clicking on it.
To specify which objects in the scene can trigger the mouse hover event, you can specify a list of sprite names separated by commas, such as “Sprite1,Sprite2”. Note that you have to use commas as the separator. This way, every object created in any of these sprites will respond to mouse hovering. If the sprite list is empty, then the current sprite’s name will be used by default.
Since you can specify multiple sprites, you only need to run this block once, usually as part of the project initialization step.
The following 4 blocks are available for you to get more information about the hovering event, including the name of the object under the mouse pointer, and the exact point on that object that’s under the mouse pointer.
Note that if no object from the specified sprites is being hovered by the mouse, the “hovered object name” will become “none”.
This block allows you to take some sprites off the list, so objects created in these sprites will no longer respond to the mouse hovering event. The list of sprite names needs to be separated by commas, such as “Sprite1,Sprite2”.
Sometimes specifying sprite names may not be granular enough. For example, suppose a sprite creates 5 objects, and you only want 3 of these objects to respond to mouse hovering. In this case, you can use the following 2 blocks to disable or enable hovering for some objects by their names.
The list of object names needs to be separated by commas, such as “Box A,Box B”.
In this demo, we make a box larger when the mouse is hovering over it, and restore its size when the mouse moves away.
There are 2 sprites.
In “Sprite1”, we enable the user to hover over any object created in the “Boxes” sprite. Then we send a message to add some boxes.
In the “Boxes” sprite, when we receive the message, we first add a plane called “ground”, but disable mouse hovering for the ground object. Then we add 5 boxes at random positions. This way, only these 5 boxes can trigger mouse hovering events.
Next, we enter a forever loop to repeatedly check the name of the object being hovered.
When “hovered object name” is not “none”, it means the mouse is hovering over an object. We use a variable “hover object name” to store the name of the object being hovered. If that variable is not the same as the “hovered object name”, it means a different object is being hovered, so we need to restore the previously hovered object’s scale to 100%. Then we select the newly hovered object and scale it up to 150%. We also store that name in the variable “hover object name”.
When the “hovered object name” is “none”, we just need to restore the previously hovered object back to 100%.
Try it yourself here: