Skip to main content
rob day
Community Expert
Community Expert
April 7, 2009
Answered

Input Text and Sprite Dragging

  • April 7, 2009
  • 1 reply
  • 821 views

I'm trying to create a draggable dialog, which includes an input text field and I want to prevent the dialog from dragging when the input text is selected. If I create a sprite with a hit area at the top everything works as expected, but when I add the input text field below the hit area selecting the text drags the dialog. Here's an example of the problem:

import flash.display.Sprite;

import flash.events.MouseEvent;

import flash.text.TextField;

import flash.text.TextFormat;

//dialog back

var rectshape:Sprite= new Sprite();

rectshape.graphics.beginFill(0xCCDDCC,.8);

rectshape.graphics.drawRect(0,0,400,300);

rectshape.graphics.endFill();

addChild(rectshape);

rectshape.buttonMode=true;

rectshape.addEventListener(MouseEvent.MOUSE_DOWN, onClick);

stage.addEventListener(MouseEvent.MOUSE_UP, onUp);

//hit area

var hit:Sprite= new Sprite();

hit.graphics.beginFill(0,.2);

hit.graphics.drawRect(0,0,400,40);

hit.graphics.endFill();

rectshape.addChild(hit);

//define the hit area

rectshape.hitArea=hit;

rectshape.x=200;

rectshape.y=200;

//input text

var format:TextFormat=new TextFormat();

var tf:TextField=new TextField();

rectshape.addChild(tf);

tf.x=20;

tf.y=50;

tf.width=350;

tf.height=150;

format.size=17;

format.color=0xFFFFFF;

format.align="left";

tf.multiline=true;

tf.wordWrap=true;

tf.selectable=true;

tf.defaultTextFormat=format;

tf.text="input text--Na alit et la cor susciduip enim dolenim acil ut prat nis nulputat ipit velissenit lamet, consed tionullam, quat. Equatum dunt praessit dignismodip et lortin ent acilla faci tat nonsed mod elestrud tem vel dit veliquis non hendiam, volore facidunt ipisl ullaore dit alit dionsed modipis nonsecte modion henissi.";

function onClick(event:MouseEvent):void {

rectshape.startDrag(false);

}

function onUp(event:MouseEvent):void {

rectshape.stopDrag();

}

This topic has been closed for replies.
Correct answer kglad

i don't see where you made your textfield's type "input", but to answer your question just add a MOUSE_DOWN listener to your textfield and enable the use-capture phase of your rectshape:

rectshape.addEventListener(MouseEvent.MOUSE_DOWN,onClick,true);

tf.addEventListener(MouseEvent.MOUSE_DOWN,whateverF);

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 7, 2009

i don't see where you made your textfield's type "input", but to answer your question just add a MOUSE_DOWN listener to your textfield and enable the use-capture phase of your rectshape:

rectshape.addEventListener(MouseEvent.MOUSE_DOWN,onClick,true);

tf.addEventListener(MouseEvent.MOUSE_DOWN,whateverF);

rob day
Community Expert
rob dayCommunity ExpertAuthor
Community Expert
April 7, 2009

Thanks. I've always been phase-challenged.

kglad
Community Expert
Community Expert
April 7, 2009

you're welcome.