I realised I should post my last two tutorials together, so when Lenny has the time he can work through them on his own (I know you're still busy, don't worry).
So, here is the 3rd lesson:
--------------------------------------------------------------------------
This is what we'll create. It looks simple, but as we get to tutorial two you should be able to add your own features to it:
Flash file: Date Widget
When you see the opening screen of Flash, click "Create New: Flash file (Actionscript 2.0). You'll see a white page. Now, always remember, this base page colour will stay the same no matter how many layers and levels (think "see-through sheets on top of a white page") you have. So, if, as you become more advanced, you choose to start adding more levels (see-through pages) on top of this base level, the bottom page in your stack will always determine your flash background colour.
Also, the layout of Flash CS3 may look daunting. But we'll get to each part as we go along. Once you get the hang of the program, you'll see it's fairly basic.
So, pick a background colour. Whatever you like. You'll find the colour setting at the bottom of the page, in the Properties panel. You could keep the page white, or try black or grey.
Here's the shapes labelled:
1) On the left in Flash you'll see a selection toolbar. These tools are used for creating 2D shapes and freehand lines and such. Press the one near the top called Rectangle Tool (R), since we want to create shape A. You'll see the bottom panel, called "Properties", change and give you options.
2) We want to make a big rectangular box to fill up the page. The "stroke color" in the Properties panel determines the colour of the border around your 2D shape; the "fill color" determines the colour of the rectangle. The other boxes change the width and style of your border, and the options under these are not needed. Pick a combination of colours, any you like. I've chosen a black border and a light purple fill, with a border of 3.
3) Draw a large box that covers most of your white page. If you aren't happy with it, go to the "Edit Menu" at the top and select "undo rectangle".
4) You may have noticed the Timeline panel at the top of your screen. If you've ever used professional animation software, or video software, you'll know what this does. It lets you create layers on top of other layers, and each of those tiny white blocks shows the keyframe (page) you're working on. For example, if you wanted to create an animation of a bouncing ball, you'd draw a ball in the first frame, then you'd create more keyframes and draw the ball in the air, then coming back down again. Then you'd play the Flash "movie" and see the ball bouncing as Flash displays each frame. You can play about with this in your own time.
Right now, you want to create a new layer, one that will sit above the rectangle you've drawn. This is because we want to create a new 2D shape – the white rectangle, shape B – and Flash cannot have two shapes on top of each other on the same level, or else the bottom shape loses whatever sat underneath the top shape. Go to the Insert menu at the top of your screen, then click Timeline, then Layer. You'll see a new layer (Layer 2) above your old one in the timeline. Press once on the name "Layer 2" at the side of the Timeline. This will ensure you're now working on the correct layer, and not Layer 1.
5) Now we want to create the next rectangle on this new layer, shape B, but with a white fill. This shape will be centred on top of the main rectangle, toward the top. Make it about 1-2cm high. It will be the box the date is displayed in. Click Create Rectangle on the side Selection panel/toolbar, then set the properties of it and create the shape.
6) Once you have the rectangle in place, press the Selection Tool on the left-hand selection toolbar. This stops your mouse from making more shapes if you accidentally press it down.
7) We want to add in code in, now, that tells Flash to check your computer's date and display it in the white rectangle we've created. In Flash, the term for writing code is called writing actionscript.
8) Now we need to create a textbox, which will display today's date in it once you've written the actionscript. On the toolbar selection panel on the left, select Text Tool. Set your ideal font and size and colour using the Properties panel at the bottom of your screen. Remember, the font must show up over the white rectangle behind, so pick a dark colour. I've chosen font size 20 and black and Garamond. Now, in the pulldown box beside this it lists types of textbox. You want "Dynamic Text", because this means the texboxt can perform functions when linked with actionscript. You also need to make sure the options are set to "Single Line" and that the font is "align centre". Now drag and draw a textbox on top of the white rectangle on your page.
9) Once you've drawn the shape (don't worry about getting it exactly centred over the white rectangle), go back to the properties panel. In the part labelled "Var:", you need a unique identifying name for your textbox (so your actionscript can see it). We'll call it display_day. If you want to move the textbox now, just press on it once and drag.
10) Okay, at this point you should save your file. Go to the File Menu, then select Save As. Call the document something like flash_tut.fla, and save it in a new folder, which makes it easier to upload the files later if you want to put them on the net.
11) Now we add the actionscript to tell Flash to check your PC and find the date, then display it in the textbox named display_day. So, in the Timeline panel, you'll see that the two layers each have a black dot in keyframe 1, with a red line that runs through both vertically. Press the black dot in frame 1 of Layer 1 to select it, because we will add the actionscript in that keyframe. Press F9 on your keyboard to bring up the actionscript panel. You'll have to get used to this panel if you want to work in Flash; it's where you will do all the "behind the scenes" coding that makes the Flash file perform effects and things.
Now, you need to add in some code. First, make sure Flash will display your text word wrapped, or else the code will run out of the visible window. Press the button that looks like three horizontal lines stacked, with an arrow beside them; it's in the right corner of the actionscript panel, near the X in the corner and the minimise button. Now press "Word Wrap". Okay, now you can add your actionscript to the actionscript window:
Have you read the greyed-out lines? Do you think you understand them? The "var" command tells Flash that the subsequent commands are variables that can be switched around. Now, if you wonder how a novice can find all these commands, such as getSeconds and getHours, look at the plus button on the top of the actionscript panel, or the split window on your left in the actionscript panel. You'll see a list of regularly used commands, although not the ones I've used here. This menu may help you write your own actionscript in the future.
12) Okay, if you've done everything correctly and the script can talk to the textbox you labelled display_day, you should now be able to see it partly working when you preview the flash file. Save your document, then press Ctrl/apple and Enter (or go to the "Control" menu at the top of the screen, then press "Test Movie". If you get an error window pop up, you've done something wrong and you need to try these instructions again. If you get no errors but you can't see any numbers in the white window, you've got the textbox font the same colour as your white rectangle, so you need to click on the textbox and change the font colour to black. If, however, you see numbers in the white box, even though the digits might not make sense, you've achieved the first part of this tutorial. Well done!
13) Now comes the next part of the actionscript. We need to tell Flash to display the date and time in written words rather than numbers in the white box. So, close the preview window, then go back to your actionscript panel. Make sure you're in the correct layer, Layer 1, the one that already has actionscript in it (again, you move through layers by pressing the titles listed to the left of the Timeline). Underneath the text already in the actionscript panel, copy this on a new line:
Okay, now to explain that whacking great chunk of code... When Flash checks your system for the day and month, it brings back the answer in digits, since it doesn't understand the concept of days, months, hours, minutes, and seconds. Flash assigns the days of the week with a number; that's how it works. So, if Flash checks your system and discovers it's Sunday, it returns with a value of 0. If Flash finds it's Monday, it returns with a value of 1. And if Flash discovers it's Friday according to the system clock, it returns with a value of 5. Again, that's how it works. But we don't want the date written in numbers in our display panel. Thus, we have to tell Flash that if it returns with a value of 0 after checking what day it is, it should understand the result also means the word "Sunday". And we have to write the code out for each day of the week, so each number will represent a day.
And that's where the If statements come in. "If Flash returns with a value of 0 for the day, then day = "Sunday". See:
if (day==0){
day = "Sunday"
And == means "equals". So, when we write the word "day" in our future code, Flash will check what day it is, then convert the integer into the appropriate word.
The same happens for the months. Flash brings back a value of 0-11 for each of the 12 months. We then have to tell Flash to display these values as words. The code is pretty much the same as the one for days.
14) Save and preview the Flash file again. Remember, preview = Ctrl/apple and Enter. If you've done everything correctly, you should see they date displayed properly. Congratulations!
Now you're free to publish it and upload it to the web.
So, here is the 3rd lesson:
--------------------------------------------------------------------------
Flash Tutorial C - Date Widget
This is what we'll create. It looks simple, but as we get to tutorial two you should be able to add your own features to it:
Flash file: Date Widget
When you see the opening screen of Flash, click "Create New: Flash file (Actionscript 2.0). You'll see a white page. Now, always remember, this base page colour will stay the same no matter how many layers and levels (think "see-through sheets on top of a white page") you have. So, if, as you become more advanced, you choose to start adding more levels (see-through pages) on top of this base level, the bottom page in your stack will always determine your flash background colour.
Also, the layout of Flash CS3 may look daunting. But we'll get to each part as we go along. Once you get the hang of the program, you'll see it's fairly basic.
So, pick a background colour. Whatever you like. You'll find the colour setting at the bottom of the page, in the Properties panel. You could keep the page white, or try black or grey.
Here's the shapes labelled:
1) On the left in Flash you'll see a selection toolbar. These tools are used for creating 2D shapes and freehand lines and such. Press the one near the top called Rectangle Tool (R), since we want to create shape A. You'll see the bottom panel, called "Properties", change and give you options.
2) We want to make a big rectangular box to fill up the page. The "stroke color" in the Properties panel determines the colour of the border around your 2D shape; the "fill color" determines the colour of the rectangle. The other boxes change the width and style of your border, and the options under these are not needed. Pick a combination of colours, any you like. I've chosen a black border and a light purple fill, with a border of 3.
3) Draw a large box that covers most of your white page. If you aren't happy with it, go to the "Edit Menu" at the top and select "undo rectangle".
4) You may have noticed the Timeline panel at the top of your screen. If you've ever used professional animation software, or video software, you'll know what this does. It lets you create layers on top of other layers, and each of those tiny white blocks shows the keyframe (page) you're working on. For example, if you wanted to create an animation of a bouncing ball, you'd draw a ball in the first frame, then you'd create more keyframes and draw the ball in the air, then coming back down again. Then you'd play the Flash "movie" and see the ball bouncing as Flash displays each frame. You can play about with this in your own time.
Right now, you want to create a new layer, one that will sit above the rectangle you've drawn. This is because we want to create a new 2D shape – the white rectangle, shape B – and Flash cannot have two shapes on top of each other on the same level, or else the bottom shape loses whatever sat underneath the top shape. Go to the Insert menu at the top of your screen, then click Timeline, then Layer. You'll see a new layer (Layer 2) above your old one in the timeline. Press once on the name "Layer 2" at the side of the Timeline. This will ensure you're now working on the correct layer, and not Layer 1.
5) Now we want to create the next rectangle on this new layer, shape B, but with a white fill. This shape will be centred on top of the main rectangle, toward the top. Make it about 1-2cm high. It will be the box the date is displayed in. Click Create Rectangle on the side Selection panel/toolbar, then set the properties of it and create the shape.
6) Once you have the rectangle in place, press the Selection Tool on the left-hand selection toolbar. This stops your mouse from making more shapes if you accidentally press it down.
7) We want to add in code in, now, that tells Flash to check your computer's date and display it in the white rectangle we've created. In Flash, the term for writing code is called writing actionscript.
8) Now we need to create a textbox, which will display today's date in it once you've written the actionscript. On the toolbar selection panel on the left, select Text Tool. Set your ideal font and size and colour using the Properties panel at the bottom of your screen. Remember, the font must show up over the white rectangle behind, so pick a dark colour. I've chosen font size 20 and black and Garamond. Now, in the pulldown box beside this it lists types of textbox. You want "Dynamic Text", because this means the texboxt can perform functions when linked with actionscript. You also need to make sure the options are set to "Single Line" and that the font is "align centre". Now drag and draw a textbox on top of the white rectangle on your page.
9) Once you've drawn the shape (don't worry about getting it exactly centred over the white rectangle), go back to the properties panel. In the part labelled "Var:", you need a unique identifying name for your textbox (so your actionscript can see it). We'll call it display_day. If you want to move the textbox now, just press on it once and drag.
10) Okay, at this point you should save your file. Go to the File Menu, then select Save As. Call the document something like flash_tut.fla, and save it in a new folder, which makes it easier to upload the files later if you want to put them on the net.
11) Now we add the actionscript to tell Flash to check your PC and find the date, then display it in the textbox named display_day. So, in the Timeline panel, you'll see that the two layers each have a black dot in keyframe 1, with a red line that runs through both vertically. Press the black dot in frame 1 of Layer 1 to select it, because we will add the actionscript in that keyframe. Press F9 on your keyboard to bring up the actionscript panel. You'll have to get used to this panel if you want to work in Flash; it's where you will do all the "behind the scenes" coding that makes the Flash file perform effects and things.
Now, you need to add in some code. First, make sure Flash will display your text word wrapped, or else the code will run out of the visible window. Press the button that looks like three horizontal lines stacked, with an arrow beside them; it's in the right corner of the actionscript panel, near the X in the corner and the minimise button. Now press "Word Wrap". Okay, now you can add your actionscript to the actionscript window:
If you copy and paste this actionscript into the Actionscript window, you'll see the lines starting with // are greyed out. This is because the lines are notes for you to read and learn from, and Flash doesn't see them.
stop();
// This stop script stops Flash from playing every frame in this main timeline, assuming there's more than one frame. Think about it: If your computer keeps cycling through frame after frame, it's going to take up processing power. So, it's always best to stop Flash playing any unneeded clips repeatedly.
finddate = new Date();
var date = finddate.getDate();
// This tells Flash to create a variable named finddate, which will then get the date from the PC's internal clock. The second line does all the hard work – yet if you look at the code, it's pretty simple: the variable we've just created (finddate) goes off to get the system date, and for whatever result it finds, Flash will recognise the name"date" as the code word for "get the date of the PC's clock". So, when we want Flash to check the date in the future, all we have to do is tell it to perorm the function called "date" (since, if you see, I've put an equals sign in the code, to show the first part of the code in the bottom line connects with the second part in the bottom line).
var day = finddate.getDay();
var month = finddate.getMonth();
var year = finddate.getFullYear();
// These variables tell finddate to find the day, month, and year from the PC's clock.
display_day = ((day) + " " + (date) + " " + (month) + " " + (year));
// The final bit of code to add in tells Flash to write the day and date and month and year as names and display them in our display_day texbox in the order listed here (the pluses mean "and"; so, it's telling Flash to display this variable's result and then this other variable's result and then this other variable's result, etc.). Now, if you notice, the word "date" is in there. That's because I'm now telling Flash to perform the functions listed in the code (the ones called "day" and "date" and "month" and "year"). However, you may realise that as yet we have no code (variable) named "day" or "month" or "year". The only one we've told Flash to recognise is "date". So, when you preview your file, those parts won't work yet – only the variable called "date" will.
Have you read the greyed-out lines? Do you think you understand them? The "var" command tells Flash that the subsequent commands are variables that can be switched around. Now, if you wonder how a novice can find all these commands, such as getSeconds and getHours, look at the plus button on the top of the actionscript panel, or the split window on your left in the actionscript panel. You'll see a list of regularly used commands, although not the ones I've used here. This menu may help you write your own actionscript in the future.
12) Okay, if you've done everything correctly and the script can talk to the textbox you labelled display_day, you should now be able to see it partly working when you preview the flash file. Save your document, then press Ctrl/apple and Enter (or go to the "Control" menu at the top of the screen, then press "Test Movie". If you get an error window pop up, you've done something wrong and you need to try these instructions again. If you get no errors but you can't see any numbers in the white window, you've got the textbox font the same colour as your white rectangle, so you need to click on the textbox and change the font colour to black. If, however, you see numbers in the white box, even though the digits might not make sense, you've achieved the first part of this tutorial. Well done!
13) Now comes the next part of the actionscript. We need to tell Flash to display the date and time in written words rather than numbers in the white box. So, close the preview window, then go back to your actionscript panel. Make sure you're in the correct layer, Layer 1, the one that already has actionscript in it (again, you move through layers by pressing the titles listed to the left of the Timeline). Underneath the text already in the actionscript panel, copy this on a new line:
if (day==0){
day = "Sunday"
} else if (day==1){
day = "Monday"
} else if (day==2){
day = "Tuesday"
} else if (day==3){
day = "Wednesday"
} else if (day==4){
day = "Thursday"
} else if (day==5){
day = "Friday"
} else if (day==6){
day = "Saturday"
}
// Now an If statement is important here. When Flash checks what day it is, it returns with a number, say 0 for Sunday, 3 for Wednesday, and so on. We want Flash to display these numbers as names of the week for us instead. This If statement does that. I think the logic is pretty simple if you read it. You always start of saying "if [insert clause] is [insert state], then do whatever I suggest next". Then you use an Else If statement to say "yet if you find [insert function] instead, do whatever else I specify next". Finally, in some If statements you can use an "else" on the end, after the Ifs and the Else-ifs. The Else statement tells Flash, "If after you've checked these other If statements and none match what I want, do this other [insert function] instead".
if (month==0){
month = "January"
} else if (month==1){
month = "February"
} else if (month==2){
month = "March"
} else if (month==3){
month = "April"
} else if (month==4){
month = "May"
} else if (month==5){
month = "June"
} else if (month==6){
month = "July"
} else if (month==7){
month = "August"
} else if (month==8){
month = "September"
} else if (month==9){
month = "October"
} else if (month==10){
month = "November"
} else if (month==11){
month = "December"
}
// Ditto to the months. When Flash checks what month it is, it will return with a number to signal the month. These If statements force Flash to display them as names, not numbers.
display_day = ((day) + " " + (date) + " " + (month) + " " + (year));
// I've repeated this same line at the end here, so it will again display your computer's date – but with the written form of the words, not the digits. Without the code above, this line only makes Flash display numbers.
Okay, now to explain that whacking great chunk of code... When Flash checks your system for the day and month, it brings back the answer in digits, since it doesn't understand the concept of days, months, hours, minutes, and seconds. Flash assigns the days of the week with a number; that's how it works. So, if Flash checks your system and discovers it's Sunday, it returns with a value of 0. If Flash finds it's Monday, it returns with a value of 1. And if Flash discovers it's Friday according to the system clock, it returns with a value of 5. Again, that's how it works. But we don't want the date written in numbers in our display panel. Thus, we have to tell Flash that if it returns with a value of 0 after checking what day it is, it should understand the result also means the word "Sunday". And we have to write the code out for each day of the week, so each number will represent a day.
And that's where the If statements come in. "If Flash returns with a value of 0 for the day, then day = "Sunday". See:
if (day==0){
day = "Sunday"
And == means "equals". So, when we write the word "day" in our future code, Flash will check what day it is, then convert the integer into the appropriate word.
The same happens for the months. Flash brings back a value of 0-11 for each of the 12 months. We then have to tell Flash to display these values as words. The code is pretty much the same as the one for days.
14) Save and preview the Flash file again. Remember, preview = Ctrl/apple and Enter. If you've done everything correctly, you should see they date displayed properly. Congratulations!
Now you're free to publish it and upload it to the web.