Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-25-2007, 04:23 PM   #1
Innova
Registered User
 
Join Date: Apr 2007
Posts: 12
Default How to dynamically add button to a TextField

I am dynamically creating multiple text fields, that I want to record clicks to.

For example:

Code:
createTextField(fieldName, depth, x, y, w, h);
this[fieldName].text = "Click Me!";
How do I execute code when the text is clicked on? Can I add a button (same x, y, w, h) to the movie clip? Can I add a button to the textField?

Do I need to first create a child movie clip, then add the text to that, and then use the onPress even handler of the child movie clip?

Any other suggestions?
Innova is offline   Reply With Quote
Old 04-25-2007, 04:32 PM   #2
dmolanphy
Flash Goooood.
 
dmolanphy's Avatar
 
Join Date: Sep 2001
Posts: 113
Default

I would suggest starting with an empty movieclip like so:

this.createEmptyMovieClip(...)

Then inside that, create the textField. That way you can check for the onRelease events of the MC.
__________________
--
dm
MD Blog
Flash goooood.
dmolanphy is offline   Reply With Quote
Old 04-25-2007, 04:37 PM   #3
infernosnow
Senior Member
 
Join Date: Jan 2007
Posts: 254
Default

I agree.
infernosnow is offline   Reply With Quote
Old 04-25-2007, 04:48 PM   #4
Innova
Registered User
 
Join Date: Apr 2007
Posts: 12
Default

I thought you guys would say that, so I started down that path.

But, it isn't going as smooth as I thought it would, can you point out the error in my ways?

Code:
function galleryNameSuccess(re:ResultEvent){
	//set initial position for text
	x = 110;
	y = 220;
	depth = 1000;
	for(i = 0; i < re.result.length; i++){
		//create uniqe fieldName
		var fieldName:String = "txtField" + i;
		var moveClipName:String = "movieField" + i;
		
		var mc:MovieClip = createEmptyMovieClip(movieClipName, depth);
		mc._x = x;
		mc._y = y;
		mc._height = 15;
		
		//create new textfield
		mc.createTextField(fieldName, depth + 1000, x, y, 50, 15);
		//set the text attribute
		mc[fieldName].text = re.result[i];
		mc._width = mc[fieldName].textWdith;
		//apply text format
		if(re.result[i] == _parent.currentGallery){
			mc[fieldName].setTextFormat(selectedFormat);
		}
		else{
			mc[fieldName].setTextFormat(fmt);
		}
		//increase x for next time through
		x += mc[fieldName].textWidth + 5;
		//only allow 5 items on the first row.
		depth++;
		if(i == 4){
			y += 15;
			x = 117;
		}
	}
}
I don't get any errors when I run this, and if I put a trace of mc[fieldName].text, I get the expected values. However, none of the text displays.

Just for reference, this is the code that I had working, before I added the child movie clip:

Code:
function galleryNameSuccess(re:ResultEvent){
	//set initial position for text
	x = 110;
	y = 220;
	depth = 1000;
	for(i = 0; i < re.result.length; i++){
		//create uniqe fieldName
		var fieldName:String = "txtField" + i;
		//create new textfield
		createTextField(fieldName, depth, x, y, 50, 25);
		//set the text attribute
		this[fieldName].text = re.result[i];
		//debugging information
		//trace(fieldName + ":" + this[fieldName].text + "," + x + "," + y);
		//apply text format
		if(re.result[i] == _parent.currentGallery){
			this[fieldName].setTextFormat(selectedFormat);
		}
		else{
			this[fieldName].setTextFormat(fmt);
		}
		//increase x for next time through
		x += this[fieldName].textWidth + 5;
		//only allow items on the first row.
		depth++;
		if(i == 4){
			y += 15;
			x = 117;
		}
	}
}
Anyone know why I am not getting text to display in the first chunk of code?
Innova is offline   Reply With Quote
Old 04-25-2007, 06:40 PM   #5
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

You could use asfunction to do that...
ActionScript Code:
this.createTextField("test_txt", 100, 0, 0, 200, 20); this.test_txt.html = true; this.test_txt.htmlText = "<a href='asfunction:test,hello'>Execute a function</a>" + " | " + "<a href='asfunction:goURL,http://www.actionscript.org'>Visit AS.org</a>"; function test(_arg) {     trace("Function test : " + _arg); } function goURL(_arg) {     trace("Function goURL : " + _arg);     getURL(_arg); }
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 04-25-2007, 06:52 PM   #6
Innova
Registered User
 
Join Date: Apr 2007
Posts: 12
Default

Thanks CyanBlue, that is exactly what I was looking for!
Innova is offline   Reply With Quote
Old 04-25-2007, 07:11 PM   #7
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Glad to hear that...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a TextField over a MovieClip cboat ActionScript 2.0 3 10-02-2006 07:57 PM
Using a textfield as a button wishIKnew ActionScript 2.0 0 09-14-2006 03:40 PM
XML nodes--forward and back buttons, or next sibling, actionscript and xml. mahoganyhorizons Projectors and CDs 18 07-27-2006 03:34 PM
newly created dynamic textfield covers button terek ActionScript 2.0 1 07-28-2005 02:10 PM
converting loaded textfield into button jerryj ActionScript 2.0 4 05-28-2005 06:38 PM


All times are GMT. The time now is 09:13 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.