A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: How to get the button name in a variable using onRollOver?

  1. #1
    Member
    Join Date
    Nov 2007
    Posts
    36

    How to get the button name in a variable using onRollOver?

    I want to capture the button name when someone rolls over the button - and then use it to activate a sound. Can someone help me out? This is the idea - but I'm not sure if AS can do what I want:

    Code:
    onRollOver=function(){
    	var buttonName = this._name;
    	var buttonName_sound = (this._name+"_sound");
    	playing=true;
    	var buttonName_sound:Sound = new Sound();
    	buttonName_sound.setVolume(100);
    	buttonName_sound.loadSound("_sounds/"+buttonName+".mp3",true) //streaming
    }
    
    buttonName.onRelease = function() {
    	buttonName._alpha = 50;
    }
    	
    buttonName.onRollOut = function() {
    	buttonName._alpha = 0;
    }
    I'll use the buttonName for everything.

    Thanks again!

    Ben
    Last edited by benflashkit; 02-10-2008 at 06:26 PM.

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    That should work fine. It is error prone through as it forces you to be careful how you name your buttons and mp3's.

    I prefer to use a multi-dimensional array (fancy name for list of lists) where I list each button name and it's associated sound plus any other associations you want to make at a later time. Then by simply assigning each button a "queue" number, I can just look up the array when something is clicked.

    Code:
    //first create a reference to the mp3 folder
    //it will make the code easier to re-use in other projects
    //since all you have to do is change it here.
    mp3Folder = "sounds/";
    
    //then create your array of data
    //I like to write out all of the data rather than use an
    //incremental for loop to evaluate the names. It makes the array
    //more accessible to the rest of the movie.
    //just 1 big array full of 2 item arrays
    buttonData = [["button1_mc", "button1_sound.mp3"],["button2_mc", "button2_sound.mp3"],
    ["button3_mc", "button3_sound.mp3"],["button4_mc", "button4_sound.mp3"]];
    
    //now create a function that gives each button a queue number so
    //it can later pass it to the function that loads sounds when clicked.
    //the clips mentioned in the array must exist before this function runs
    function = setupButtons(){
    for(i = 0; i< buttonData.length; i++){
    var tempBtn = this[buttonData[i][0]];//the button for this loop iteration
    tempBtn.queue = i;//assign its queue number
    tempBtn.onRelease = function(){
    //call the function that loads sounds
    //we'll call it playMySound(); and pass it the clicked buttons queue
    playMySound(this.queue);
    }
    }
    }
    //the play my sound function
    function = playMySound(queue){
    var tmpSnd = this["sound"+queue] = new Sound();
    tmpSnd.loadSound(mp3Folder+buttonData[queue][1]);
    tmpSnd.play();
    }
    
    //then just run this to get it working
    setupButtons();

  3. #3
    Member
    Join Date
    Nov 2007
    Posts
    36
    That's pretty slick - although I have so many sounds, I don't think I could keep track of them.

    The code I posted is attached to the main frame and no the button itself. And the code has no errors, but it doesn't work. That is, it doesn't fetch the instance name. When I put a trace on it, the trace doesn't display, so I assume that the buttonName is empty.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center