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

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.

Related

Which implementation of InteractiveObject that is lightweight should I use to keep track of mousefocus in flex?

I have a spark List control that shows a side-list on ListEvent.ITEM_ROLL_OVER event. Basicaly it shows the contents of the item you are hovering. I would be also using ListEvent.ITEM_ROLL_OUT to hide it again but I want the user to be able to click on it (the sidelist) too.
So, i use MouseEvent.ROLL_OUT and check the event.relatedObject's id to see where the cursor went. I added an invisible Rectangle overlaying the sidelist that's a bit larger so it gains focus 1st. But it doesn't.
Googling around, I think that the Spark Rectangle cant gain focus as it doesn't implement InteractiveObject. What should I use?
My logic:
protected function listOUT(event:MouseEvent):void
{
var obj:Object = event.relatedObject;
if (obj.id != "focusRect")
{
sidelist.visible = false;
}
}
My struggle is to keep the side-list visible if the user moves the cursor from the main list to the sidelist.
Any other suggestions outside my approach are also welcome.
Update
(image link)
Red are the invisible Buttons
On roll_over select the item in the mainlist, display the sidelist
based on the selecteditem in the mainlist.
Hide the sidelist when there is no item selected in the main list.
Deselect items inthe mainlist if the sidelist loses focus.
Furthermore mx.core.UIComponent is the lightest component that dispatches focus events.

AS3 addChild() drawing on top of my movieClip on CLICK

So basically my issue is when I click on my movieClip I want it to spawn this animation I did over the protonCore. Essentially to show that when you click the protonCore you generate 1 proton. However the problem with this when spamming your CLICK, is that when it adds this child on every click, it draws on top of the movieClip and prevents hit detection while the addedChild "fuseSpark" is added to the stage. Is there a way to make it so when I add this child it doesn't affect the hitBox of the clickable movieClip?
function protonGenerator(e:MouseEvent):void
{
var fuseSpark:MovieClip = new MC_FX_fuse;
stage.addChild(fuseSpark);
fuseSpark.x = stage_protonCore.x;
fuseSpark.y = stage_protonCore.y;
}
An easy solution would be to disable the mouse for those children when you create them:
fuseSpark.mouseEnabled = false;
That is, ofcourse, only if you don't care if the user can click those elements.

ActionScript 3 goto and play event not working

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);
}

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.