ActionScript 3 - several variables for event listener.

OK, I have a question (of course this is the reason why I'm here).  I put emphasis on PHP a lot lately and just had a customer who requires an MP3 play several songs so I had to whip on my rusty actionscript skills.  I have the complete player and it does not work as you wish.  The downside is that there are several pieces.  I use separate functions for each song and separate event listeners.  My main question is there a way to combine these functions into one and just use the event listener to pass another variable to functions?  Here is a sample of one of the code for Song1.  I would like to do so anywhere there is Song1 in the code, I can make a variable so I can put for example (Song2, Song3, Song4, etc..)  In this way, I can have one or two functions to manage all the songs instead of having to copy, paste and then search and replace.

Thanks for the help!

// Song 1
var Song1:Sound = new Sound();
pbSong1.source = Song1;
//pbSong1.visible = false;
var Song1Loaded:Boolean = false;

pbSong1.addEventListener(Event.COMPLETE, Song1LoadComplete);
btnSong1Play.addEventListener(MouseEvent.CLICK, Song1Play);
btnSong1Stop.addEventListener(MouseEvent.CLICK, Stop);

function Song1Play(event:MouseEvent) {
     if (Song1Loaded == true) {
          sndchnlMain.stop();
          sndchnlMain = Song1.play();
     } else {
          pbSong1.visible = true;
          Song1.load(new URLRequest("media/music/Song1.mp3"));
     }
}

function Song1LoadComplete(event:Event) {
     trace("Size of file: " + Song1.bytesTotal);
     Song1.close();
     Song1Loaded = true;
     if(sndchnlMain) {
          sndchnlMain.stop();
     }
     sndchnlMain = Song1.play();
     pbSong1.visible = false;
}

You can code

function SongPlay(event:MouseEvent) {var songNum:int = Number(event.currentTarget.name.split(btnSong).join("").split("Play").join(""));      if (SongLoaded) {  // use one booleanSongLoaded=false;           sndchn.stop();  // use one soundchannel
         sndchn = this["Song"+songNum].play();
     } else {
          pbSong1.visible = true;
          this["Song"+songNum].load(new URLRequest("media/music/Song"+soundNum+".mp3"));
     }
}

Tags: Adobe Animate

Similar Questions

  • How can I create an event listener for the change of variable shared or similar?

    Hello

    I have a 'big' 6 devices communicating measurement system via the TCP protocol. The system is designed to use REST (the representative State transfer) with JSON (JavaScript object notation).

    Now, I need to add labview program to this environment. I've implemented a solution to 'work' with the help of this forum. Thank you for this! Now, I need to do better (currently no timestamps, large delays, etc.).

    I currently have a web service deployed using http-get as input. My web service .vi is just passing the web entrance to shared variables that are then questioned in real measurement program. Reason is that when I put the measurement program directly to the web service, it does not (something about rights and dependencies). Measurement program includes one third of the owners (Instron) drivers. Also I would not direct access via the network to our mechanical testing device potentially dangerous.

    Now, I want to change the message from the web services .vi commensurate .vi. Currently I have a loop of 10ms to query for changes in the shared variable. Is there a better way to do this? I thought create a listener to the shared variable change events.

    In addition, if you have any ideas (preferably, working code example) how to make the interface between labview and TCP-JSON I would really appreciate it.

    BR,

    Juha

    Currently in LabVIEW, the only way to create an event listener or event NSV is to use the DSC toolkit.  Most of the time the DSC is a very expensive tool that simply encapsulates the functionality that is built into the motor of the PSP and OAS and which is accessible to anyone who can program in LabWindows/CVI.

    http://zone.NI.com/reference/en-XX/help/370051P-01/CVI/libref/cvicnvcreatesubscriber/

    If you want to collaborate, I would like to create a small library of LV that would create a base SV events using a vi LV reminder or possibly passing return user LV event which is how works the DSC toolkit.  I asked OR several times to explain how do this and they're very tight discreet about it.  I guess that they do not want to give users LV less reason to buy the DSC.

  • How to define the event listener AFTER_ACTIVATE for each document

    Well, I want to be able to connect an AFTER_ACTIVATE event listener to any document that is created or opened. Is anyway to do this?

    "update of certain variables"? You mean, like global variables in a persistent script?

    I suggest that you could much better be architecting your script differently.

    Maybe to have a global object that your script consults the active document, instead of using a global variable.

    Or put your data inside the document with. insertLabel().

    Global variables are generally a bad idea. In this case, it seems that you are trying to mimic the local scope by using event handlers, and this sounds a bit scary for me.

  • the click event listener smile ActionScript 3.0 works only once

    I am a newbie. I use actionscript 3.0 and I have a file .as separated with all my action script code. I have 2 buttons called myBtn1 and myBtn2 in my .fla including the base class file is two flash.display.SimpleButton.
    The event listener for myBtn1 (buttonClickHandler) always fire correctly. But the eventHandler for myBtn2 (textClickHandler) fires once and only if myBtn2 is the first button click. (I know that these event handlers and instructions switch sounds pretty ridiculous with a single element of each, but I removed the code).
    Why textClickHandler only seems to work once?
    Any help would be greatly appreciated!

    According to one of the Adobe Technical Notes sometimes the event mouse move is missed by the DisplayObject, thus, rather than attach the listener to the stage. Which works very well. So now instead of

    myBtn1.addEventListener(MouseEvent.CLICK,buttonClickHandler);
    myBtn2.addEventListener(MouseEvent.CLICK,buttonClickHandler);

    I have

    stage.addEventListener(MouseEvent.CLICK,buttonClickHandler);

    Read all about it:
    http://www.Adobe.com/cfusion/knowledgebase/index.cfm?id=69a2ae4b

  • event listener for when the Panel is open?

    is there an event listener for when a CEP Panel is open.

    I have a persistent Panel the onload works only when the Panel is opened for the first time. I want to keep persistent, but I want to know when the Panel is open again

    Maybe I've misunderstood something, because my English is bad. Unfortunately the listener for events at the opening of the Panel, I also have not found, I had to go through the back door.

  • How to listen for events that hit the application?

    How to register an event listener in a subcomponent to listen to events that spread at the Application level?

    For example: Comp1 raises an event that is spreading to the hand. How to register Comp2 listening to this event?
    Main.MXML (Application)
    -Mgr1
    -Ordi1
    -Mgr2
    -Comp2

    The code that would be placed in the component and the hand would be greatly appreciated.
    Thank you.

    Try this:

    . AddEventListener (Application.application as Application)...

    from the inside of any component.

    Tim

  • Photoshop CC 2014 Extension - CEP event reminder fires several times by event

    Try to do my extensions to watch a bunch of events and it turns out to be a real pain in the *.

    I hope that there is someone here who has a little more experience to play with them.

    I used the sample code from here (Page 43): http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/creativesuite/pdfs/CC14_Extension_S DK.pdf

    I modified the code just a little. I removed unnecessary things from her. I also added a counter, in an attempt to visualize the problem.

    Every time I pasted the code here, the global variable seems to reproduce. Eeeeh Yes...  the sample code can be found here.

    The problem is:

    I close a single document to raise the event, but for some reason the recall seems to run several times.

    Even better, it does not seem be consistent on this issue. Sometimes she repeats only twice and sometimes he repeats 160 times.

    It seems that whenever the extension Panel is reopened, it comes up with a new number.

    Currently, if I close a document, it seems to trigger the recall 15 times

    Since it's the screenshot, you will have to take my word for it. I closed only a single document, and the code has been 15 times.

    Screen Shot 2015-04-18 at 21.21.28.png

    I do something wrong by any chance?

    Edit:

    I think I just find out the reason and a way to recreate the issue.

    I tested the opportunity of expansion by closing and opening the Panel, which seems to have caused the problem.

    It seems every time I close and then open the Control Panel, IT... Adds a run more to the stack.

    -Update the chrome Panel does not seem to affect it.

    When I restart photoshop, it resets to zero. So after the first opening of the Panel, the l' evenement event triggers the callback only once.

    -------

    I forgot to mention that the events of the host application work fine without these issues.

    In fact, I tried to use documentAfterActivate before, but that I could find, there is no way kind of filter specific events within the host application events.
    For example, I can't specify something different happen when a new document is opened.

    As much a side note:

    documentAfterActivate has its own side effects due to the way photoshop works.

    It is triggered when you:

    • Create a new document
    • Open a new document
    • Switch to an open document

    This is the part "Switch to a document open" which makes this event listener to trigger when you close a document.

    Because when you close a document, photoshop switches to the previous document which in turn triggers the event listener when he shouldn't, I guess.

    Of course it does not raise the event when you close the last document since none document to fit right in there.

    .. but it's beyond the scope of the original question.

    I had this problem also with flash panels, but I noticed that this has happened only if the Tribunal was persistent, I solve it by cancelling the registration of events when the Panel was closed.

  • several structures of event in a VI

    I have two event structures in a single VI, which handles the events that freeze the user interface and the other for events that are not. This last group is all triggered acquisition tasks, where arm you data acquisition, but maybe can abandon it (or do other things, while waiting for the data to come. Of course, you need a stop button to be active and responsive. With hindsight, using two loops was perhaps a bad choice, but one that I did because it never occurred to me that these things would be only after the fire and should be put in a while loop to be kept active. No other language not as I know. Anyway, the question is how can I keep the two assets? I surround both in a single while loop? Can I give each their own while loop. Can I give up and merge the structures of two events into one. For clarity, I would like to separate them, but this is not essential. I ' LD would like to know what are my options.

    Rossu says:

    If they are sperate while loops will be they have both run. If they are in a loop the loop recycling if a structure is still waiting for an event?

    The answer to this is fundamental to the work of LabVIEW. A loop will not proceed to the next round until the end of everything inside. If you have two event structures in the same loop, two of them need to deal with an event (or a timeout) before the will of loop iterate.

    You can have several event facilities in the same VI.

    By 'freeze the user interface,' do you mean the 'Blocking façade...' box "in the case of dialogue? There is no problem to have some events that can I lock the Panel front and some that are not in the same structure of the event. I'm not sure that's what you mean by 'freeze' the UI, however.

    EDIT: and comment more. Generally, you should not put long tasks (such as data acquisition) within a structure of the event. Instead, the case of the event should pass these tasks to a separate loop (for example, by using a queue) so that the structure of the event can quickly return to listen for other events.

  • [noob] Help understand why the event listener continues even after the withdrawal.

    Hey again,

    I'll try to do my best to explain. I can get this to work properly, but I'd like to understand why what I did before was not working.

    I have a little trouble understanding why an event listener continues listening to even after that I delete it. I have a facility where a Parent MovieClip ("CampScene") is established and an event listener is added to the look for a function ("walkUpToCampfire") to see if a condition is true ("comingFromAx is true", that won't be true until later).

    If it is true, an animation plays, an event listener is added to see if the animation is made and if it's an event listener is added to listen for a click ("campScene.goToAx") to a new animation to play and other events. (It won't be true until later.)

    If this isn't true (which is the situation that I work with at the start), the event listener said listening for a click on "campScene.goToAx" is implemented immediately. The function following removes the event listener, so it is supposed to stop listening to a click and let an animation play throughout.

    The problem is a continuous click to be listened to and if you keep clicking on the reboot of animation every time, which is what I don't want to happen.

    --------------------------------------

    I'm able to resolve the problem by adding the line "campScene.removeEventListener (Event.ENTER_FRAME, walkUpToCampfire)"; or in mouseEnabled affecting false for "campScene.goToAx", but I try to understand what is happening.

    It is, even if I'm deleting the listener ("campScene.goToAx") in a function ("goToAxScene"), "campScene.addEventListener (Event.ENTER_FRAME, walkUpToCampfire)"; continues to wait for a mouse click, unless it is removed also?

    Again, I am trying to understand how it works.

    --------------------------------------

    This should be all ACEs. Of course, I would like to know if you need more information. (I can post the FLA / AS files if need be.)

    public void practice_adventure8() {}

    ...

    initial event listeners

    addEventListener (Event.ENTER_FRAME, CampScene);

    }

    public void CampScene(event:Event) {}

    remove the event listener
    removeEventListener (Event.ENTER_FRAME, CampScene);

    Add the first camp scene
    campScene.x = - 120;
    campScene.y = - 10;
    addChild (campScene);
    setChildIndex (campScene, 0);

    Add the event listener to see if guys works with camp fire or not
    campScene.addEventListener (Event.ENTER_FRAME, walkUpToCampfire);

    }

    IF RETURNING TO CAMPSCENE
    public void walkUpToCampfire(event:Event) {}

    If the guy is walking back to axe
    If (comingFromAx == true) {}

    event listeners
    campScene.guyAtCampScene.gotoAndPlay ("guyComingFromAx");
    campScene.addEventListener (Event.ENTER_FRAME, ifGuyDoneWalking);

    otherwise if initial creation
    } else {}

    remove the old event listener
    campScene.removeEventListener (Event.ENTER_FRAME, walkUpToCampfire);

    trace ("comingFromAx = false");
    event listeners
    campScene.addEventListener (Event.ENTER_FRAME, sceneHoverInfo);
    campScene.goToAx.addEventListener (MouseEvent.MOUSE_UP, goToAxScene);
    campScene.goToBridge.addEventListener (MouseEvent.MOUSE_UP, goToBridgeScene);
    }
    }

    IF RETURNING TO CAMPSCENE FACT
    public void ifGuyDoneWalking(event:Event) {}

    If the guy ended up in foot
    If (campScene.guyAtCampScene.currentLabel == "guyAtCampSceneNormal") {}

    reset the Boolean values of scene
    comingFromAx = false;

    Add event listeners
    campScene.addEventListener (Event.ENTER_FRAME, sceneHoverInfo);
    campScene.goToAx.addEventListener (MouseEvent.MOUSE_UP, goToAxScene);

    }
    }

    GET READY FOR THE NEW SCENE

    public void goToAxScene(event:MouseEvent) {}

    delete movement event listeners
    campScene.goToAx.removeEventListener (MouseEvent.MOUSE_UP, goToAxScene);


    stop all sounds

    SoundMixer.stopAll ();

    play the clip of guy walking to AxScene
    campScene.guyAtCampScene.gotoAndPlay ("guyGoingToAx");

    check the label to see if axScene must appear
    campScene.guyAtCampScene.addEventListener (Event.ENTER_FRAME, addScene);
    }

    Your best bet to solve why things happen is to make use of the trace function.  In this case, you may have a trace run every time this listener can addedand any time it is deleted.  In this way, you should be able to see if it was added in after he deleted.  If there are several places where it is added, then adjust your tracks to indicate which line is involved as well.

  • [noob] Possible to add an event listener in a table?

    Hi all

    I suspect, it might not be a way to do this, but I thought I would ask.

    I was wondering if there is anyway to add an event listener to a table ("array.addeventlistener (EVENT, cheetosFunction)');.

    I'm working on writing from a table that has several pushed DisplayObjects in, and I was hoping that instead of having to associate each DisplayObject instance with a listener event separately ("head.addEventListener [...], torso.addEventListener [...] legs.addEventListener [...]" ""), since the entire table would be these DisplayObjects, I could save time by adding a unique EventListener in the table, which has a single line of code instead of several.

    Any way to do this, or do something useful in the same way?

    use:

    for (var i: int = 0; i<>

    table [i] .addEventListener (MouseEvent.CLICK, cheetosFunction);

    }

  • Cannot remove the Image event listener

    I'm clearly missing something and would be grateful for the help.  I'm trying to run an event handler when a full picture of loading, then remove the Manager so that it does not once again the image needs to be reloaded later.

    The problem I'm having is that the event handler simple custom ' disappear, despite the call removeEventListener on the Image object.

    The following example illustrates the problem (the example do anything useful... it's just the shortest example to demonstrate the problem).  (To run the application, you will need to provide any file JPEG ol ' named 'myImage.jpg' in the directory 'src' to your project).

    What to expect to happen is:

    (1) at startup, the image loads and runs of loadComplete().

    2) loadComplete removes the event listener so that subsequent re-loads don't get re-triggers the Manager.  I want only the Manager to run once.

    3) 'loadComplete' should appear in the debug console.

    (4) a click on the button should display only 'change picture' in the debug console

    What I get instead is that Manager loadComplete is not removed, and whenever I click on the button, loadComplete runs when the image is re-load (i.e., each button click results in the showing in the debug console 'change picture' AND 'loadComplete' ).

    <? XML version = "1.0" encoding = "utf-8"? >
    " < = xmlns:mx mx:Application ' http://www.Adobe.com/2006/MXML "layout ="absolute"> "
    "<mx:Image width ="655"height ="181" source =" myImage.jpg" id ="MyImage" autoLoad ="true"="true"scaleContent complete =" loadComplete () " ="100"x ="100"/ >
    < mx:Button = "100" x = "341" label = "Button" click = "click (event)" / >
    < mx:Script >
    <! [CDATA]
    private void loadComplete(): void
    {
    trace ('loadComplete');
    myImage.removeEventListener ("complete", loadComplete);
    }
    private void click(evt:Event):void
    {
    trace ("change Image");
    myImage.load ("myImage.jpg");    Reload image even; This is just an example
    }
    []] >
    < / mx:Script >
    < / mx:Application >

    Hello

    You can only remove event listeners you added with the addEventListener() method in an ActionScript block. You cannot remove an event listener that has been defined in the MXML tag, even if it has been registered by using a call to the addEventListener () method that was made within an attribute of the tag.

    Check this page for reference.

    http://livedocs.Adobe.com/Flex/3/HTML/Help.HTML?content=events_05.html

    You can change the code a bit get it works

    
    
    private function myImage_creationCompleteHandler(event:FlexEvent):void
    {
         myImage.addEventListener("complete",myImage_completeHandler);
         myImage.load("myImage.jpg");
    }
    
    private function myImage_completeHandler(event:Event):void
    {
         myImage.removeEventListener("complete",myImage_completeHandler);
    }
    
  • How can I add an event listener

    I tried to add an event listener for this object:

    < mx:Button id = "calculateButton".
    label =""
    toolTip =""
    styleName = "enable".
    Click = "swapToResultsState ()" "
    rollOverEffect = "{glowButton}".
    = Red "-100" horizontalCenter = "0".
    tabIndex = "85" / >

    by using this code:

    calculateButton.addEventListener (KeyboardEvent.KEY_DOWN, swapToResultsState);

    who gave this error:

    Several markers to this line :-1120: access of undefined property calculateButton. -1120: access
    undefined property swapToResultsState.

    What I am doing wrong?

    Keyboard events spread so you do not need to forward.  Just can't do anything if keyCode is 9

  • Using the variable string quickly calling several variables

    OK, I'm not entirely sure how to explain it, but I will do my best...

    I have had several similar Boolean variables which are publicly declared at the beginning of my class.

    for example

    public var _ball0_mcPlaced:Boolean = false;
    public var _ball1_mcPlaced:Boolean = false;
    public var _ball2_mcPlaced:Boolean = false; 

    This will determine whether or not a particular ball was placed in a target area.

    Now I want to use this variable again in a function... However, I do not want to write in the variable for each ball in the service.

    Is there a faster way to access this variable by using a string inside the function varibale?  Each ball becomes a target of the event if clicked, so I could use something like...

    ("_" + event.target.name + "Placé") to create the dependent variable on which ball is selected?
    So, if ball0_mc is selected it returns the _ball0_mcPlaced variable?

    How can I do this?


    See you soon

    It would be written using the array notation in the form of...

    ["_" + event.target.name + "Placé"] = true;

  • How in fact blackberry event listener

    Hi, m havin a little doubt

    Cvent JDE listener how actually works?

    They keep listening in the background for the event

    OR

    When the event occurs, then the OS itself call all the event listener for this event?

    I think that he shud of same way java event listeners work:

    Follow the link

    http://www.JavaWorld.com/JavaWorld/javaqa/2000-08/01-QA-0804-events.html

  • Global keyboard event listener?

    Is it possible to create a global keyboard event listener that is unrelated to a window, button, or other user interface element?  The best I could get to add the event to the window listener.  This only works if the window is active.  If the user clicks outside the window and then the receiver for the keyboard does not work.  In addition, when the dialog box appears first of all, it won't work until after the user has made some type of interaction with the window.  I want is to have keyboard shortcuts for users that work regardless of the place where they clicked on the screen, even if the dialogue window is not 'active'.  Is this possible?

    Here is the code I use to the receiver of keyboard for the window.

    mywindow.addEventListener ("keydown", function (k) {keyListen(k)});
    function keyListen(k){if(k.keyName=="Z"){alert("Do stuff here");}}
    

    I don't think that you can create listeners of world events, as for the first appearance of the dialog box, I solved this problem in my script: scripts buliarca BCM_ExtendShortcuts UI by a button activates the event listener to this button. A problem with attach listeners to window, it's in CC2015 events can only be attached to controls and not windows. Setting for windows works in CS6...

Maybe you are looking for