Search code examples
actionscript-3flashuser-interfaceaudiorollover

Play sound on rollover of all buttons in movie


I thought this would be a very basic thing to do but I've Googled myself in to a corner. I simply want a sound to play every time a button type element is hovered on, regardless of the button's position, rooting, parent, name, instance name, etc.

The sound file is in the library with the class/linkage "sroll" (Sound Rollover) and I use the following method to play all the other sounds:

MovieClip(root).audio_channel_c = MovieClip(root).snd8.play(0,1);

In my mind I'm looking for something along the lines of:

public button.eventlistener(MouseEvent.ROLL_OVER, function(){
    MovieClip(root).audio_channel_c = MovieClip(root).snd10.play(0,1);
});

I'm working in AS3


Solution

  • After more searching I found a rather crude way to do this. The listener picks up what's under the cursor at each pixel movement. If the pixel under the cursor is a SimpleButton of object class it determines if the mouse is still moving on a button it already entered. If it's a fresh enter it traces.

    The result is a trace every time the cursor enters a button, but not as you scroll over the button. It also works on every instance of every button throughout the entire stage.

    import flash.events.MouseEvent;
    import flash.display.SimpleButton;
    import flash.utils.getQualifiedClassName;
    
    function getClass(obj:Object):Class {
        return Class(getDefinitionByName(getQualifiedClassName(obj)));
    }
    
    var last_button_to_mouse = "";
    
    stage.addEventListener(MouseEvent.MOUSE_MOVE, handle_custom_event, true);
    function handle_custom_event(e:Event):void
    {
        if(getQualifiedClassName(e.target)!="flash.display::SimpleButton"){
            last_button_to_mouse = ""
        }
    
        if(getQualifiedClassName(e.target)=="flash.display::SimpleButton" && (e.target.name != last_button_to_mouse)){
            last_button_to_mouse = e.target.name;
    
            trace("Place your script here for when the mouse enters any button on the stage");
    
        }
    }