This is starting to get good. Are you building up to the bouncing smilie?
I like the attachment demo/sample at the end.+rep
CSS and Action Script
Earlier we looked at the text format object as a way of doing simple formatting. What if you need more control? This is where you can use CSS to get a lot more flexilibity over your formatting.
This is where it helps to have a basic understanding of HTML as well. You can use html in your flash program by setting the htmlText property instead of the text property.
For this tutorial, we are going to create a text field in code and add it to the stage. Use this code:
This should all make more sense now, and it really isn't complicated. The text field will appear at the top left of the stage.Code:var txtCC:TextField = new TextField(); txtCC.selectable = false; stage.addChild(txtCC);
htmlText Property
The generic HTML for a link is:
We would replace path with a URL that we want to link to. If you are familar with HTML you know that there are other properties such as target that you can use. Those you can use in AS as well.Code:<a href="path">text</a>
When you run it, you will see codecall and if you move the mouse over the control the cursor will change. One thing to note is that there is no default settings in the Flash Player unlike in the web browser.Code:txtCC.htmlText = '<a href="http://www.codecall.net">Code Call</a>';
CSS for the link
When the user doesn't have the mouse over the link we want the text to be underlined and red. When the user moves the mouse over the link, we don't want the text to be underlined and the color will become blue.
Just the CSS for this is:
If I was making an HTML page, that is what I would use. We might have to use hexadecimal in Action Script though. The :hover is a pseudo class which allows you to format things when the mouse moves over the element. In this case when the user hovers over a link.Code:a { text-decoration: underline; color: red; } a:hover { text-decoration: none; color: blue; }
Using a style sheet object
The first thing we have to do is create a new style sheet object.
Now, we can set the styles for the style sheet and then apply them to the label (actually a text field).Code:var styles:StyleSheet = new StyleSheet();
Example:
The setStyle takes two parameters the HTML element to add the property to and the property to set. The properties to set should appear in brace brackets. The property is not in quotations and followed by a colon. Then in quotes is the value of the property.Code:styles.setStyle("a",{color:"Red"});
If you are setting more than one property, they should be seperated by colons like this:
Notice that backgroundColor is in camel case. In the web browser, we would write it like this:Code:styles.setStyle("a",{color:"Red",backgroundColor: "green"});
However, in flash this doesn't work. You must use camel case.Code:background-color
The styles that we want to set for the link are:
You have to use hexadecimal for the colors. Applying the styles to the text field is as easy as this:Code:styles.setStyle("a",{color: "#FF0033",textDecoration: "underline"});
Code:txtCC.styleSheet = styles;
Mouse Overs
Now, we are going to set the styles for a:hover. This isn't anything different.
The code is:
Add thatt line just about the txtCC.styleSheet line. Then run the program again.Code:styles.setStyle("a:hover",{color: "#6666FF", textDecoration: "none"});
Calling a function on click
You can also link to a function from a link. To demonstrate this we are going to add a second text field.
We are going to apply the same style sheet above to this text field.Code:var txtLink:TextField = new TextField(); txtLink.x = 100; txtLink.y = 50; txtLink.htmlText = '<a href="event:output">Output</a>'; stage.addChild(txtLink);
Now, we can set up an event listener to listen for link clicks and a callback function to respond.Code:txtLink.styleSheet = styles;
First the listener:
When the Link is clicked, we will call the output function which will count how many times you clicked the link. At the top of the file create a variable nCount = 0.Code:txtLink.addEventListener(TextEvent.LINK,output);
Everytime that you click the event, that function will be called and you will get a running total of how many times the link was clicked.Code:function output(e:TextEvent):void { nCount++; txtLink.htmlText = '<a href="event:output">' + String(nCount) + '</a>'; }
External Style Sheets
Later, we will assess if it is possible to use external CSS files to save us time. The beauty of this is, you could change the design without recompiling your flash movie.
You can view the result here: CSS.swf.
This is starting to get good. Are you building up to the bouncing smilie?
I like the attachment demo/sample at the end.+rep
Cool![]()
If you're not careful, you'll become our flash expert. +rep
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
There are currently 1 users browsing this thread. (0 members and 1 guests)