Skip navigation
Currently Being Moderated

getCharBoundaries

Nov 7, 2011 5:01 AM

Hi,

 

I'm trying to display several movie clips at the "____" positions on the stage. The following code works fine for displaying one 'hTarg' movie clip, but what I want to do is display the hTarg movie clip at all of the occurances of "____". I've been trying to loop through to make this work, but can't seem to nail it down. If anyone could show me how to loop through in order to accomplish this(or possibly an altogether better way of accomplishing this), I would very much appreciate it.

 

Thanks,

~chipleh

 

myTitle.text = "The ____ jumped over the ____ with a ____ in the ____";
var mystr:String = myTitle.text;
var myArr:Array = [];
myArr = mystr.split("____");
trace(myArr.length);
for(var h:Number = 0;h<myArr.length;h++)
{
 var hTarg:hTClip = new hTClip();
 addChild(hTarg); 
 var a : int = (myTitle.text as String).indexOf("____");  
 var b : Rectangle = myTitle.getCharBoundaries(a);
  
 hTarg.x = myTitle.x + b.x
 hTarg.y = myTitle.y + b.y
 hTarg.width = (b.width);
 hTarg.height = b.height;   
}
 
Replies
  • Currently Being Moderated
    Nov 7, 2011 5:51 AM   in reply to Chipleh

    Here's one approach, though it is off slightly due to the difference in width between and x and an underscore character.  Maybe you can fiddle with it and find a character that has the same width as the underscore.  It looks like you also need to adjust how you compute the width property.

     

    myTitle.text = "The ____ jumped over the ____ with a ____ in the ____";
    var mystr:String = myTitle.text;
    var myArr:Array = [];
    myArr = mystr.split("____");
    trace(myArr.length);
    for(var h:Number = 0;h<myArr.length-1;h++)
    {
    var hTarg:hTClip = new hTClip();
    addChild(hTarg);
    var a : int = (myTitle.text as String).indexOf("____"); 
    var b : Rectangle = myTitle.getCharBoundaries(a);
     
    hTarg.x = myTitle.x + b.x
    hTarg.y = myTitle.y + b.y
    hTarg.width = (b.width);
    hTarg.height = b.height;

     

    // take the current set of underscores out
    var myPattern:RegExp = /____/; 
    myTitle.text = String(myTitle.text).replace(myPattern, "xxxx");
    }

     

    // put all the underscores back in

    var myPattern2:RegExp = /xxxx/g; 
    myTitle.text = String(myTitle.text).replace(myPattern2, "____");

     

     

    Edit:  Obviously, Peter's solution is much better.  He knew of what I was wishing for... the start index parameter for the indexOf method.

     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 7, 2011 5:44 AM   in reply to Chipleh

    This should do the trick:

     

    myTitle.text = "The ____ jumped over the ____ with a ____ in the ____";
    var index:int = -1;
     
    while(true) {
        index = myTitle.text.indexOf("____", index + 1);
        if(index == -1) {
            break;
        }
     
        var startRect:Rectangle = myTitle.getCharBoundaries(index);
        var endRect:Rectangle = myTitle.getCharBoundaries(index + 3);
     
        var hTarg:hTClip  = new hTClip();
        addChild(hTarg);
     
        // I'm not sure if the rect is relative to the TextField or its parent.. try, maybe you don't need myTitle.x / myTitle.y
        hTarg.x = myTitle.x + startRect.x;
        hTarg.y = myTitle.y + startRect.y;
        hTarg.width = endRect.right - startRect.left;
        hTarg.height = startRect.height;
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Nov 7, 2011 5:53 AM   in reply to Chipleh

    There's no question which is the cleaner solution.  I don't mind a helpful though.

     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 7, 2011 6:02 AM   in reply to Chipleh

    Ned was quicker I was writing it for a while and didn't notice Ned's answer. To be fair, he deserves correct.

     

    ... or give Ned 2 helpfuls and we're even

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points