Skip to content

Archive for March, 2007

29
Mar

Taking a Crack at Flash Components: DateField Components

Informal Tutorial – Intermediate

DateField Components

Learn how to calculate days in between two dates, using two DateField components.

After learning some basic concepts with the DateChooser component, it took me less time to build the ADG calculator using two DateField components. Unfortunately due to some legal concerns I can’t show my finished calculators just yet. I won’t bore you with the details, we’ll just get on with the tutorial.

Once again, using Flash Pro 8 and ActionScript 2.0 in an actions layer. For larger projects I would export the code, but for smaller ones it’s easier not to have to keep track of an .as file and a .swf file.

  1. Create three layers: Components, Text and Actions.
  2. In the Components layer, drop two DateField components onto the stage, instance names my_df and my_df2. Drag a plain button onto the stage. Give it an instance name of calculate_btn.
  3. In the Text layer, create a dynamic text field, instance name message_txt.
  4. In the Actions layer, add this code:
import mx.controls.*
var my_df:DateField;
calendar = new Object();
calendar.change = function(eventObj){
	date1 = my_df.selectedDate;
	year = date1.getFullYear();
	month = date1.getMonth();
	day = date1.getDate();
	my_df2.disabledRanges = [{rangeEnd: new Date(year, month, day)}];
}
my_df.addEventListener("change", calendar);

var my_df2:DateField;
calendar2 = new Object();
calendar2.change = function(eventObj){
	date2 = my_df2.selectedDate;
}
my_df2.addEventListener("change", calendar2);

function calculateIt(){
	totalDays = date2 - date1;
	numDays = Math.floor((date2 - date1)/86400000);
	trace("totalDays var = " + totalDays);
	trace("numDays var = " + numDays);
	message_txt.text = "Number of days in between are " + numDays;
}

calculate_btn.onRelease = function(){
		if(isNaN(date1) || isNaN(date2)){
			message_txt.text = "Pick dates.";
		}
		else{
			calculateIt();
		}
}

You can see some of the elements from the DateChooser tutorial are repeated here. Skipping down to the first new line of code:

my_df2.disabledRanges = [{rangeEnd: new Date(year, month, day)}];

this line prevents the second calendar from picking a time in the past of the first chosen date. Handy feature that is often seen when you book your flight/hotel/car online. The calculator button serves two purposes in this example. One is screening out a basic error and the other is to run the

calculateIt()

function. This function is has been simplified to the basics, but this is where you would put the more complex error checking and error messages in. Next we have:

	totalDays = date2 - date1;
	numDays = Math.floor((date2 - date1)/86400000);

While the first line does the actual calculation, the second puts it back into readable form – converting it from milliseconds to days. And that’s it! You can use this plain vanilla .fla for reference if you are having trouble.

Questions, comments and complaints are welcome. Let me know what I can do to improve this tutorial.

Related Posts:

27
Mar

Taking a Crack at Flash Components: the DateChooser Component

An Informal Tutorial – Beginner to Intermediate

Calculate time in the future

I’ve been working on two small calculators. One is a gestation calculator, that predicts the due date of a calf. The other is an Average Daily Gain calculator, that allows the user to plug in their animal’s data and see what the animal’s weight gain has been for a specific period in time.

While neither of these calculators have been refined, I’m posting my code in hopes that they will be a useful reference on how to get the DateField and DateChooser components to work for you. I have been unable to find any tutorials on how to use them and was forced to read up on the Date class and the DateField/DateChooser components.

DateChooser Component

I’m using Flash 8 Pro and ActionScript 2.0 in an actions layer.

  1. Build three layers: Components, Text, and Actions.
  2. On the Components layer, drop the DateChooser component onto the stage. In the properties panel name the instance my_dc.
  3. On the Text layer, add a dynamic text field, instance name message_txt.
  4. In the Actions layer, add: (line wraps marked with >>)
import mx.controls.*var my_dc:DateChooser;calendar =
>> new Object();calendar.change = function(eventObj){

eventDate = my_dc.selectedDate;

day = eventDate.getDate();

trace day;

month = eventDate.getMonth();

trace month;

year = eventDate.getFullYear();

trace year;

var endDate = new Date(year, month, (day+283));

message_txt.text = "Your lucky date is " +

>>(endDate.getMonth()+1) +"/"+ endDate.getDate()

>> +"/"+ endDate.getFullYear();

}my_dc.addEventListener("change", calendar);
import mx.controls.*

tells Flash that you want to import all the controls to the ActionScript library. I import all of them in case I am using multiple components for a project. If you only want the DateChooser component, you can use

import mx.controls.DateChooser

instead.

var my_dc:DateChooser;

I’m just now grasping the power of ActionScript 2 and strict datatyping. While it’s good practice to use it all the time, most of my projects are fairly simple. I generally only use it when I am bug hunting or have a larger project.

calendar = new Object();calendar.change = function(eventObj){
eventDate = my_dc.selectedDate;
}
my_dc.addEventListener("change", calendar);

This is how I grabbed the date from the DateChooser component. Every time a user picks a date, the event listener stores the info in the variable eventDate for my use. I manipulate the date here:

day = eventDate.getDate();
trace day;
month = eventDate.getMonth();
trace month;
year = eventDate.getFullYear();

trace year;

day, month, and year are there for the sake of brevity. I could leave them out, but then this

var endDate = new Date(year, month, (day+283));

would become this

var endDate = new Date(eventDate.getFullYear(),
>>eventDate.getMonth(), (eventDate.getDate()+283));

which is ugly and looks complicated. Then you take your results and spit them out in a place the user can read:

message_txt.text = "Your lucky date is " +
>>(endDate.getMonth()+1) +"/"+ endDate.getDate()
>> +"/"+ endDate.getFullYear();

I added some traces just so you can see what ActionScript will spit out for each variable. The date format is mm/dd/yyyy, for any non-U.S. visitors, and one is added to the ending date’s month because of standard array conventions – January is zero to ActionScript. [So January 27, 2007 would look like 0/27/2007 in the display, instead of 1/27/2007]. You can download the stripped-down .fla here if you are having trouble building your own.

Questions, corrections and comments are welcomed. I’ll tackle the DateField component next.

Related posts:

22
Mar

Pop Quiz! Do you know what it is?

I recently heard a second grader talking about learning how to use Microsoft Powerpoint in school. Wow. I know kids suck up technology like sponges, but how well are adults keeping up? Need a quick and easy reference? Here’s a cheat sheet for the rest of us.

Social sites
Generally a social site allows you to store, search, share, edit, and personalize content. Your friends can tag and/or comment on whatever content you posted.

Facebook
/MySpace: Social networking sites. What does that mean? It’s a profile about your favorite everything-under-the sun, a place for comments from friends, and basically a catch-all area for the narcissists.

Flickr: A place to store, search and share your photos.

YouTube/YFly: A place to store, search and share your home movies, video clips and music.

Twitter: Answers that all-important question: What are you doing? Log in, post your answer, and set it to answer on the phone, IM, or on the web site.

Geni: A fast and fun way to create your family tree. No, really! Share your account with your family members so they can help you build and expand upon the family tree. No trees need be harmed in the process!

Prosper: Another social site, but this time only interesting to the grown-ups. Prosper is a “people-to-people lending” site. You can sign up and become a borrower, a lender or both. How it works: a borrower asks for a loan with a starting interest rate. Lenders then bid on the loan. They can fund partial amounts or the full loan, and once a loan has become 100% funded, other lenders can win the bid by offering a lower interest rate. Prosper is interesting because it personalizes lending and borrowing. You borrow from real people. You lend to real people. Each loan is tied to a face and a story.

News sites
(or what is loosely construed as a news site)
Digg: News stories submitted by users. Articles are dugg up or dugg down, depending on its popularity. Each user has the ability to digg up or down on an article once. So if a story has 3,226 diggs, then over 3,226 read it and dugg it.

Technorati: “Who is saying what. Right now.” Search for the most recent news/articles posted on blogs, as they are updated.

Del.icio.us: Social bookmarking. Store, search and share your favorite bookmarks with your friends, family, co-workers or the world.

There are many others out there, waiting to be discovered. These are simply a few I continue to hear mentioned in podcasts and blogs. So there you have it, a sweet and short description for the rest of us.

15
Mar

Flash Resources

Some excellent Flash resources I found recently:

  • For the few female developers out there, I give you Flash Goddess: a resource and community site, showcasing women who work with Flash.
  • For the QA Testers, how about Flash Switcher and KewBee Plugin Switcher, so you no longer need three dinosaur PCs just to test your Flash project in an older Flash player?
  • For the Flash animators, check out Flash Sandy – an open source 3D API.
  • For anyone using ActionScript, try gotAPI when referencing Adobe’s LiveDocs. All the ActionScript classes and components documented in a clean and easy to use interface.

I’m currently building some calculators for work, using the DateField and DateChooser components. It’s difficult to understand how they work, since they contain pre-compiled code, but once I figure out how to manipulate them, I’m sure it’ll save me time on future projects. I have some prototypes without any set parameters finished calculators you can take a peek at: gestation calculator | ADG calculator.

Having trouble with the DateField or DateChooser components? Try my tutorials here:

6
Mar

Web 2.0 Expo

Hopefully things will slow down enough so I can get back to my regular posts. Until then, check out the Web 2.0 Expo that I’m hoping to attend.