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;
}
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.
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;
}
North America
Europe, Middle East and Africa
Asia Pacific