1

I have variable names stored in an array, and I want to loop through array and set the visible property of that instance to false. However, I'm getting error;

Error #1056: Cannot create property visible on String.

Here is my code:

package  {
    import flash.events.TouchEvent;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class touch extends MovieClip
    {
        public function touch()
        {
            var menuitems:Array = new Array("menu_One", "menu_Two", "menu_Three", "menu_Three", "menu_Four", "menu_Five");//array with instance names
            for(var i:int=0;i<6;i++){
                var tempName = menuitems[i];
                bsF_txt.text = tempName;
                trace(tempName);
                tempName.visible = false;
                //menu_One.visible = false;
            }

         }
      }
}

Is. what I'm trying to do possible in AS3?

1
  • The String Object in As3 does not have a visible parameter. There is no .visible for String which is causing your error. What are you trying to accomplish? It would work if you had say movieclips instead of a string... I just noticed your comment that those strings are instance names so I added an answer below Commented Apr 20, 2012 at 17:10

2 Answers 2

2

First yes it is possible!

The problem is your looping through an array of strings, not variables or anything that references a DisplayObject (maybe a MovieClip in your case?)

Assuming those strings are either instance names of MovieClips that are on your stage or vars that are referencing them you could try something like this:

public function touch()
    {
        var menuitems:Array = new Array(menu_One, menu_Two, menu_Three, menu_Three, "menu_Four", menu_Five);//if this gives you an error please paste some more code because these are not instance names or vars
        for(var i:int=0; i<menuitems.length ;i++){ //you don't need to explicitly use 6 here you can check the menuitems arrays length 
            var tempName = menuitems[i]; //note, this is not needed
            bsF_txt.text = tempName.name; //I think you're looking for this?
            trace(tempName);
            tempName.visible = false;
            //menu_One.visible = false;
        }

     }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, thanks! I was using the 'tempName' to check if the menu items were actually being read, hence the trace a line later. Thanks this works fine, as well as the M. Laing's solution.
@user1005253 Cool. In this case you can just do all your actions on menuitems[i]
1

Try using the following code (I just noticed you said those are instance names...)

package  { 
import flash.events.TouchEvent; 
import flash.ui.Multitouch; 
import flash.ui.MultitouchInputMode; 
import flash.display.MovieClip; 
import flash.events.MouseEvent; 

public class touch extends MovieClip 
{ 
    public function touch() 
    { 
        var menuitems:Array = new Array("menu_One", "menu_Two", "menu_Three", "menu_Three", "menu_Four", "menu_Five");//array with instance names 
        for(var i:int=0;i<6;i++){ 
            var tempName = menuitems[i]; 
            bsF_txt.text = tempName; 
            trace(tempName); 
            getChildByName(tempName).visible = false; 
            //menu_One.visible = false; 
        } 

     } 
  } 
} 

The main change is that you need to tell flash that the string in your array is an instance name. So use getChildByName assuming they are added to to the stage.

The reason your current code is failing is because you are trying to access the visible property on a String, but String does not have a visible property. But the actually instance of that string name might.

1 Comment

Brilliant! Thanks. I was thinking the compiler would automatically try and find the instance for the given string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.