Using pictures for buttons. Want to change there appearance when hovered over
MZ How to change a picture when the mouse is hovering over it?
Learning JavascriptThere are two parts to this:
- How to change the appearance (event or script call?)
If your choice of "changed appearance" is offered by event commands, e.g. Show Picture or Tint Picture, then I assume there's no problem. 😮💨
If you need a script, maybe specify exactly what effect you'd like. Note that you can look up various event commands in rmmz_objects.js, found in your project's js folder. For example, searching forTint Picturelocates this chunk of code:
You can then search forJavaScript:// Tint Picture Game_Interpreter.prototype.command234 = function(params) { $gameScreen.tintPicture(params[0], params[1], params[2]); if (params[3]) { this.wait(params[2]); } return true; };tintPictureto figure out what inputs it requires, etc...
In this case,JavaScript:Game_Screen.prototype.tintPicture = function(pictureId, tone, duration) { const picture = this.picture(pictureId); if (picture) { picture.tint(tone, duration); } };toneis an array of four values: red, green, blue, grey. This is just as an example, e.g. maybe you'd want to useshowPictureinstead, to swap out the image. Check out the "When" spoiler for script examples~ - When to change the appearance
Unless you want to run a parallel event and check the cursor coordinates every (few) frame(s), I think you'll need two things for this:
- An "on enter trigger", for when the cursor moves onto the picture.
- An "on leave trigger", for when the cursor moves off of the picture.
onMouseEnterandonMouseExitmethods. In case you're interested, I have a plugin called Cae_PictureTouch that lets you bind a script or common event to these using Plugin Commands, e.g.
- Highlight "this picture" on enter (6-frame fade time):
JavaScript:$gameScreen.tintPicture(pictureId, [64,64,64,0], 6); - Reset tint of "this picture" on leave (6-frame fade time):
JavaScript:$gameScreen.tintPicture(pictureId, null, 6);
JavaScript:(alias => { Sprite_Picture.prototype.onMouseEnter = function() { alias.apply(this, arguments); const hoverTone = [64, 64, 64, 0]; const duration = 6; // 0.1 seconds transition time this.picture()?.tint(hoverTone, duration); }; })(Sprite_Picture.prototype.onMouseEnter); (alias => { Sprite_Picture.prototype.onMouseExit = function() { alias.apply(this, arguments); const leaveTone = null; // no tone, i.e. reset const duration = 6; // 0.1 seconds transition time this.picture()?.tint(leaveTone, duration); }; })(Sprite_Picture.prototype.onMouseExit);
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
Original thread: https://forums.rpgmakerweb.com/threads/how-to-change-a-picture-when-the-mouse-is-hovering-over-it.128408/