PDA

View Full Version : Dynamically built calendar using AS3


Sparkmasterflex
11-05-2009, 09:29 PM
Hello all,

I have been fighting with this for nearly a month, (My wife is tired of hearing about it) and have been avoiding putting the question onto this forum because it feels like it's just within my reach. But obviously it isn't.

I have the code set up to dynamically build this calendar. It works for the month we're in but attempting to go to the previous or next month does not work. Sometimes next month works actually.

Ok I just checked it and it really only worked for October and now it is incorrect for November. The problem is determining the first and last days (i.e. Sunday, Monday, etc...) of the month. Here is my code so far:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;

//these give the x position of each date box for the calendar//
var Sunday:int = 10;
var Monday:int = 75;
var Tuesday:int = 140;
var Wednesday:int = 205;
var Thursday:int = 270;
var Friday:int = 335;
var Saturday:int = 400;

//these give the y position of each date box for the calendar//
var week1:int = 40;
var week2:int = 105;
var week3:int = 170;
var week4:int = 235;
var week5:int = 300;
var week6:int = 365;

var format:TextFormat = new TextFormat();
format.color = 0xffffff;
var dayArray:Array = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthArray:Array = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var xPosArray:Array = new Array(Sunday,Monday,Tuesday,Wednesday,Thursday,Fri day,Saturday);
var yPosArray:Array = new Array(week1,week2,week3,week4,week5,week6);
var daysInMonth:int;
var dateInMonth:int;

var firstDay:int;
var lastDay:int;
var date_mc:MCcalDate;
var entry:MCinsert;
var dateMC:MovieClip = new MovieClip();
var nextMonth:MConeMonth;
var prevMonth:MConeMonth;
var nextYear:MConeYear;
var prevYear:MConeYear;
var dOW:TextField;
var todaysDate:Date = new Date;
var date = todaysDate.getDate();
var day = todaysDate.getDay();
var year = todaysDate.getFullYear();
var month = todaysDate.getMonth();
var month2:int;
var intDate:int;
var intDay:int;
var d:int;
var w:int;
var addTask:Boolean = false;
var calArray:Array;

var alphaTween:Tween;
var scaleTween:Tween;
var scaleYTween:Tween;
var dayTween:Tween;

function getDate(event:Event):void {
//todaysDate = new Date();
month = todaysDate.getMonth();
day = todaysDate.getDay();
year = todaysDate.getFullYear();
date = todaysDate.getDate();
trace(dayArray[day] + " " + monthArray[month] + " " + date);
intDate = parseInt(date);
intDay = parseInt(day);
for (var WD:int = 0; WD<dayArray.length; WD++) {
dOW = new TextField();
dOW.defaultTextFormat = format;
dOW.text = dayArray[WD];
stage.addChild(dOW);
dOW.x = xPosArray[WD] + 3;
dOW.y = 25;
}

if (month == 1) {
if (year%4 == 0) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
} else if (month == 3 || month == 5 || month == 8 || month == 10) {
daysInMonth = 30;
} else {
daysInMonth = 31;
}
dateInMonth = daysInMonth;

for (var i:int=0; daysInMonth>=7; i++) {
daysInMonth = daysInMonth - 7;
}

firstDay = daysInMonth;
trace(firstDay);
d = firstDay;
w = 0;
nextMonth = new MConeMonth();
prevMonth = new MConeMonth();
nextYear = new MConeYear();
stage.addChild(nextMonth);
stage.addChild(prevMonth);
//stage.addChild(nextYear);
nextMonth.x = 340;
nextMonth.y = 7;
prevMonth.x = 200;
prevMonth.scaleX = -1;
prevMonth.y = 7;
//nextYear.buttonMode = true;
nextMonth.buttonMode = true;
nextMonth.addEventListener(MouseEvent.CLICK, oneMonth);
prevMonth.buttonMode = true;
prevMonth.addEventListener(MouseEvent.CLICK, previousMonth);
//nextYear.addEventListener(MouseEvent.CLICK, oneYear);
stage.addChild(dateMC);
buildCalendar(null);
}

function buildCalendar(event:Event):void {
for (var j:int=0; j<dateInMonth; j++) {
if (d == 7) {
d = 0;
w++;
}
date_mc = new MCcalDate();
dateMC.addChild(date_mc);
date_mc.name = j.toString();
calArray = new Array();
calArray.push(date_mc);
date_mc.x = xPosArray[d];
date_mc.y = -5;
date_mc.dateCal_txt.text = (j + 1).toString();
d++;
dayTween = new Tween(date_mc,"y",None.easeNone,-5,yPosArray[w],.4,true);

date_mc.addEventListener(MouseEvent.CLICK, dateSelected);
}
test_txt.text = monthArray[month] + " " + year;
}

function oneMonth(event:Event):void {
var j:int= dateMC.numChildren;
while (j--) {
dateMC.removeChildAt(j);
}
if (d == 7) {
w = -1;
} else {
//trace(d);
w = 0;
}
todaysDate.month += 1;
day = todaysDate.getDay();
getDate(null);
}


function previousMonth(event:Event):void {
var j:int= dateMC.numChildren;
while (j--) {
dateMC.removeChildAt(j);
}
if (d == 7) {
w = -1;
} else {
//trace(d);
w = 0;
}

todaysDate.month -= 1;

getDate(null);
}

////// For the event and task entry window //////////
function dateSelected(event:Event):void {
entry = new MCinsert();
dateMC.addChild(entry);
entry.name = event.target.name;
//trace(entry.name);
entry.alpha = 0;
entry.scaleX = .1;
entry.scaleY = .1;
entry.x = 275 - (entry.width / 2);
entry.y = 225 - (entry.height / 2);
alphaTween = new Tween(entry,"alpha",None.easeNone,0,1,.5,true);
scaleTween = new Tween(entry,"scaleX",None.easeNone,.1,1,.5,true);
scaleYTween = new Tween(entry,"scaleY",None.easeNone,.1,1,.5,true);
entry.name = event.target.name;
entry.submit_btn.addEventListener(MouseEvent.MOUSE _OVER, entry.overBtn);
entry.submit_btn.addEventListener(MouseEvent.MOUSE _OUT, entry.outBtn);
entry.submit_btn.addEventListener(MouseEvent.CLICK , entry.submit);
entry.clear_btn.addEventListener(MouseEvent.MOUSE_ OVER, entry.overBtn);
entry.clear_btn.addEventListener(MouseEvent.MOUSE_ OUT, entry.outBtn);
entry.clear_btn.addEventListener(MouseEvent.CLICK, entry.clearText);
entry.close_btn.addEventListener(MouseEvent.MOUSE_ OVER, entry.overBtn);
entry.close_btn.addEventListener(MouseEvent.MOUSE_ OUT, entry.outBtn);
entry.event_cb.addEventListener(Event.CHANGE, entry.cb_change);
entry.task_ch.addEventListener(Event.CHANGE, entry.ch_change);
entry.close_btn.addEventListener(MouseEvent.CLICK, closeEntry);
}

function closeEntry(event:Event):void
{
alphaTween = new Tween(entry,"alpha",None.easeNone,1,0,.5,true);
scaleTween = new Tween(entry,"scaleX",None.easeNone,1,.1,.5,true);
scaleYTween = new Tween(entry,"scaleY",None.easeNone,1,.1,.5,true);
scaleYTween.addEventListener(TweenEvent.MOTION_FIN ISH, removeBox);
}

function removeBox(event:Event):void
{
scaleYTween.removeEventListener(TweenEvent.MOTION_ FINISH, removeBox);
dateMC.removeChild(entry);
}
////////////////////////////////////////////////////////////////

getDate(null);

dateMC.addEventListener("closeThis",closeEntry);

There is also code in here to open a window for the user to enter in events or tasks. That is working so far except for the whole sharedObject. Not that it's not working but I need to think about how to make a sharedObject for each date w/o actually making one for each date.

Thank you in advance.