Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we'll talk about loading an external CSS file into Flash, and using that CSS file to style text in a dynamic text field. Before we go into Flash, if you have access to exercise files, find Chapter 03 and open the styles folder and open up style.css in a text editor. I'm going to open it in TextWrangler. This file is a very simple CSS file. This file defines two CSS selectors - styling for the a tag and styling selling for a:hover. In the last movie we talked to a lot about what a tag does. Remember the a tag is the html anchor tag or link tag The code I have selected here everywhere from the a, to the open and close curly braces, specifies a style for the a tag.
Notice the color here is set to CC0000. That's going to give us the same dark red color for our link that we had in the last movie. What's different here is a:hover. A:hover refers to a link when it's rolled over or hovered over with a mouse. In curly braces, there's the code that says text--decoration:underline. Text-decoration is a CSS property. The value is set using the : and the value is underline.
So the link will be red in Flash and then when we hover our mouse over the link there will be an underline under the link. I'm going to Tab over to Flash. In Flash I'm working in 05_CSS_External.fla. This file has a text field on the stage that we've been working with throughout this chapter. Let's go to the first keyframe of the actions layer and open the Actions panel by pressing Option+F9 on the Mac or F9 on the PC. The code that's already in the Actions panel simply loads the external text file htmlText.
Now let's talk about bringing an external CSS file into Flash. I'm going to go below line two. I'm going to create a variable. And this variable is going to be called cssLoader and this is going to load that CSS file that we looked at earlier. The data type is going to be URLLoader. Now set it equal to new URLLoader. Now, the great thing about ActionScript 3.0 is that loading an external text file is exactly the same as loading external CSS file.
And that's one great thing about this language, is that you can load an external text file, no matter what file type, in the same way. And that way you don't have to learn a different syntax for each type of file you're loading into Flash. So in the next line, create a variable called cssRequests, with a capital R. The data type will be URLRequest. Set that equal to a new URLRequest. Then in parentheses we'll pass in the path to our CSS file.
So in the parentheses I'm going to type an open and close quotes. Inside of the quote I'm going to type styles/ style.css Now, just like a loading our external text file, we have to tell Flash to load our external CSS file. What we want to do is load that file once the text files finish loading. So the way their code will work is that Flash will create all the variables that it needs to know to work with loading the text and CSS files.
Once Flash has loaded the text file, it will load our CSS file, and once the text and CSS files are loaded, then it will display the text inside of a text field styled using our CSS file. On the next line, let's create another variable to hold our CSS style and we'll call this variable style. The data type will be a StyleSheet. I'll give the value of the variable a new instance of the StyleSheet class.
Notice creating a new StyleSheet for internal CSS like we did in the last movie and for external CSS like we're doing in this movie is done in the same way. We create a new variable, give it a name, the data type is a StyleSheet, we set it to a new instance of the StyleSheet class. Now that we have our new variable set up, let's load our CSS file. The way that we're going to load the CSS file is we'll wait until the text file is fully loaded. Once the text file is loaded we'll load our CSS file. And then once the CSS file and the text file finish loading, then we'll put the text from the text file inside of the text field and we'll apply the CSS from the CSS file to the text in the text field.
Inside of the textLoaded function, select external_txt.htmlText = textLoader.data, on my code it's on line nine. Then I'm going to press Command+X on the Mac or Ctrl+X on the PC to cut the code. We'll paste it in just a minute. Inside of this function, the first thing we'll do is make our cssLoader load our CSS file. So type cssLoader, with a capital L, .load. And in parentheses pass in cssRequest, with a capital R.
On the next line we'll add an event listener to our css.Loader. So type cssLoader.addEventListener The event will be Event, capital E, .complete all caps Add a comma and a space and once the CSS file is loaded, we'll run a function called cssLoaded with a capital L. That way we can do something once the CSS file is finished. So now let's create the cssLoaded function. Below the textLoaded function, create a new function called cssLoaded. In the parentheses type event:Event with a capital E.
After the parenthesis :void because this function is not returning a value. Below the line of code we just wrote, type some open and closed curly braces and put your cursor inside of the curly braces. The first thing we need to do is associate the CSS file we loaded with our style variable that we created on my code on line 5. The way to do that is to use a method called parse CSS. So inside of the curly braces of the cssLoaded function type style.parseCSS, CSS are all capitalized.
Then inside of the parentheses we'll pass in cssLoader.data. The parse CSS method takes CSS text and essentially tells Flash to take a block of text and read it as if it were CSS code. Let's go to next line and the next thing we'll do is associate the StyleSheet we created with the text field on the stage. So type external_txt.stylesheet = style So this connects our StyleSheet with the text field on the stage. The last thing we have to do is take the text that's loaded from the external file and display it in our dynamic text field as html text.
To do that I'm going to press Command+V, or Ctrl+V on the PC, to paste the code that we cut earlier. Test the movie by pressing Command+Return on the Mac, Ctrl+Enter on the PC. Notice the link is red as we specified in our external CSS file. If you roll over the link, there's an underline. I'll close the Preview window. And there's a look at how to bring an external CSS file into Flash and apply it to text in a text field.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.