A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: passing values

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    16

    passing values

    Hello,
    I tried to passing values. Example.
    I have Button_45 and Button_23. Now it flash loaded then it passing default value.
    Actionscript Code:
    var nice:String="value 1";
    // if i pressed Button_45 then
    var nice:String="value 45";
    // if i pressed Button_23 then
    var nice:String="value 23";

    // if i pressed some button it gives value else gives default value like on the top
    trace("Value whati choose is: " + nice);

    Please somebody help how can i do this?

    Best,

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That cannot be your actual code. It would give errors about redeclaring the nice variable.

    Post your actual code.

    You are probably redeclaring your variable inside your event handler, which means you are not changing the larger scoped variable with the same name.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    Quote Originally Posted by 5TonsOfFlax View Post
    That cannot be your actual code. It would give errors about redeclaring the nice variable.

    Post your actual code.

    You are probably redeclaring your variable inside your event handler, which means you are not changing the larger scoped variable with the same name.
    Thank u for the answer. I dont have a code i start with new flash project. How can i to this
    Actionscript Code:
    if (obj.myDProp=="a") {
        trace ("Valu A");
    } else {
        trace ("Error");
    }
    Now i want to change obj.MyDProp value with clicking button? and if i dont press the button then trace print Error

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If you have a variable and you want to change its value, then just re-assign it. Do not redeclare it.
    Code:
    var a:String = "hi";
    
    function someButtonHandler(evt:MouseEvent):void{
      a = "hello"; //Do not use "var" here if you want to set the a variable above.
    }

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    Quote Originally Posted by 5TonsOfFlax View Post
    If you have a variable and you want to change its value, then just re-assign it. Do not redeclare it.
    Code:
    var a:String = "hi";
    
    function someButtonHandler(evt:MouseEvent):void{
      a = "hello"; //Do not use "var" here if you want to set the a variable above.
    }
    Actionscript Code:
    b1_btn.addEventListener(MouseEvent.CLICK, btnPressed);
    var obj:String = "hi";

    function btnPressed(evt:MouseEvent):void
    {
    obj = "a";
    }

    if (obj=="a") {
        trace ("Value A");
    } else {
        trace ("Error");
    }
    No i tried this but if i pressed button it dont change nothing happend. Output shows only trace Error. If i click button output dont change trace Value A? Whats wrong?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Of course it shows "Error". The code that does the if and trace runs before you have a chance to click the button.

    When do you want that code to run?

  7. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    Quote Originally Posted by 5TonsOfFlax View Post
    Of course it shows "Error". The code that does the if and trace runs before you have a chance to click the button.

    When do you want that code to run?
    I need to start this every button click but i need to set a default value to. Like if i pressed button it change var obj.String value and delete previouse value?

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Was that a question? I don't know what you're trying to say.

    If you want to test the value of obj on every button click, do that in an event handler.
    Code:
    var obj:String = "hi";
    b1_btn.addEventListener(MouseEvent.CLICK, btnPressed);
    addEventListener(MouseEvent.CLICK, testObjValue);
    
    function btnPressed(evt:MouseEvent):void{
      obj = "a";
    }
    
    function testObjValue(e:MouseEvent):void{
      if (obj=="a") {
        trace ("Value A");
      } else {
        trace ("Error");
      }
    }

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    Quote Originally Posted by 5TonsOfFlax View Post
    Was that a question? I don't know what you're trying to say.

    If you want to test the value of obj on every button click, do that in an event handler.
    Code:
    var obj:String = "hi";
    b1_btn.addEventListener(MouseEvent.CLICK, btnPressed);
    addEventListener(MouseEvent.CLICK, testObjValue);
    
    function btnPressed(evt:MouseEvent):void{
      obj = "a";
    }
    
    function testObjValue(e:MouseEvent):void{
      if (obj=="a") {
        trace ("Value A");
      } else {
        trace ("Error");
      }
    }
    Sorry my english is bad yes. I tried to explain. shortly i need to delete previouse value and replace to new one with button click

    Actionscript Code:
    // Flash start
    var obj:String = "hi";
    trace ("Error:" + obj);
    // If button clicked delete previouse var obj:String="hi"; and start new one var obj:String="Value A";
    var obj:String = "value A";
    trace("Value: " + obj);
    if this possible? Thank for your time!

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You do not need to "delete" the old variable. Simply re-assigning it as we have done above will give it the new value.

  11. #11
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    Can i but it some another var? is there some ways? Thanks again for your time
    Actionscript Code:
    var obj:String = "hi";
    b1_btn.addEventListener(MouseEvent.CLICK, btnPressed);
    addEventListener(MouseEvent.CLICK, testObjValue);

    function btnPressed(evt:MouseEvent):void{
      obj = "a";
    }

    function testObjValue(e:MouseEvent):void{
      if (obj=="a") {
        trace ("Value A");
      } else {
        trace ("Error");
      }
    }
    var another:String="var"+obj;

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm sorry, but I still don't know what you're trying to say. Perhaps you could write it in your native language and use google translate.

    If you are asking whether you can assign the value of one variable to another, then yes, of course you can.
    Code:
    var another:String = obj;
    But be aware of when you do that assignment. In the code you just posted, the variable another will have the value "varhi". I don't think that's what you want.

  13. #13
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    Quote Originally Posted by 5TonsOfFlax View Post
    I'm sorry, but I still don't know what you're trying to say. Perhaps you could write it in your native language and use google translate.

    If you are asking whether you can assign the value of one variable to another, then yes, of course you can.
    Code:
    var another:String = obj;
    But be aware of when you do that assignment. In the code you just posted, the variable another will have the value "varhi". I don't think that's what you want.
    Yes i know no if i press button b1_btn the it have to be var another:String = "a"; ?

  14. #14
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Do you mean this?
    Code:
    function btnPressed(evt:MouseEvent):void{
      obj = another;
    }

  15. #15
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    Quote Originally Posted by niceguy7654 View Post
    Can i but it some another var? is there some ways? Thanks again for your time
    Actionscript Code:
    var obj:String = "hi";
    b1_btn.addEventListener(MouseEvent.CLICK, btnPressed);
    addEventListener(MouseEvent.CLICK, testObjValue);

    function btnPressed(evt:MouseEvent):void{
      obj = "a";
    }

    function testObjValue(e:MouseEvent):void{
      if (obj=="a") {
        trace ("Value A");
      } else {
        trace ("Error");
      }
    }
    var another:String="var"+obj;
    "obj" must change every button click and if button not clicked "obj" have to be "hi"

  16. #16
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    1) what must obj change to?
    2) when do you need to check for button click? You obviously cannot check in a listener for click on the button, because then it would always have been clicked.

  17. #17
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    Quote Originally Posted by 5TonsOfFlax View Post
    1) what must obj change to?
    2) when do you need to check for button click? You obviously cannot check in a listener for click on the button, because then it would always have been clicked.
    I trie to give
    Actionscript Code:
    private var xmlURL:String = "xml/main"+output_txt.text+".xml";
    wich open another xml default loads main1.xml and after i click button it open main2.xml? I think i cant make it with this? Sorry i want to learn but this do not work

  18. #18
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Okay. Let's see if I finally get what you're trying to do.
    Code:
    private var xmlURL:String = "xml/main"+output_txt.text+".xml";
    b1_btn.addEventListener(MouseEvent.CLICK, btnPressed);
    
    function btnPressed(evt:MouseEvent):void{
      xmlURL = "xml/main2.xml";
      loadXml(xmlURL);
    }
    
    function loadXml(someURL:String):void{
      //do your loading code here.
    }
    
    loadXml(xmlURL); //this is the initial default load.

  19. #19
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    THANK UUUUUU u are best. This is exactly what i want! thaank uuuuuuuuu again

  20. #20
    Junior Member
    Join Date
    Feb 2011
    Posts
    16
    Hello,
    I have one problem. The buttons are wrong place now. If i resize browser then button stays width and height. But i need to make then fluid. like page? Or can i but them into another Frame and click them there? Now i have to but them root timeline only then buttons work. If i but them another frame then button click does not to anything?

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