Skip to main content
Inspiring
April 23, 2011
Answered

problem with getCharIndexAtPoint function

  • April 23, 2011
  • 2 replies
  • 1805 views

Hi

I've met a strange problem using the function; this is a simple code that works well:

package

{

     import flash.display.Sprite;

     import flash.events.MouseEvent;

    import fl.text.TLFTextField;

    import flash.text.TextFieldType;

    import flash.geom.Rectangle;

    import flashx.textLayout.elements.Configuration;

    import flashx.textLayout.elements.TextFlow;

    import flashx.textLayout.conversion.TextConverter;

    import flashx.textLayout.conversion.ConversionType;

     public class scrollV extends Sprite

        {

          private var tlf:TLFTextField = new TLFTextField;

          private var str:String = "Click in this text field. Compare the "+

                                           "difference between clicking without "+

                                           "selecting versus clicking and selecting text.";

                   

         public function scrollV()

           {

             var config:Configuration = TextFlow.defaultConfiguration;         

              config.manageTabKey = true;     // activate the tab key

              addChild(tlf);     tlf.width = 200;

              tlf.text = str;

              tlf.wordWrap = true;

              tlf.type = TextFieldType.INPUT;

              tlf.addEventListener(MouseEvent.CLICK, printCursorPosition);

trace(TextConverter.export(tlf.textFlow, TextConverter.TEXT_LAYOUT_FORMAT,ConversionType.STRING_TYPE) as String);

            }

    

          private function printCursorPosition(e:*):void

            {

              trace("char #:", tlf.caretIndex+" from:", tlf.selectionBeginIndex+" to:", tlf.selectionEndIndex);

              var rett:Rectangle = tlf.getCharBoundaries(tlf.caretIndex);

              trace('char pos: '+rett.x+','+rett.y);

              trace('idx #: '+tlf.getCharIndexAtPoint(rett.x,rett.y));

           }

       }

}         

In this code the getCharIndexAtPoint function work correctly; then I wanted to try the same function but using textFlow and, using the output of export trace, I have written the following code:

package

{

  import flash.display.Sprite;

  import flash.events.MouseEvent;

  import fl.text.TLFTextField;

  import flash.text.TextFieldType;

  import flash.geom.Rectangle;

  import flashx.textLayout.elements.Configuration;

  import flashx.textLayout.elements.TextFlow;

  import flashx.textLayout.conversion.TextConverter;

import flashx.textLayout.conversion.ConversionType;

  import flashx.textLayout.container.ContainerController;

  public class scrollV extends Sprite

   {

       private var tlf:TLFTextField = new TLFTextField;

       private var str:String =  '<TextFlow columnCount="inherit" +

                                         'columnGap="inherit" +

                                         'columnWidth="inherit" '+

                                         'lineBreak="explicit" paddingBottom="inherit" +

                                         'paddingLeft="inherit" '+

                                         'paddingRight="inherit" paddingTop="inherit" +

                                         'renderingMode="normal" '+

                                         'verticalAlign="inherit" +

                                         'whiteSpaceCollapse="preserve" '+

                                         'xmlns="http://ns.adobe.com/textLayout/2008"> '+

                                         '<p><span renderingMode="inherit">'+

                                         'Click in this text field. Compare the difference  '+

                                         'between clicking without selecting '+

                                         'versus clicking and selecting text. '+

                                         '</span></p>'+

                                         '</TextFlow>';

                   

       public function scrollV()

          {

             var config:Configuration = TextFlow.defaultConfiguration;         

             config.manageTabKey = true;   // activate the tab key

             addChild(tlf);     tlf.width = 200;

             tlf.textFlow= TextConverter.importToFlow(str,TextConverter.TEXT_LAYOUT_FORMAT);

             var control:ContainerController = new ContainerController(tlf);

             tlf.textFlow.flowComposer.addController(control);

             tlf.textFlow.flowComposer.compose();

             tlf.textFlow.flowComposer.updateAllControllers();

             tlf.wordWrap = true;

            tlf.type = TextFieldType.INPUT;

             tlf.addEventListener(MouseEvent.CLICK, printCursorPosition);

trace(TextConverter.export(tlf.textFlow, TextConverter.TEXT_LAYOUT_FORMAT,ConversionType.STRING_TYPE) as String);

           }

    

         private function printCursorPosition(e:*):void

          {

             trace("char #:", tlf.caretIndex+" from:", tlf.selectionBeginIndex+" to:", tlf.selectionEndIndex);

             var rett:Rectangle = tlf.getCharBoundaries(tlf.caretIndex);

             trace('pos: '+rett.x+','+rett.y);

            trace('idx #: '+tlf.getCharIndexAtPoint(rett.x,rett.y));

           }

     }

}         

in this code the getCharIndexAtPoint function gives the error #1009:Cannot access a property or method of a null object reference.

Does someone know how to tell me where the error is in this code?

thank for all.

This topic has been closed for replies.
Correct answer Jin-Huang

1. TLFTextField is a kind of Sprite (extends Sprite). So your solution makes sense anyway. I suggest you firstly ask the question again on the sub-forum related to Flash pro (or Flash authoring), because TLFTextField is under their control, rather than TLF.

2. More details of TLF, you can see its codes, docs and examples in http://sourceforge.net/adobe/tlf/home/

How to implement *getCharIndexAtPoint*

The cusor of TLF can return its own position, which can also present index of the char.

Sample Code:
textFlow.interactionManager = new SelectionManager();

textFlow.interactionManager.activePosition

textFlow.interactionManager.anchorPosition

2 replies

Adobe Employee
April 24, 2011

As I know, TLFTextField refers to *TLF text* located in *text engine* selection list of *Text tool* in Flash pro.

If you are not using Flash pro, you don't need TLFTextField. You can just use Sprite as the textflow's container.

public class as_helloWorld extends Sprite
    {
        public function as_helloWorld()
        {

            var markup:String = "<TextFlow xmlns='http://ns.adobe.com/textLayout/2008'><p><span>Hello, World</span></p></TextFlow>";
            var textFlow:TextFlow =  TextConverter.importToFlow(markup, TextConverter.TEXT_LAYOUT_FORMAT);

            textFlow.flowComposer.addController(new ContainerController(this,400,200));
            textFlow.flowComposer.updateAllControllers();

            textFlow.interactionManager = new EditManager(); // if you want your text to be editable
        }
    }

Also, you can add more sprites onto the main sprite by this.addChild. Then there are multi-containers and multi-textflows.

Adobe Employee
April 24, 2011

I add a TLFTextField named *tlf* onto the canvas by GUI in Flash pro and use the TLFTextField by its name as what you did, your error disappears.

Inspiring
April 24, 2011

Jin-Huang

thanks for the answer

I use Flash as an editor/compiler and create my components in as3 without using the GUI; for the static TLF I've used thin to now the sprite as container and textFlow for formatting, but so I cannot use the specific methods of TLFTextField. I'm now implementing an editor (text, images, link, video....) and the methods of TLFTextFields need me, particularly getCharIndexAtPoint and the methods of scroll (also these work if I implement the code in a frame of flash, but not in a class).

Nevertheless I would like to try your solution, but I'm not practical in the use of the GUI; you would be so kind to be written step to step what I should make?

thanks in advance

JeanPaul

Jin-HuangCorrect answer
Adobe Employee
April 25, 2011

1. TLFTextField is a kind of Sprite (extends Sprite). So your solution makes sense anyway. I suggest you firstly ask the question again on the sub-forum related to Flash pro (or Flash authoring), because TLFTextField is under their control, rather than TLF.

2. More details of TLF, you can see its codes, docs and examples in http://sourceforge.net/adobe/tlf/home/

How to implement *getCharIndexAtPoint*

The cusor of TLF can return its own position, which can also present index of the char.

Sample Code:
textFlow.interactionManager = new SelectionManager();

textFlow.interactionManager.activePosition

textFlow.interactionManager.anchorPosition