Hey, here's an example of what I'd do with my current knowledge (it's more efficient than what you're doing).
Basically put all your buttons inside a MovieClip and then add the listeners to that clip and let the event bubble through the buttons, and use event.target to control the buttons:
Code:
myMenu.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler, false, 0, true);
myMenu.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler, false, 0, true);
myMenu.addEventListener(MouseEvent.CLICK, onClickHandler, false, 0, true);
myMenu.useHandCursor = true;
myMenu.buttonMode = true;
myMenu.menuGB.useHandCursor = false;
myMenu.menuGB.buttonMode = false;
function onClickHandler(event:MouseEvent):void
{
trace("clicked: " + event.target.name);
switch (event.target.name)
{
case "button1" : trace ("doing button1 stuff");
break;
case "button2" : trace ("doing button2 stuff");
break;
case "button3" : trace ("doing button3 stuff");
break;
//etc
}
}
function onRollOutHandler(event:MouseEvent):void
{
trace("out: " + event.target.name);
//use event for stuff that is the same on all MCs
//event.target.gotoAndPlay("rollOut");
}
function onRollOverHandler(event:MouseEvent):void
{
trace("over: " + event.target.name);
//use event for stuff that is the same on all MCs
//event.target.gotoAndPlay("rollOver");
}
ranoka's idea is the way to go, but the code is a little off. First, create an empty Movie Clip called myMenu_mc. Then take all of your buttons off the stage and put them in myMenu_mc. Put myMenu_mc on the stage and give it the instance name myMenu. Then put this code in:
PHP Code:
//The false, 0, true arguments aren't required here but if you often forget to //remove Event Listeners when you're done with them then you can add them in myMenu.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver); myMenu.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut); myMenu.addEventListener(MouseEvent.CLICK, onClick);
//useHandCursor is true by default so you only need to set buttonMode to true myMenu.buttonMode = true;
//In all of these functions, evt.target points to myMenu and its children //Since the buttons are nested in myMenu, they get included function onMouseOver(evt:MouseEvent):void { //This will make any button you mouse over semi-transparent evt.target.alpha = 0.5; }
function onMouseOut(evt:MouseEvent):void { //This will make the button solid again when you mouse out evt.target.alpha = 1; }
function onClick(evt:MouseEvent):void { switch (evt.target.name) { case "button1" : //Execute button1 code break; case "button2" : //Execute button2 code break; default : //This will execute when you click a button whose name isn't listed //in this switch statement trace("Check your code for errors"); break; } }
Most of the code is pretty similar to ranoka's - the major difference here is I'm using MOUSE_OVER instead of ROLL_OVER. ROLL_OVER works, but counts the entire container movie clip, so when you roll over individual buttons it doesn't do anything. MOUSE_OVER counts each individual nested button. Each has its own uses, depending on whether you want to deal only with the parent movie clip or with the parent clip and all of its children.
Here's an example I was playing around with yesterday that uses a set of movie clips instead of buttons:
Last edited by DoomOfTheLiving; 11-04-2008 at 07:18 AM.
I have a movie clip which has different symbols / icons on each key frame, called 'mc_symbols'. This mc is nested inside the btn movieclip which is duplicated to make btn1, btn2, btn3, etc...
I have a gotoAndStop code which makes each button's mc_symbols clip, go to a different frame with a different symbol on.
Problem is, with the nested symbols clips, they keep acting funny. When you click on them instead of another part of the button, for some reason, the symbols clip goes to frame 1, displaying the wrong symbol...
Is this something to do with the EventListeners for my buttons?
I would get rid of the buttons altogether - just place multiple instances of mc_symbols inside the myMenu movie clip. When you set myMenu.buttonMode to true, it treats all the nested movie clips as buttons as well. This way you can just click the movie clips themselves instead of nesting them inside buttons.
I don't want to be able to click these nested clips, I just want them to be a static graphic / icon. However, I understand that by setting the btn1, btn2, etc buttonMode to true, it sets those nested clips to buttons as well. Is there a way to keep the nested movie clips as movie clips. Is this what casting is?
I've tried using that casting method but can't seem to get it to work...
Thanks DoomOfTheLiving,
I quickly rushed the example before leaving for work. I only used ROLL_OVER because it was in the pointguard's original code. Would have changed it over if had more time. Thanks for expanding and explaining the code better too.
Thanks DoomOfTheLiving,
I quickly rushed the example before leaving for work. I only used ROLL_OVER because it was in the pointguard's original code. Would have changed it over if had more time. Thanks for expanding and explaining the code better too.
No problem, I was just studying this type of thing a few days ago so it's one of the few things in AS3 I have a pretty good grasp on so far.
Originally Posted by pointguard
I don't want to be able to click these nested clips, I just want them to be a static graphic / icon. However, I understand that by setting the btn1, btn2, etc buttonMode to true, it sets those nested clips to buttons as well. Is there a way to keep the nested movie clips as movie clips. Is this what casting is?
I've tried using that casting method but can't seem to get it to work...
So you have a button with a nested movie clip, and in each frame of the movie clip you store a different graphic. When you click anywhere on the button, you want the nested movie clip to move to the next frame, right? If that's the case, you can just get rid of the buttons altogether, create a movie clip that acts as a button, and add your graphics to that.
Rather than explain the whole process I'll just attach an example. I made two different movie clips, one that just cycles through 3 different symbols when clicked, and one that emulates a button with an over, up, and down state that cycles through the same 3 symbols when clicked.
Also, to answer your question, casting is when you convert an object from one type to another. Here's an example:
PHP Code:
var firstNum:String = "17";
var secondNum:String = "29";
trace(firstNum + secondNum); // 1729
trace(Number(firstNum) + Number(secondNum)); // 46
Since firstNum and secondNum are defined as Strings, trace(firstNum + secondNum) gives you 1729 ("17" + "29" = "1729"). Once you convert them from Strings to Numbers, they can be added together, and you get 46.
My movie clip goes to a certain frame, showing a certain graphic at the start, before anything is clicked...(see attached)
I ca't really see a way around this, other than having new movie clips in my library, for each button, and then just having a static graphic of each symbol / icon on a different layer inside those...
unless there is a way around it other than that?
Last edited by pointguard; 11-05-2008 at 08:37 AM.
I've played around with your example and managed to get it working!
Note that the fix is using event.currentTarget instead of event.target
Also, I changed CLICK to MOUSE_DOWN 'cos you can't use MOUSE_UP and CLICK together (they overlap).
I commented out the stopPropagations because they aren't needed, but might help in other situations.
If you trace the event.target for the click event you'll see that the icons are picking it up and that's why they're changing.
Here's the code:
Code:
// Set up buttons for menu...
import flash.display.MovieClip;
import flash.events.MouseEvent;
//set up buttons array...
var _buttons:Array = [btn1, btn2];
for each (var $button in _buttons) {
$button.buttonMode = true;
$button.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler, false, 0, true);
$button.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler, false, 0, true);
$button.addEventListener(MouseEvent.MOUSE_DOWN, onClickHandler, false, 0, true);
$button.addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler, false, 0, true);
}
//set up symbols...
btn1.mc_symbols.gotoAndStop(1);
btn2.mc_symbols.gotoAndStop(2);
//set up functions for mouse events...
function onRollOverHandler(myEvent:MouseEvent) {
//myEvent.stopPropagation();
myEvent.currentTarget.gotoAndPlay(11);
}
function onRollOutHandler(myEvent:MouseEvent) {
//myEvent.stopPropagation();
myEvent.currentTarget.gotoAndPlay(1);
}
function onClickHandler(myEvent:MouseEvent) {
//myEvent.stopPropagation();
myEvent.currentTarget.gotoAndPlay(21);
}
function onReleaseHandler(myEvent:MouseEvent) {
//myEvent.stopPropagation();
myEvent.currentTarget.gotoAndPlay(1);
}
stop();