Flash Essential

Loading External CSS Into Your Site

May 3rd 2008
3 Comments
respond
trackback

In this tutorial I will show you how to use CSS as a sort of text-editor for your flash content. We'll load in our text from our actionscript using a String, so all we'll have on the stage is our background. Take a look at the Finished Example

Download:

Starter File

Finished File

Step 1

Ok the starter file contains a CSS file and a Fla file with a background of a book, badly made in Photoshop by myself I might add. So open up the fla file, go to the Actions layer and Press F9 to open up the Actions Panel. The first thing we are going to do is create a String that will contain the text we want to be displayed on the site.

var pageContent:String = "<H1>Tutorial 3</H1>\n\n<P>Started a big tutorial today but got really really annoyed with it, the navigation part to be exact so decided to start this load CSS tutorial instead.</P>";

Notice the html tags inside the text string? This is picked up by our CSS file allowing us to edit it.

Step 2

Next we'll create a text container to hold our content, we'll do this by creating a Sprite called txtContainer. We position the txtContainer on the stage by giving it X and Y values. Finally we use the addChild method, this adds the txtContainer to the stage

var txtContainer:Sprite = new Sprite();
  • txtContainer.x = 50;
  • txtContainer.y = 100;
  • addChild(txtContainer);
  • Code Explained

    1. Create a new sprite called textContainer, a sprite is basically a container.
    2. The x position of the textContainer on the stage
    3. the y position of the textContainer on the stage
    4. the addChild method adds the text container to the stage

    Step 3

    Now we'll create a TextField to go inside the txtContainer we have just created. So we create a new TextField called myTxt and then add some simple properties.

    1. var myTxt:TextField = new TextField();
    2. myTxt.width = 300;
    3. myTxt.wordWrap = true;
    4. myTxt.autoSize =TextFieldAutoSize.LEFT;
    5. txtContainer.addChild(myTxt);

    Code Explained

    1. New text field named myTxt
    2. Gives it a width of 300px
    3. Sets the wordWrap value to true meaning the text is word wrapped
    4. The text is treated as left-justified text
    5. Adds mytxt to the txtContainer

    Step 4

    Now we need to load our CSS, we do this by using a URLRequest.

    1. var cssLoader:URLLoader = new URLLoader();
    2. var cssRequest:URLRequest = new URLRequest("loadcss.css");
    3. cssLoader.addEventListener(Event.COMPLETE, cssLoaderComplete);
    4. cssLoader.load(cssRequest);

    Code Explained

    1. A new URLLoader called cssLoader
    2. A new URL Request call cssRequest, and inside this request we have the link to our CSS file.
    3. adds an event listener to our cssLoader(line1) that listens for when the event has completed. We'll give this the name cssLoaded.
    4. This loads the the cssRequest file

    Step 5

    Finally we add our event handler for cssLoaderComplete(Step 4 line 3).

    1. function cssLoaderComplete(evt:Event):void{
    2. var css:StyleSheet = new StyleSheet();
    3. css.parseCSS(URLLoader(evt.target).data);
    4. myTxt.styleSheet = css;
    5. myTxt.htmlText = pageContent;
    6. }

    Code Explained

    1. cssLoaded function which is an Event object, we type void because we aren't returning any values
    2. Creates a new stylesheet object.
    3. This css parse method fills our stylesheet object with the css we have loaded.
    4. This basically attaches our myTxt text field to our CSS content that we have loaded in.
    5. Make sure the text doesn't load until our stylesheet has been loaded.

    Conclusion

    So thats it. Pretty simple when you look at it. Obviously you can add more text fields and more html tags inside the page content String so you can format more text, but this is just a basic overview of how it works. I hope it helps.

    Tip: If you aren't sure what a line of code does (my explanations aren't the best) just highlight the word inside your actionscript panel and hit F1, and it will open up the help topic and give you a much better explanation of the code.


    This post is tagged , , ,



    Sponsors

    Explore Recent





    Monthly Archives



    Friends and Affiliates



    3 Comments

    1. Thanks.
      It is soo good.

    2. Brandon

      Thnx, great tutorial. Clear and useful

    Incoming Links

    Leave a Reply

    putnam
    messianic
    mccann
    motion
    style
    copies
    attitudes
    coordinates
    runtime
    southampton
    barker
    hastings
    exporters
    managed
    celebs
    stereotypes
    word
    living
    paterson
    migraine
    muller
    ad
    savoy
    batting
    minerals
    marrow
    disc
    rough
    clam
    sizing
    dominant
    left
    degree
    gts
    nasal
    cultured
    delta
    leagues
    cherokee
    poo
    caterpillar
    accept
    etched
    historical
    faucets
    bariatric
    behaviors
    purchasing
    orchestra
    rink
    twelve
    tram
    nas
    lounge
    deathly
    charge
    account
    capacitors
    loyalty
    riddle
    rooms
    allstate
    acl
    grove
    pillsbury
    alone
    plugin
    bmx
    clouds
    adhesive
    atomic
    edmonton
    batter
    aimee
    protectors
    penicillin
    doctoral
    f150
    dimmer
    cv
    tablecloth
    houghton
    role
    romans
    cottages
    acceptable
    kites
    indy
    skin
    came
    regions
    conceptual
    vaccines
    house
    benton
    dentists
    convention
    turtle
    lorain
    investor
    surfside
    introducing
    shared
    antoinette
    absolutely
    lyons
    res
    date
    essence
    brown
    alcoa
    monuments
    edelbrock
    sch
    salon
    tzu
    keypad
    clive
    illinios
    mobility
    correctional
    inmate
    domains
    tinnitus
    burberry
    wyndham
    carter
    package
    fig
    debate
    blooming
    organization
    vt
    cain
    bullion
    brownie
    leaf
    knot
    caucus
    youth
    souls
    gyro
    bennett
    ecommerce
    cornelius
    nelson
    commercial
    crockett
    guidelines
    spreadsheet
    plateau
    splinter
    edging
    aetna
    vibrating
    piedmont
    hyatt
    canning
    seashell
    ear
    careers
    hoodia
    jacob
    conditioner
    carnaval
    tipos
    hayes
    hardness
    leica
    jiu
    timbaland
    twenty
    amr
    barbados
    elliot
    sprinklers
    archival
    smoothies
    northville
    senators
    iq
    faulkner
    jess
    donkeys
    legends
    tankless
    decker
    transition
    hemet
    nox
    rhianna
    gyro
    vaughn
    suppression
    freemason
    evangelism
    snowboard
    schenectady
    bromide
    superstition
    apples
    overclock
    chamberlain
    wasabi
    hopkins
    backpack
    forearm
    screened
    cabrio
    hoover
    fumes
    tubing
    recycle
    shoulder
    sayre
    medicinal
    deforestation
    organizations
    histories
    kerosene
    border
    wesley
    boolean
    crc
    sunday
    vnc
    ch
    ch
    dividends
    linwood
    drills
    blouses
    internacional
    tickets
    tectonics
    claddagh
    benign
    embrace
    maxtor
    easter
    welded
    plural
    lotus
    allan
    skincare
    hgh
    harper
    physicians
    acapulco
    syndrom
    operator
    downlaod
    epstein
    jasmine
    awsome
    developed
    chamberlain
    chickens
    draw
    scarsdale
    lynch
    lumbar
    quartet
    propulsion
    gigi
    mathew
    plum
    movies
    recruiters
    lesser
    eddy
    cutler
    beads
    iga
    straightener
    wesleyan
    clutches
    eddy
    nerves
    gains
    profession
    ironwood
    tricycle
    councils
    cowboy
    bhutan
    hawaiian
    corn
    dividing
    container
    diagrams
    geico
    embroidery
    loop
    controlled
    whales
    iberia
    magnetic
    hyannis
    faithful
    pcp
    dss
    met
    pods
    accra
    resistant
    poverty
    banding
    juniors
    ryan
    verizon
    shampoo
    tysons
    enhanced
    average