AS3 ClickToGoToAndStop function from main timeline targeted to frame within movieclip - actionscript-3

I'm creating a website in flash and I'm having trouble finding what code to use for timeline and movieclip navigation. Right now I have my main navigation buttons on the main timeline, and a separate animation that serves as my content container on a movieclip within the main timeline. I don't know how to make it so that when I click a button on the main timeline to have it navigate to a frame within the content movieclip. I've found many troubleshooters regarding how to control the main timeline from within a movieclip, which is the opposite of what I'm trying to do.
I also have MouseOut handlers on the buttons which direct to mouse out animations on the main timeline.
HomeBtn1.addEventListener(MouseEvent.MOUSE_OUT, fl_MouseOutHandler_1);
Num2Btn.addEventListener(MouseEvent.MOUSE_OUT, fl_MouseOutHandler_2);
ThrdBtn1.addEventListener(MouseEvent.MOUSE_OUT, fl_MouseOutHandler_3);
function fl_MouseOutHandler_1(event:MouseEvent):void
{
gotoAndPlay(2);
trace("Moused out");
}
function fl_MouseOutHandler_2(event:MouseEvent):void
{
gotoAndPlay(30);
trace("Moused out");
}
function fl_MouseOutHandler_3(event:MouseEvent):void
{
gotoAndPlay(56);
trace("Moused out");
}
I would like it if I could add a function like this:
function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
gotoAndStop(x);
}
But with a path targeted within the movieclip, I know it may sound like a simple question but I'm relatively new to flash.

To target the content movieclip:
1. Give it an instance name.
Select it on the stage and enter a name in the textfield on the Properties panel. Call it something like 'contentClip'.
2. Access it in your code using the instance name.
contentClip.gotoAndStop(30);
Will make the content clip go to frame 30.

Related

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.

Why do my buttons only work on the first frame of the main timeline in Flash AS3?

I'm trying to code the navigation of a Flash AS3 project in an actionscript file. In the project there are several frames with buttons to navigate through the project. The ones I have coded for the first frame work, but on any other frame they don't.
here is the code:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
public function Main()
{
trace("it's working");
btn_one.addEventListener(MouseEvent.MOUSE_UP,eventResponse1);
btn_two.addEventListener(MouseEvent.MOUSE_UP,eventResponse2);
btn_three.addEventListener(MouseEvent.MOUSE_UP,eventResponse3);
btn_four.addEventListener(MouseEvent.MOUSE_UP,eventResponse4);
btn_five.addEventListener(MouseEvent.MOUSE_UP,eventResponse5);
btn_six.addEventListener(MouseEvent.MOUSE_UP,eventResponse6);
}
function eventResponse1(evt:MouseEvent):void
{
gotoAndStop("game");
}
function eventResponse2(evt:MouseEvent):void
{
gotoAndStop("specimenroom");
}
function eventResponse3(evt:MouseEvent):void
{
gotoAndStop("how");
}
function eventResponse4(evt:MouseEvent):void
{
gotoAndStop("game");
}
function eventResponse5(evt:MouseEvent):void
{
gotoAndStop("feedback");
}
function eventResponse6(evt:MouseEvent):void
{
gotoAndStop("home");
}
}
}
In the code the first five buttons work, but the sixth doesn't. This button is not located on the first frame of the main timeline, it's on the second and third frame. It doesn't work at all. What code is needed to get them to work? Any help is much appreciated, thank you.
The button must be present on the frame that the AS3 code gets executed on.
For example, if your button is on frame 2 and your code that tries to add the listener is on frame 1, that will not work.
I assume you are using a Document class, so the same applies. Only what is present on the stage at the time that constructor code runs, is available for you to work with.
What you could do is have a method that adds listeners to the appropriate buttons when you go to that frame.
There are other solutions that would require a more radical change to your current design of using the timeline to change screens, so I suggested the one that would require minimum change.
Because you asked in comments another possible solution is to not navigate through your different screens via timeline frames, but instead do it via code and MovieClip symbols that contain your UI screens. You would just create the instances of the screens in code as needed and handle their handlers appropriately when doing so.
other solution is that make button accessible on first frame and change its alpha to zero so that its not visible. This why u will not have to change the code and just change the time line.

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 :)

Bring Timeline animation to top

I have a movie that adds a MovieClip to it's stage in the constructor, i Also have an animation on the timeline that plays on certain events. Everything is working, except the movie I need the movie on the timeline to be the top layer, it is on the bottom currently.
public Class BallCollision extends MovieClip{
public function BallCollision(){
mcBall = new MovieClip();
stage.addChild(mcBall);
//Adds stuff to the movie clip
}
}
You can do one of the following:
Create a container on the timeline called 'container' and then add mcBall to that instead. This container will be on a layer underneath the existing animation.
Place all of the existing animation into a MovieClip and give it an instance name like animation. Whenever you add something to the stage, also use stage.addChild(animation) to bring it back to the top.
Obviously option 1 is preferable, but I've offered option 2 for the sake of free knowledge.

AS3: change handcursor graphic on buttons

I would like to replace the little handcursor that appears when hovering over movieclips with buttonMode=true by a custom graphic.
Is there any way to do this globally for my whole application or do I have to go to every single button, add MOUSE_OVER, MOUSE_OUT and MOUSE_MOVE handlers which will hide and show the mouse cursor as well place the handgraphics over it and position it correctly?
seems tedious...
There seems to be a way to change the native cursor; http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/ui/Mouse.html#registerCursor()
But you would have to target flash player 10.2+ only...
You could write a single set of MOUSE_OVER, MOUSE_MOVE and MOUSE_OUT handlers that do the replacement and positioning as you described but then add listeners to all your buttons that point to these handlers. Remember, the same handlers can be used for multiple objects.
Or alternatively, assuming your 'buttons' are MovieClips, you could write a custom ActionScript class that extends MovieClip, defines this default functionality and then have all your button instances use this class as a base. See below:
public class ButtonMovieClip extends MovieClip {
public function ButtonMovieClip() {
addEventListener(MouseEvent.MOUSE_OVER, buttonOver);
addEventListener(MouseEvent.MOUSE_OUT, buttonOut);
addEventListener(MouseEvent.MOUSE_MOVE, buttonMove);
}
public function buttonOver(event:MouseEvent) {
//Hide mouse and add replacement graphic
}
public function buttonOut(event:MouseEvent) {
//Show mouse and remove graphic
}
public function buttonMove(event:MouseEvent) {
//Position graphic where mouse should be
}
}
The key then is to ensure all your buttons are created from this class (either by code or from within the Flash IDE).