A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Evaluating a string value into a variable name

  1. #1
    Junior Member
    Join Date
    Jun 2005
    Location
    Jewett City, CT
    Posts
    11

    Exclamation Evaluating a string value into a variable name

    I am new to AS 3.0, but have programmed extensively in AS 2.0. I have 3 checkboxes on my stage, each of which correspond to a specific boolean variable. I am trying to create a single eventListener function to handle them. In AS 2, I was able to take the name of the checkbox that was clicked and use eval to allow me set the appropriate boolean value. However, when I try to do this in AS 3, it tells me that eval is an undefined method. Is there a simple way to take the name of an object on the stage and get it's appropriate variable?

    Here's what I'm talking about:

    I have 3 checkboxes named count_mc, placement_mc, and spray_mc and 3 boolean variables called count, placement and spray. In the past, I could get the name of the movieclip that was clicked, strip off the _mc and place that string in an eval statement as follows:

    var btnClicked = event.target.name.substring(0, event.target.name.indexOf("_mc"));
    btnVar = eval(btnClicked);
    btnVar = !btnVar;

    I appreciate any help anyone can give me.


    Chris

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    var spray:Boolean = false;
    var count:Boolean = false;
    var placement:Boolean = false;
    
    count_mc.addEventListener(MouseEvent.CLICK, doClick, false, 0, true);
    placement_mc.addEventListener(MouseEvent.CLICK, doClick, false, 0, true);
    spray_mc.addEventListener(MouseEvent.CLICK, doClick, false, 0, true);
    
    function doClick($e:Event):void {
            var btnClicked:String = $e.target.name.substring(0,$e.target.name.indexOf("_mc"));
            this[btnClicked] = ! this[btnClicked];
            trace(this[btnClicked]);
    }

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