dcsimg
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: button to send variable??

  1. #1
    Senior Member
    Join Date
    Dec 2010
    Posts
    111

    button to send variable??

    Hi all

    not sure what I am doing wrong..

    want a button to send a variable, but can't seem to get it working.

    In a simple test, I set up a dynamic text box to display the variable sent by different buttons.

    What wrong with my code here?

    -------------------AS3

    btn1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
    function mouseDownHandler1(event:MouseEvent):void {
    var idv:int=('1');
    }

    btn2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
    function mouseDownHandler2(event:MouseEvent):void {
    var idv:int=('2');
    }

    txt1.text = idv.toString();

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

    cheers

  2. #2
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    There are multiple things wrong.
    If you assign an integer value, you do that like this

    var idv:int = 1;

    Another thing is the scope of variables. If you declare a variable inside a function like you do, the variable only exists within that function.

  3. #3
    Senior Member
    Join Date
    Dec 2010
    Posts
    111
    can I have a function within a function?

  4. #4
    Senior Member
    Join Date
    Dec 2010
    Posts
    111
    how can I set a variable with a button so that it can be called at a later frame?

  5. #5
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Declare the variable outside of a function to start with then it's scope is Global then your basic idea would work

    for example
    var myvar;

    btn1.addEventListener(MouseEvent.CLICK,set1);
    btn2.addEventListener(MouseEvent.CLICK,set2);

    function set1(e:MouseEvent){
    myvar=1;
    txt1.text=myvar;
    }
    function set2(e:MouseEvent){
    myvar=2;
    txt1.text=myvar;
    }



    But before you proceed, This is poor coding.
    The real advantage of a function is to REUSE it.

    You can tell which button called the function from the currentTarget and act accordingly.
    var myvar;

    btn1.addEventListener(MouseEvent.CLICK,setMe);
    btn2.addEventListener(MouseEvent.CLICK,setMe);

    function setMe(e:MouseEvent){
    caller=e.currentTarget.name;
    switch(caller){
    case "btn1":
    txt1.text=1;
    break;
    case "btn2":
    txt1.text=2;
    break;
    }

    }


    The benefit really comes into play when you have a lot of things calling the function, otherwise you'd have to create a function for every button
    Last edited by blanius; 01-26-2011 at 11:38 PM.

  6. #6
    Senior Member
    Join Date
    Dec 2010
    Posts
    111
    isn't there a simpler method? why must I bother with php?

    I want to set a variable within the same flash file - just a button so that later on the timeline the variable can be called.

    Using Externalinterface.call to javascript it is very easy, why is it not easy from one frame to another in flash?

    using Externalinterface.call I just use

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

    btn10.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler24);
    function mouseDownHandler24(event:MouseEvent):void {
    var idx:int = MPclassical.playlist.selectedIndex;
    var lbl:String = MPclassical.playlist.selectedItem.label;
    var format:String = "Trk";
    var gnre:String = "Classical";
    var type:String = "media";
    ExternalInterface.call("MediaStop", idx, lbl, format, type, gnre);
    }

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

    and the variables idx, lbl, format, type and gnre are set.

    How can I do the same but instead of sending to javascript, just send to another frame in flash? how do I set up some kind of catch function?


    appreciated


  7. #7
    Senior Member
    Join Date
    Dec 2010
    Posts
    111
    aha! figured it out.

    You wrote 'php' by error.

    it is AS3 code


  8. #8
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    LOL yes sorry for the confusion it is AS3 but the PHP tags were intentional as it makes the actionscript look nice

    and I keep forgetting that we now have the AS tags which well, we used to have to use the PHP tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center