Skip to main content
December 4, 2009
Answered

Best or easiest to load texts with link?

  • December 4, 2009
  • 1 reply
  • 1855 views

I was contemplating which is the best or easiest way to create three texts with links an Internet site. One method is creating a static text in design mode and only show the static texts with hyperlink or creating all these programmatically by loading external texts and adding the link in code.

I was thinking that to be more flexible and portable, creating the hyperlink to some texts programmatically is better but I'm not sure where to start on that. I know how to load external texts now but I'm not sure how to add hyperlinks to it.

Any suggestion is appreciated.

This topic has been closed for replies.
Correct answer kglad

It's still not working. Here's the code:

function formatLink():void {
    var myCSS:StyleSheet = new StyleSheet();
    myCSS.setStyle("a:link", {color:'#0000CC',textDecoration:'none'});
    myCSS.setStyle("a:hover", {color:'#0000FF',textDecoration:'underline'});
    txtFormat.font = weiss.fontName;
    txtFormat.color=0x000066;
    txtFormat.size=24;
   
    //myTxtField.setTextFormat(txtFormat);
    myTxtField.defaultTextFormat(txtFormat);
    myTxtField.embedFonts = true;
   
    //loading external CSS file
    /*cssLoader = new URLLoader();// Create a new URLLoader object
    cssLoader.load(new URLRequest("eGreeting.css"));// Load "default.css" with a new URLRequest object
    css = new StyleSheet();// Create a new StyleSheet object
    css.parseCSS(cssLoader.data);// Parsing CSS*/
    //end of loeading external CSS file
   
    myTxtField.styleSheet = myCSS; // Linking CSS to TextField
    //end creating css
   
    myTxtField.htmlText="";
    myTxtField.htmlText="<p><a href='http://www.mysite.com/' target='_blank'>My Link</a></p>";
    myTxtField.wordWrap=false;
    //myTxtField.embedFonts = true;
    myTxtField.x=150;
    myTxtField.y=100;
    //trace("Third txtField.y: " + txtField.y);
    myTxtField.alpha=0;
}

And the error for the defaultTextFormat:

1195: Attempted access of inaccessible method defaultTextFormat through a reference with static type flash.text:TextField.

Is the above error means I can't do this?

myTxtField.htmlText="<p><a href='http://www.mysite.com/' target='_blank'>My Link</a></p>";


the defaultTextFormat is a property, not a method.   instead of:

   myTxtField.defaultTextFormat(txtFormat);

use:

   myTxtField.defaultTextFormat = txtFormat;

1 reply

kglad
Community Expert
Community Expert
December 4, 2009

you can use the asfunction to create a link in a textfield.

December 7, 2009

Will you explain what a "asfunction" is and how do I use it? Any link to examples is much appreciated.

December 7, 2009

Okay, here's a version I tried but the texts with hyperlink does not show up on the stage. Any suggestion?

//import classes
import com.greensock.TweenLite;
import com.greensock.easing.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
//end of importing

//start of sound section is for sound
var soundReq:URLRequest=new URLRequest("PaukenBrumfiel_AngelsOnHigh.mp3");
var sound:Sound = new Sound();
sound.load(soundReq);
sound.addEventListener(Event.COMPLETE, onComplete);
//end of sound section

//Loading external texts
var weiss:Font = new Weiss();
var slideXTween:Tween;
var txtAlphaTween:Tween;
var txtNextText:Tween;
var txtContainer:Sprite;
var txtField:TextField = new TextField();
var textRequest:URLRequest=new URLRequest("happyHoliday.txt");
var textLoad:URLLoader = new URLLoader();
var txtFormat:TextFormat = new TextFormat();
//end of loading external texts

function onComplete(event:Event):void {
    sound.play();
}

function textReady(event:Event):void
{
    txtFormat.font = weiss.fontName;
    txtFormat.color = 0x000066;
    txtFormat.size = 24;
    //txtField.x = stage.stageWidth;
    //txtField.width = 800;
    txtField.autoSize = TextFieldAutoSize.LEFT;
    //txtField.height = txtField.autoSize
    txtField.wordWrap = false;
    txtField.text = event.target.data;
    txtField.setTextFormat(txtFormat);
    //txtField.y = 350;
    txtField.embedFonts = true;
}

//load external texts
textLoad.load(textRequest);
textLoad.addEventListener(Event.COMPLETE, textReady);
txtField.x = stage.stageWidth;
addChild(txtField);
//trace("First txtField.y: " + txtField.y);
txtField.y = 350;
TweenLite.to(txtField, 40, {x:-stage.stageWidth*2.25, ease:Linear.easeNone, delay:0.5, onComplete:myFunction});
function myFunction():void {
    //trace("Second txtField.y: " + txtField.y);
    textLoad.load(new URLRequest("inspiration.txt"));
    txtField.x = 150;
    txtField.y = 330;
    //trace("Third txtField.y: " + txtField.y);
    txtField.alpha = 0;
   
    TweenLite.to(txtField, 5, {alpha:1, onComplete:nextText});
}
function nextText():void{
    //create and initialize css
    var myCSS:StyleSheet = new StyleSheet();
    myCSS.setStyle("a:link", {color:'#0000CC',textDecoration:'none'});
    myCSS.setStyle("a:hover", {color:'#0000FF',textDecoration:'underline'});
   
    txtField.styleSheet = myCSS;
    //end creating css
   
    txtField.htmlText = "";
    txtField.htmlText = "<a href='http://www.uwstout.edu/advancement/' target='_blank'>University Advancement Office</a>";
   
    txtField.x = 150;
    txtField.y = 350;
    //trace("Third txtField.y: " + txtField.y);
    txtField.alpha = 0;
   
    TweenLite.to(txtField, 5, {alpha:1, onComplete:nextLink});
   
    trace("tween done");
}
function nextLink():void{
    trace("done nextLink fuction");
}

//end of loading external texts