forked from: TextField.getCharIndexAtPointメソッドに関する質問

by 9re forked from TextField.getCharIndexAtPointメソッドに関する質問 (diff: 14)
TextField.getCharIndexAtPointメソッドに関する質問です。
TextField.getCharIndexAtPointに引数として与えるx座標の値が
textField.widthを超えると、
そのx座標に文字があるはずなのに、返り値が-1となってしまいます。
正常にインデックスを取得する方法はないでしょうか?
♥0 | Line 33 | Modified 2010-04-26 19:14:39 | MIT License
play

ActionScript3 source code

/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/hr5t
 */

// forked from wetcradle's TextField.getCharIndexAtPointメソッドに関する質問
/*
TextField.getCharIndexAtPointメソッドに関する質問です。
TextField.getCharIndexAtPointに引数として与えるx座標の値が
textField.widthを超えると、
そのx座標に文字があるはずなのに、返り値が-1となってしまいます。
正常にインデックスを取得する方法はないでしょうか?
*/
package {
    import flash.display.Sprite;
    import flash.text.*;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            var out:TextField = new TextField();
            out.y = 22;
            out.height = 400;
            out.border = true;
            addChild(out);
            
            var textField:TextField = new TextField();
            textField.height = 22;
            textField.multiline = false;
            textField.border = true;
            textField.text = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
            textField.width = textField.textWidth + 4;
            textField.scrollH = textField.maxScrollH;
            addChild(textField);
            var max:Number = textField.width;
            var index:int;
            XML.prettyPrinting = false;
            for (var x:Number=0; x<max; x += 10) {
            	//out.appendText(x+", "+textField.getCharIndexAtPoint(x, 10)+"\n");
            	// 下のコメントも参照下さい
            		out.appendText(<>{
	            			x
	            		}, {
	            			index = textField.getCharIndexAtPoint(x + 2, 2)
	            		}{
	            			(index >= 0) ? ", " + textField.text.charAt(index) : ""
	            		}</> + "\n");
            }
        }
    }
}