FORUMS: list search recent posts

AS3 Button Walkthrough - Help Please

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Adam Zepeda
AS3 Button Walkthrough - Help Please
on Apr 1, 2010 at 6:58:16 pm

Could someone help me out some if you have time. I wanted to get a little walkthrough on setting up my buttons on my navigation bar, but done correctly, meaning I would like all my as on one layer in the timeline. I have seen tutorials all over the web but none of them are really working for the as3 that I have done up to use. Im not planning on using any kind of button base, just the word itself.

Below is what I have for my code. Now I would just like to set the button up properly to function with this.

Thanks


stop();

var frameNum:Number;
var btnLabels:Array =
[
"HOME",
"ABOUT",
"SERVICES",
"PORTFOLIO",
"LOCATION",
];

for (var i:Number = 0; i < btnGroup.numChildren; i++)
{
var btn:MovieClip = MovieClip(btnGroup.getChildAt(i));
btn.id = i;
btn.buttonMode = true;
btn.mouseChildren = false;
btn.btnLabel.text = String(btnLabels[i]);
btn.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
}

function btnClick(e:MouseEvent):void
{
frameNum = e.target.id + 1;
gotoAndStop(frameNum)
}


Return to posts index

demetri tashie
Re: AS3 Button Walkthrough - Help Please
on Apr 3, 2010 at 8:55:37 pm

i came back to this post several times wanting to help, and each time became frustrated by the way it was presented, lacking some important information, so i gave up each time. please try to be as informative, yet succint, in your descriptions and requests as possible- otherwise it becomes real hard to figure out what someone is asking or wants to do.

there are 3 lines of code, which, unless you have left out some crucial information, tell me that your code will not work.

for (var i:Number = 0; i < btnGroup.numChildren; i++)

what is btnGroup.numChildren ? is there a btnGroup movieclip on the stage WITH children already? if so, then why have

var btn:MovieClip = MovieClip(btnGroup.getChildAt(i));

what are you trying to accomplish there?
and of course, if the btnGroup movieclip is already populated with 5 children, then how are they being labeled with text with this line

btn.btnLabel.text = String(btnLabels[i]); ?

unless i am missing the boat, i deduce that you have a movieclip called btnGroup which will act as the 'button navigation bar'. then, you have or need 5 buttons which will be labeled "HOME", "ABOUT" etc, which when clicked, will navigate to frames 1-5.

those 5 buttons all need a dynamic text field, right? IF those buttons(movieclips) are already on the stage with dynamic text fields inside of them, how are the text fields named? all as btnLabel ? those text fields would have to have been given unique instance names, and then accounted for, probably in a separate array.

if those 5 button/movieclips are NOT already in the btnGroup movieclip, then they need to be added as children to that movieclip.

do you see these issues?

i would suggest that you have your btnGroup movieclip symbol on the stage, and that in your library you have a movieclip symbol with the linkage name btn1 . this btn1 symbol will have a dynamic text field inside of it with the instance name "btnLabeText"

your btnLabelsArray were already in String form, so btn.btnLabel.text = String(btnLabels[i]); made no sense. also of course btn.btnLabel.text = String(btnLabels[i]); didn't make sense either.

this redone code should do what i believe you want, as long as your file is set up correctly to. i don't encourage frame based navigation, but if that is the stage you are at, then at least understand it. make sure that the code is on the first fame, and the btnGroup symbol carries over the five frames as in the attached screenshot
677_timeline2.jpg.zip

so now the for loop is truly relating to the btnLabels array, and will add a child to the btnGroup for each indice of that array, and will put the name into the text field -effectively labeling the button. you'll have to work out the correct positioning for your added children.
everthing else should make sense.

stop();

var frameNum:uint;
var btnLabels:Array = [ "HOME", "ABOUT", "SERVICES", "PORTFOLIO", "LOCATION"];
var btnLalbelLength:uint = btnLabels.length;

for (var i:uint = 0; i < btnLalbelLength; i++) {
var btn:btn1 = new btn1();
btnGroup.addChild(btn);
btn.x=50+(i*100);// use whatever positioning makes sense for you
btn.id = i;
btn.buttonMode = true;
btn.mouseChildren = false;
btn.btnLabeText.text = btnLabels[i];
btn.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
}

function btnClick(e:MouseEvent):void {
frameNum = e.target.id + 1;
gotoAndStop(frameNum);
}


i hope i understood your needs correctly, and if so, that this helps.


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]