How to add smiley to chat application using Action Script 3 in Flash Professional

  October 20, 2012   by:  muni   No Comment

This may be simple task when someone give you this, But really some what difficult is there  while positioning smiley in the textfield. I had gone through Google none of them explained this.

Download sample files here.

Here is simple way I had tried to position the smiley in chat application at exact position, may it useful to you guys. Try this and let me know if you have any ideas about smiley positioning.

In this tutorial I getting text and smiley we had entered in the textbox and save it in a string variable. Then using split() method I split-ed that string   into text and smiley and saved it in array variable. Now you can get text and smiley separately using array index like.

str1[0]= hi every body

str1[1]=<img src=’eye.jpg’ vspace=’-20′ height=’40′ width=’40′/>

Now I added this ‘str1[0]= hi every body‘ in the TextField to know width of text to position the smiley in after the text. Then adjusting vspace and hspace property of img tag I positioned smiley at exact position.

To know various techniques for finding  text width of TextField refer my previous tutorial ‘how to find text width of TextField using ActionScript 3 in Flash professional’

How to add smiley to chat application in Action Script 3 Flash Professional
package
{
import flash.text.TextField;
import flash.display.MovieClip;
import flash.text.TextFormat;

	public class SmileyText extends MovieClip
	{
		public function SmileyText()
		{
			var txt:TextField= new TextField;
			var txt1:TextField= new TextField;
			var txt_format:TextFormat=new TextFormat;
			txt_format.font='Verdana';
			txt_format.size='15';
			var str:String="hi every body <img src='eye.jpg' vspace='-20' height='40' width='40'/>";

			var str1=str.split('<img');
			txt1.defaultTextFormat=txt_format;
			txt1.text=str1[0];
			var txt_width=txt1.textWidth;
			var str2:String="hi every body <img src='eye.jpg' vspace='-20' hspace='"+txt_width+"' height='40' width='40'/>";

			txt.defaultTextFormat=txt_format;
			txt.width=200;
			txt.htmlText=str2;

			addChild(txt);
		}
	}
}