very impressive “TextField.getCharBoundaries” in Player 9.

April 22, 2008 at 5:55 am (Flex, flash) (, , )

hi i always wanted to play with text in flash each charecter of text and lots more.well all this was not possible with previous versions.

see below one of the best use of getCharBoundariesof text field.

var sptChar:Array = [".", ","];
//create a textfield with name “box_txt”
box_txt.text= “hi may name is jasmeet singh sehgal. I love scpting and playing with objects.”;

function drawBox(x,y,wd,ht,spr) {
var boxHold:Sprite = new Sprite();
boxHold.name =”boxHold”;
var sh:Shape = new Shape();
boxHold.addChild(sh);

sh.graphics.beginFill(0xFFFF00, 1);
sh.graphics.drawRect(x,y,wd,ht);
sh.graphics.endFill();
spr.addChild(boxHold);
}
var wordCount:int;
function recurFunct(stInd:int,txtField:TextField,clickSprite:Sprite,run:Boolean=true):void {

var str:String = String(txtField.text);
var stWordInd:int = stInd;

var endWordInd:int = str.indexOf(” “, stInd);
if (!run) {
endWordInd = str.length;
}
var word:String = str.substr(stWordInd, endWordInd – stWordInd);
var spcChar:Boolean = false;
for (var i in sptChar) {
if (word.lastIndexOf(sptChar[i]) > -1) {
spcChar = true;
trace(word.lastIndexOf(sptChar[i])+” :: “+ word.length +” :: “+ sptChar[i] );
}
}
var spr1:Sprite = new Sprite();

spr1.name = “BOXSprite” + wordCount;
wordCount++;

var rect1:Rectangle = txtField.getCharBoundaries(stWordInd);
var rect2:Rectangle = txtField.getCharBoundaries(endWordInd-1);

if (spcChar) {
trace(“rec 2 new”);
rect2 = txtField.getCharBoundaries(endWordInd-2);
}
//spr1.drawBox(0, 0, w, h);
trace(rect2 +”–”+ spcChar+”–”+word.substr(7,1));
drawBox(0, 0, (rect2.x + rect2.width – rect1.x), rect1.height,spr1);
spr1.x = txtField.x+rect1.x;
spr1.y = txtField.y+rect1.y;

spr1.addEventListener(MouseEvent.CLICK, clickHandler);
spr1.buttonMode = true;
clickSprite.addChild(spr1);
// trace(stWordInd +”::” + word + “::” + endWordInd + ” :: ” + (rect2.x + rect2.width – rect1.x ))
var box1:Sprite = spr1.getChildByName(“boxHold”) as Sprite;
box1.alpha = 0.5;

if (str.indexOf(” “,endWordInd+1) != -1) {
recurFunct(endWordInd+1,txtField,clickSprite);
} else if (str.indexOf(” “,endWordInd+1) == -1 && run) {
recurFunct(endWordInd + 1,txtField,clickSprite,false);
wordCount = 0;
}
}
recurFunct(0,box_txt,this);
function clickHandler(e:MouseEvent):void {
trace(e.currentTarget.name);
}

Permalink 1 Comment

resize image/swf as per container propotionately

April 21, 2008 at 10:05 am (Air, Flex, flash)

This is a genral function to resize the cntrols as per container the resize and position of
controls are intentionally kept different in 2 treads.

function resize(cont:DisplayObject,imgControl:DisplayObject):void
{
if (imgControl.width > imgControl.height ) {
imgControl.scaleX = imgControl.scaleY = ((cont.width)/imgControl.width) ;
} else {
imgControl.scaleY = imgControl.scaleX = ((cont.height )/imgControl.height ) ;
}
}

Calling the function and reseting the position to center here ;)

resize(canvas, image_cntr);
// put there 2 lines after calling the function resize
//these are for setting the X,Y of control in the center
image_cntr.x = (canvas.width/2)-(image_cntr.width/2 );
image_cntr.y = (canvas.height/2)-(image_cntr.height/2);

Permalink Leave a Comment