Locating Sound Button MC with Actionscript?

Ok, I’ve managed to create a working On/Off Sound button (in a separate file) using this tutorial here.

However, when I’ve tried to adapt the same scripting to the music button in my interface (located within an MC on the main timeline), I can’t get it to work.

I’ve applied exactly the same code the new button, and got the sound playing alright, but the button doesn’t stop the sound or show the “off” state once clicked…It just dissapears, then shows button again and plays another instance of the sound over the top of the existing sound when clicked again. I’ve tried applying the “Tell-Target” command to locate the frame labels within the MC, but still no luck!

Code: Frame 1:

my_sound = new Sound();
my_sound.attachSound("music");
my_sound.start(0,1000);
gotoAndStop("play");

Original Code: Play Button (Frame 1).

on (release) {
_root.my_sound.start(0,1000);
_root.gotoAndStop("stop");
}

Adapted Code: Play Button (Frame 1):

on (release) {
    _root.my_sound.start(0, 1000);
    tellTarget ("_root.speaker") {
        gotoAndStop("stop");
    }
}

[COLOR=Orange]

[/COLOR]Can anyone please tell me what I’m doing wrong? Am I on the right track at least? :expressionless:

Cheers,
DC.

Hi DC.

It’s hard to tell what exactly is going on without seeing your code, but I do have a few pointers for you.

First of all, using the gotoAndStop action on an object makes that object go to that frame (and stop), but nothing else, so make sure you aren’t confusing that “stop” with the stopping of a sound. This means that in order to stop playback of the sound, your “stop” frame would need to have some other script of its own, something like a stopAllSounds() call or a my_sound.stop().

Second, tellTarget is just a deprecated way of using the dot operator on an instance, so you shouldn’t really ever use it. Instead of writing:


tellTarget ("_root.speaker") {
        gotoAndStop("stop");
}

use:

_root.speaker.gotoAndStop("stop");

– Which has the exact same functionality, but is written in the proper manner =]

Let me know if adding a stop call to your “stop” frame’s actionscript solves your problem, or if you want you can post your code here and I’ll take a look at it as well.

Thanks for your help Sean but I’d already figured it out before reading this post…after 2 long weeks I might add. Woohoo! :beer2:

And just thought I might add too. I’m gonna submit a tutorial on how to create a “Multi-State Music/Speaker Button” if I get enough time aswell. Thought I might save a few other “noobs” (like me) the headache and show them how it’s done. Stay tuned. :slight_smile:

Well, you could always just post your solution here and then mark it as resolved =)

Ok, here goes, but it’s a little bit complicated. If your new to this, go have a look at the original tutorial I followed here 1st.

Basically, I wanted 2 create a Speaker that not only stopped and started sound but played Music On/Music Off text and also played speaker waves reducing and increasing on clicks. It took me 2 LONG WEEKS to overcome all the glitches and road-blocks (and with NO help from these forums), but heres how I did it:

Pic 1: Music ON on Roll Off

Pic 2: Music ON on Release/Roll Over

Pic 2: Music OFF on Release/Roll Over

*Note: I left out 1 state from the picture above. The RollOff state for music OFF. That would obviously be a speaker with no Waves in opaque White (Alpha 50%) like the picture at the top. ***

Create 4 Layers:**

  1. Actions
  2. Buttons
  3. Invisible button
  4. Tweens (This will be an MC buttons reference on clicks to play animations.)

**
Create an MC and set it up with layers Similar to this: **

Ok, now basically what you see above is 2 sets of animations. “musicOff” which will play soundwaves reducing and fade in and out “MUSIC OFF” text…And “musicOn”, which of course will play soundwaves increasing and fade “MUSIC ON” in and out. I’ve also named the instance of my MC as “speakerTweens”.
**
Back to the main timeline…**

Basically, on each On/Off button in the speaker Hits layer I created 4 standard button states. Ie: Off, Over, Release and Hit. You can use whatever colours you want for these, it’s really up to you.

**The basic principle is:

**That the the first one represents Music ON (Full Waves) and the second one Music OFF (No Waves). Ive also created an Invisible button (3 Frames long) to allow the animation of Waves Increasing to play, because keep in mind the ON button is speaker with FULL waves, therefore that wouldn’t allow us to see the “waves increasing” animation if it jumped to this button immediately after previous button was clicked…Understand?

**ActionScript:

Actions Layer:

Frame 2 (See above):


Note: Placing this on 1st (or 2nd) frame will allow music to play automatically on site load. Rename “your_sound_name” with whatever name you’ve given your track or loop in the “linkage” properties in Library. If you don’t know how to link music, search the tutorials on this site.*

my_sound = new Sound();
my_sound.attachSound("your_sound_name");
my_sound.start(0, 1000);

**At the end of each Button on Actions layer (See above):

**

stop();

**On Invisible Button:

**

on (release) {
    gotoAndPlay("off");
}

On Button 1 (Just after frame labelled “on”):

on (release) {
    gotoAndPlay("off");
}
on (release) {
    tellTarget ("_root.speakerTweens") {
        gotoAndPlay("musicOff");
    }
}
on (release) {
    stopAllSounds();
}

Note: “speakerTweens” is the instance name I’ve given to my animations MC.

***Code on Button 2 **(Frame labelled “off”):

on (release) {
    gotoAndPlay("on");
}
on (release) {
    tellTarget ("_root.speakerTweens") {
        gotoAndPlay("musicOn");
    }
}
on (release) {
    _root.my_sound.stop();
}

…And voila. That’s pretty much it. An animated speaker button!

I’ll create a proper tutorial on this when I get some spare time.

Any questions until then , just ask me. I’ll be happy to help.

Cheers,

Dallas. :slight_smile:

Whoops! I resized those screenshots, but it looks like they went back to normal size. Sorry guys!