A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Flash search box with actionscript3 help

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1

    Flash search box with actionscript3 help

    Hi, I would like to create a search box that searches whatever I put on the stage. For example, I place 10 movie symbols on the stage, I then create an input search text field where I would type in one of the movie names in the box and hit search. If the name matches with the name of either of the symbols on the stage, it would then highlight that movie symbol. Can we do it with As3?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, you can iterate through all the children in the display list by traversing it as a tree. You can compare each DisplayObject's name with your search term. Here's an example which returns an Array of all objects with a name which exactly matches the search input. You can alter it to match in a more general way by changing the compare function.

    Code:
    function compareNameExact(dobj:DisplayObject, criteria:String):Boolean{
      return dobj.name == criteria;
    }
    
    function searchDisplayTree(searchTerm:String, compareFunction:Function):Array{
      var matches:Array = [];
      walkTree(stage, matches, searchTerm, compareFunction);
      return matches;
    }
    
    function walkTree(node:DisplayObjectContainer, accumulator:Array, term:String, cf:Function):void{
      if (cf(node, term)){
         accumulator.push(node);
      }
      for (var i:int = 0; i < node.numChildren; i++){
        var kid:DisplayObject = node.getChildAt(i);
        if (kid is DisplayObjectContainer){
          walkTree(DisplayObjectContainer(kid), accumulator, term, cf);
        }else{
           if (cf(kid, term)){
             accumulator.push(kid);
           }
        }
      }
    }
    
    trace(searchDisplayTree("someName", compareNameExact));
    You could further abstract it so that the walking function does not take the term, and have the comparison function built dynamically, but that'll do for now.

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