I have built a class based on a calendar script found here on this forum. I have modified the script to work in a class and expanded on the calendar by checking each day of the month against a database to see if there are events on that day. (if there is an event, the day movie clip goes to another frame to highlight said day)
Everything works great… at first glance. The first time the calendar loads, all the days are properly in place and the Event.ADDED_TO_STAGES runs correctly, highlighting the dates with events.
The problem occurs when the user changes the month. Everything again works the way it should except for the Event.ADDED_TO_STAGE. This event only runs for each day the first time the calendar is loaded. Why does it not work after a user changes the month?? I’m getting so frustrated, i can cry.
package classes {
//import necessary classes
import flash.events.*;
import flash.display.MovieClip;
import flash.utils.getDefinitionByName;
import flash.external.ExternalInterface;
import flash.net.*;
import flash.system.Security;
// eventsCalendar Class
public class eventsCalendar extends MovieClip {
//* definitions *//
//variables and arrays
var currentDate:Date = new Date();
var monthsOfYear:Array = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var daysOfMonths:Array = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var addMonths:int;
var myDate:Date;
var startDay:int;
var nM:int = 0;
//movieclips
var header_clip:MovieClip;
var days_clip:MovieClip;
//load date information into variables and start initial state
public function eventsCalendar():void {
//set variable to display initial month
if(nM == 0){
nM = 0;
}
//Run method to add header including month, year, arrow buttons, and days of the week
setHeader();
//run method to setup calendar according to month
setMonth(nM);
}
//method to set header
private function setHeader():void {
//attach header clip
import mcHeader;
header_clip = new mcHeader();
addChild(header_clip);
header_clip.x = 5;
header_clip.y = 5;
//set next and previous month button actions
header_clip.lM_mc.addEventListener(MouseEvent.MOUSE_DOWN, goLastMonth);
header_clip.nM_mc.addEventListener(MouseEvent.MOUSE_DOWN, goNextMonth);
}
//method to set the month
private function setMonth(nM):void {
//create new date, get day of the week to start
addMonths = (currentDate.getMonth()+nM);
myDate = new Date(currentDate.getFullYear(), currentDate.getMonth()+nM);
startDay = myDate.getDay();
//ensure correct month
while (addMonths > 11)
{
addMonths = addMonths - 12;
}
while (addMonths < 0)
{
addMonths = addMonths + 12;
}
//set month and year
header_clip.month_txt.text = monthsOfYear[addMonths];
header_clip.year_txt.text = " " + myDate.fullYear;
//method to set the days
setDays();
}
//previous month button actions
private function goLastMonth(event:Event){
nM = nM-1;
removeChild(days_clip);
play();
//change month
setMonth(nM)
}
//next month button actions
private function goNextMonth(event:Event){
nM = nM+1
removeChild(days_clip);
play();
//change month
setMonth(nM)
}
//set new clip for days and add day squares
private function setDays():void {
//variables and arrays
var myArray:Array = new Array();
var row:Number = 0;
//add days holder
days_clip = new MovieClip;
addChild(days_clip);
days_clip.x = 35;
days_clip.y = 10;
//add a square for each day of the month
for (var t:int = 0; t < getDays(myDate); t++) {
//variables and arrays
myArray[t] = (t+1);
var dayNum:String = myArray[t];
//Variables to pass on
var yearNum:int = myDate.getFullYear();
var monthNum:int = myDate.getMonth()+1;
// attach square
import box;
var day:MovieClip = new box();
days_clip.addChild(day);
day.name = yearNum + "-" + leadingZero(monthNum) + "-" + leadingZero(Number(dayNum));
day.texter.text = dayNum;
day.x = startDay *75
day.y = (row+1)*65
startDay++;
if(startDay >= 7){
startDay = 0;
row++;
}
//events for the day square
day.mouseChildren = false;
day.addEventListener(MouseEvent.MOUSE_OVER, dayOver);
day.addEventListener(MouseEvent.MOUSE_OUT, dayOut);
day.addEventListener(Event.ADDED_TO_STAGE, onDayReady(yearNum, monthNum, Number(dayNum)));
day.addEventListener(MouseEvent.MOUSE_DOWN, printDate(yearNum, monthNum, Number(dayNum)));
}
}
private function dayOver (e:Event):void {
}
private function dayOut (e:Event):void {
}
private function printDate (year:int, month:int, day:int){
return function (e:Event){
trace(year + "-" + leadingZero(month) + "-" + leadingZero(day));
}
}
private function onDayReady(year:int, month:int, day:int) {
return function (e:Event){
trace("Checking " +day+ " to see if there is an event");
//set variables
var variables:URLVariables = new URLVariables();
variables.date = year + "-" + leadingZero(month) + "-" + leadingZero(day);
//send request to php file
var request:URLRequest = new URLRequest("http://localhost/script/getEvents.php");
request.method = URLRequestMethod.POST;
request.data = variables;
//load data into loader
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
//attach event handler
loader.addEventListener(Event.COMPLETE, dataLoaded);
//error catch
try{
loader.load(request);
}
catch (error:Error) {
trace("Unable to load URL");
}
}
}
//method to handle returned data
private function dataLoaded(e:Event):void {
//set loaded variables into vars
var vars:URLVariables = new URLVariables(e.target.data);
var count:int = vars["count"];
var thisdate:String = vars["thisdate"];
var race:String = vars["race"];
//trace(count + ", date: " + thisdate + ", race: " + race);
//if no data is returned for the date, do nothing
if (count == 0) {
//do nothing
}
else if (count == 1) {
//set date clip to correct color
var calendarday_ = days_clip.getChildByName(thisdate);
calendarday_.gotoAndStop(4);
}
}
//methods to handle date management
//add leading zero
private function leadingZero(num:Number):String {
if(num < 10) {
return "0" + num;
}
return num.toString();
}
//get number of days in the month accounting for leap years.
private function getDays(date:Date):uint {
return (myDate.getFullYear()%4 == 0 && myDate.getMonth() == 1 ? 29 : daysOfMonths[myDate.getMonth()]);
}
}
}