Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding Links to Dynamic Text

Posted on 2008-10-14
4
Medium Priority
?
2,965 Views
Last Modified: 2013-11-11
I'm trying to add a link to some text in a dynamic text box in AS3.0 (actually its an AIR file, but most of the rules are the same).  I need the selected text to allow me to download a file whenever that text is clicked on, the file being just 1.jpg

I ran something similar in AS 2.0, where I had some text that I dynamically added links to.  That code is at the bottom.  I tried to modify my code for AS 3.0, but got some errors about htmlText.

My AS 2.0 code

function setupTextField(txtInst, txt, wordToSearch, gotoFrame) {
   
        var wordLength = wordToSearch.length;
        var index = txt.indexOf(wordToSearch);
        if (index == -1) {
                return;
        }
            
        var linkTxt = '<a href="asfunction:gotoFunction,'+gotoFrame+'~~'+wordToSearch+'"><font color="#FFFFFF">'+wordToSearch+'</font></a>';
        txt = txt.split(wordToSearch).join(linkTxt);
       returnTxt = txt
            return returnTxt
        //trace(txt.htmlText);
}

//gotoFunction('word~~word');
function gotoFunction(param) {
        var frame = param.split('~~')[0];
        var upcomingFrameVariable = param.split('~~')[1];
   
            clickedName.text = upcomingFrameVariable
            gotoAndPlay("caseStudies")
}
public function showContent():void {
 
introCopy+= "the usage guidelines (PDF)";
this.bodytext.introText.text = introCopy
//when clicked, the words 'usage guidelines' should download the 
//file 1.jpg
 
}

Open in new window

0
Comment
Question by:walker6o9
  • 3
4 Comments
 
LVL 39

Accepted Solution

by:
blue-genie earned 2000 total points
ID: 22718285

package 
{
  import flash.display.MovieClip;
	public class Download extends MovieClip
	{
		private var introCopy:String = "";
		
		public function Download() {
			
			showContent();
		}
		public function showContent():void
		{
 
			introCopy = "the <a href='http://www.google.co.za'>usage guidelines (PDF)</a>";
			bodytext.introText.htmlText = introCopy;
			//when clicked, the words 'usage guidelines' should download the 
			//file 1.jpg
 
		}
	}
}

Open in new window

0
 
LVL 1

Author Comment

by:walker6o9
ID: 22726099
Blue-genie.

I'm sorry, I think you misunderstood me, or I was not clear.

What I need to have happen, is that rather than have go to a website when a link is clicked, I need an image, that is in the same folder as my Flash/AIR file, to download to the user's computer.  

If you don't know how to make an image download, i would be also be helped by just having a simple function run (like a trace statement saying HELLO WORLD), when the link is clicked, and then try to figure the rest out myself.
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 22728713
FileReference.download() in AS3 lets you download from a remote server.
usage code available in the Help file. how you want to integrate that into the text, hmmm.
asfunction doesn't exist in AS3 anymore - how i would "hack" it.

1. either put an invisible button on that bit of text - normal click listener to trigger fileReference dialog (this by the way open a normal windows browse for file box - whether you can specify a file and location not sure -won't hurt to try and see if it laughs at you).
2 .use the same method posted above but include javascript to trigger the function.
3. find out if AIR has something to make it happen (i've never used air yet)
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 22728842
oh hey, i just found the answer :-)
i'm learning from your problems this rocks.
see post from help file.
Example 
 
 
In the following example, the playMP3() function is defined. A TextField object named list is created and populated with HTML text. The text "Track 1" and "Track 2" are links inside the text field. The playMP3() function is called when the user clicks either link. The name of the MP3 file, which follows the string "event:" in the href attribute of the HTML tag, is passed to the linkHandler() method as the text property of the link event object.
 
package {
    import flash.display.Sprite;
    import flash.errors.IOError;
    import flash.events.IOErrorEvent;
    import flash.events.TextEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
 
    public class TextField_event_link extends Sprite
    {
        private var myMP3:Sound;
        public function TextField_event_link() {
            myMP3 = new Sound();
            var list:TextField = new TextField();
            list.autoSize = TextFieldAutoSize.LEFT;
            list.multiline = true;
            list.htmlText = "<a href=\"event:track1.mp3\">Track 1</a><br>";
            list.htmlText += "<a href=\"event:track2.mp3\">Track 2</a><br>";
            addEventListener(TextEvent.LINK, linkHandler);
            addChild(list);
        }
        
        private function playMP3(mp3:String):void {
            try {    
                myMP3.load(new URLRequest(mp3));
                myMP3.play();
            }
            catch(err:Error) {
                trace(err.message);
            }
            myMP3.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
        }
        
        private function linkHandler(linkEvent:TextEvent):void {
            playMP3(linkEvent.text);
        }
        
        private function errorHandler(errorEvent:IOErrorEvent):void {
            trace(errorEvent.text);
        }
    }
}

Open in new window

0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question