ActionScript 3 goto and play event not working - actionscript-3

I have a navigation and inside the navigation movie clip, I have buttons and I put this code in my of my button frames
aboutbtn.addEventListener(MouseEvent.CLICK,goAbout);
function goAbout(e:MouseEvent){
this.gotoAndPlay('245');
}
But this didnt work and when I click my button it does nothing, is there something wrong with my code?

this.gotoAndPlay('245');
'245' is a string and gotoAndPlay interprets as a frame label. Remove the quotes, passing an integer, to go to the frame 245.
aboutbtn.addEventListener(MouseEvent.CLICK,goAbout);
function goAbout(e:MouseEvent){
this.gotoAndPlay(245);
}
If that still doesn't work, make sure this refers to the movieclip you want to change the frame. For example, if this code is in the document class, referring to the root instance, this will change the stage's frame.
If you want to change the button frame (only makes sense if it's a SimpleButton instance), change this to aboutbtn, for example.
If it's anything else, you'll to give us more context, it could be a load of other things (different stage? is there a frame 245? is the button mouse enabled? is there any invisible buttons on top of the object?).
Edit:
After clarification: if you want to change the frame of the object above nav, its parent, use:
aboutbtn.addEventListener(MouseEvent.CLICK,goAbout);
function goAbout(e:MouseEvent){
MovieClip(parent).gotoAndPlay(245);
}

Related

AS3 Having difficulty getting movieclips to stay on top over other clips

So I have 5 movieclips on screen that when clicked (DOWN state) expand. They all come to the top of the screen because of this code:
addChildAt(this.Step0btn,1);
addChildAt(this.Step1btn,1);
addChildAt(this.Step2btn,1);
addChildAt(this.Step3btn,1);
addChildAt(this.Step4btn,1);
My issue is that on each of the expanded movieclips is another movieclip that also expands, but when I click that it still displays underneath everything else. I thought I could just add another addChildAt command like:
addChildAt(this.Step0btn.Step0img,1);
But when I do that I get this error:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChildAt()
at SMARTTTraining_fla::MainTimeline/SMARTTTraining_fla::frame5()[SMARTTTraining_fla.MainTimeline::frame5:14]
And then the expanded movieclip still displays underneath everything. What could be wrong here?
Step0img is null when that line of code runs. Likely explaination could be that Step0img doesn't exist on the frame that Step0Btn is on.
For your layering issue (and your error I suppose too), this would be what you'd want to do:
this.addChild(Step0btn);
OR
this.setChildIndex(Step0btn,this.numChildren-1);
They do the same thing. This will bring to the foremost layer above the other step buttons, you don't want to change parent/child relationship by doing addChild(step0btn.step0img);
If you are using the same code that I provided in your other question, this is what the onPress method would look like now:
function onPress(event:MouseEvent):void
{
// toggle between frame 1 and 3 on button press
gotoAndStop(Step0btn.currentFrame == 3 ? 1 : 3); //this expands your button presumably
parent.addChild(this); //moves this (this button) to the foreground
}

Actionscript 3, how do I make a menu appear by clicking a button, on the same frame?

So I have a button called manpb. When I click manpb, I want a menu to appear. The menu is a picture, but I can convert it into an object if this helps.
The best I can do is: Make a second frame with the menu, and insert the code inside the man_pb function:
gotoAndStop(2);
My problem is that I want the menu to appear on the same frame; then the menu will have buttons of his own. Any idea what to type inside the function below?
manpb.addEventListener(MouseEvent.CLICK, man_pb);
function man_pb(event:MouseEvent):void{
}
A big thank you!
The most intuitive solution might be to have your menu inside a MovieClip on the same frame. The menu buttons can also be placed within that menu MovieClip.
Simply convert your menu picture to a MovieClip (right-click, convert to MovieClip). Make sure that you select the MovieClip, and in the Properties panel give it an Instance Name, like menuMC or anything you want.
There are then a couple ways that you could handle making the menu appear only when you click the button.
1- Set the MovieClip's opacity to 0 by default, and then include this in your button function:
menuMC.alpha = 1;
thereby changing the MovieClip to fully opaque.
2- Make the MovieClip comprised of two frames, one empty frame that has a stop(); action, and one frame that contains your menu image and buttons. Then the button code would be:
menuMC.gotoAndStop(2);
3- Load the MovieClip dynamically from your library. See this for more information. edit: This is the approach that #DodgerThud is referring too, and is the more advanced but more comprehensive approach.
Use your Menu as an Object and add it to the MovieClip you want.
var menu:Menu = new Menu();//This is your Menu Symbol in your Library, you need to create one before you can use it
var manpb;//you could do the same thing for button, so you only need one symbol that has uses different labels
manpb.addEventListener(MouseEvent.CLICK, man_pb);
function man_pb(event:MouseEvent):void{
if(contains(menu)){//checking if "menu" already exists
removeChild(menu);//remove it from the displaylist
}else{
addChild(menu);//add it to the displaylist
}
}
In the Listener function you check if your current MovieClip (this should NOT be Button) already has a child that is menu. If it does, it removes the menu, otherwise, it will add the menu.
Don't forget to export your Menu for ActionScript.

As3 (code snippets) change property of object on stage from within another object

I've been searching for the past few hours how to do this but with no luck. First of all I'm new to AS and not sure what to search for.
So here's what I have and what I want to do:The stage has only 1 frame and on the stage I have 1 button (b1), 1 movie clip that is not visible (area) and 1 visible movie clip that is an animation of 20 frames(ani). For the moment when I press button "b1" it will start the movie "ani" (movie is stopped initially) that will stop when it reaches frame 20. Now what I want is when it reaches the last frame to make movie clip "area" visible. Since I am inside "ani" and on frame 20, I cannot use directly area.visible = true; as I would get the error "Access of unidentified property area." What would be the way to access "area"'s properties from within the other object ?
Inside your "ani" MovieClip (at last frame) add the following sentence,
MovieClip(this.parent).area.visible = true;
Here, parent is your main timeline.
(Note: This approach is not recommended).
Instead, use External classes approach. e.g. use Loader class to load animated swf with COMPLETE event and with contentLoaderInfo get swf object and detect for last frame and make area MovieClip visible.
I'm not sure I completely understand everything you said, but I think what you want to do is something like this on your stage where both ani and area have scope :
ani.addEventListener(Event.ENTER_FRAME, frameCheck);
function frameCheck(e:Event):void
{
if (ani.currentFrame == ani.totalFrames)
{
ani.removeEventListener(Event.ENTER_FRAME, frameCheck);
area.visible = true;
}
}
That was just an example of how you could detect ani hitting the last frame and dealing with that appropriately.
You would need to add the event listener EACH TIME the button is pressed.

Flash Action Script 3: drag movie clip issue

I am new to as3. I create a rectangle and convert it to symbol. Then I go into the symbol and create two sliders. Also, I make the symbol draggable.
controlPanel_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStartF);
controlPanel_mc.addEventListener(MouseEvent.MOUSE_UP, dragEndF);
function dragStartF(e:MouseEvent){
e.currentTarget.startDrag();
}
function dragEndF(e:MouseEvent){
e.currentTarget.stopDrag();
}
It works fine, but I can't use the slider. If I drag the slider, the entire movie clip moved. How can I solve this problem? Cheers!
Without more info on the structure of your FLA-file I assume that controlPanel_mc is the container containing the rectangle[background] and the two sliders.
What it seems like you did in your code was to add event listener to the container, what that means is that "if a click occures anywhere on this object, do the following". Since that clip "owns" the 2 sliders, the sliders will not get any MOUSE_DOWN-events since the parent is always the one handling the event first.
You probably want to convert the background into a symbol and add the eventListeners to that object instead.
bg.addEventListener(MouseEvent.MOUSE_DOWN, onBgClick);
bg.addEventListener(MouseEvent.MOUSE_UP, onBgRelease);
function onBgClick(e:MouseEvent){
controlPanel_mc.startDrag();
}
function onBgRelease(e:MouseEvent){
controlPanel_mc.stopDrag();
}
That should solve your issue :)

Move MovieClip and everything it contains

I have a MovieClip called navbar and it has buttons over it. How do I connect the buttons to the MovieClip so that when the MovieClip is moved the buttons move with it? I have been able to make the navbar draggable but the buttons aren't dragged with it.
I have tried the following:
navbar.addChild(button1);
This just made the button disappear.
Your approach is correct: adding the buttons as children to the MovieClip will allow them all to be moved as one item.
The button disappearing could be any number of reasons, for instance x and y now relative to new parent (i.e. setting button y to 600 is now 600 pixels down from the navbar, not from the stage or old parent).
Try commenting out any properties you have set on the button and see if that resolves the issue, from there you can determine which property is causing the button to disappear.
So you should basically just have something like this:
var button1:Button = new Button();
navbar.addChild(button1);
If even with that minimal code doesn't result in the button displaying on the navbar, you'll need to post more code so that we can see where the problem is occurring.
you can calculate distance from Movieclip's X,Y to buttons and you can write a code like this:
var diff1:int = navbar.x - example_button1.x;
stage.addEventListener(Event.ENTER_FRAME, function(event:Event):void{
example_button1.x=navbar.x-diff1;
});
you can duplicate example_buttons and diff variables.
or you can startDrag() sametime with same event listener,
navbar.addEventListener(someEvent.some, function(event:someEvent):void{
MovieClip(root).navbar.startDrag();
MovieClip(root).example_button1.startDrag();
});
MovieClip(root) allows you to effect main stage. With this property you can effect an object from inside of navbar for example.
As you supposed, if you want the navbar and the buttons to act like a unique element you need to put buttons inside the navbar and not just over it.
The reason the button is disappearing is due to the fact that it has been put on the stage in Designer so, when you add it to navbar, you have it into two different display stacks, and that's not allowed.
You should put buttons inside the navbar in Designer, or export them for AS and then instance them dynamically, as follow:
var btn:Button1 = new Button1()
navbar.addChild(btn)
Class name Button1 is assigned in the MovieClip properties window of the Library, under Export for ActionScript.