IamFace
01-25-2009, 02:54 AM
Hey fellow developers,
I have started working on building a calendar class. I'm working on getting the number of days in the month, and what day it starts on, which I have fine. I run a loop that draws a new Sprite, adds it to the stage for each day.
It works perfect right now only because I am using a static number, but would like to have something like box.width rather than 31.
I believe I have just looking at it too long and am overlooking something simple. I would like a second set of eyes to see how I can position my code to run properly. The line var boxX:int = 80+(31*firstDay); is where I would like it to say var boxX:int = 80+(box.width*firstDay); but the line hasn't been drawn at this time if I just put it in because the each Sprite needs to be drawn in the loop.
Can anyone see how to restructure the function/loop for it to run right?
Thanks in advance!
// returns first day of the month
function theFirstDay(year:int, month:int):int
{
return new Date(year, month).getDay();
}
var firstDay = theFirstDay(theYear, theMonth);
// returns number of days in the month
function numDaysMonth(year:int, month:int):int
{
return new Date(year, month, 0).getDate();
}
var numDays = numDaysMonth(theYear, theMonth);
function makeGrid()
{
var box:Sprite;
var boxX:int = 80+(31*firstDay);
var boxY:int = 80;
for(var i:int=1; i<numDays+1; i++)
{
box = new Sprite;
box.graphics.lineStyle(1, 0x000000);
box.graphics.drawRect(0, 0, 25, 25);
box.x = boxX;
box.y = boxY;
addChild(box);
boxX += box.width + 5;
switch(i+firstDay)
{
case 7:
case 14:
case 21:
case 28:
case 35:
boxX = 80; boxY += box.height + 5; break;
}
}
}
makeGrid();
I have started working on building a calendar class. I'm working on getting the number of days in the month, and what day it starts on, which I have fine. I run a loop that draws a new Sprite, adds it to the stage for each day.
It works perfect right now only because I am using a static number, but would like to have something like box.width rather than 31.
I believe I have just looking at it too long and am overlooking something simple. I would like a second set of eyes to see how I can position my code to run properly. The line var boxX:int = 80+(31*firstDay); is where I would like it to say var boxX:int = 80+(box.width*firstDay); but the line hasn't been drawn at this time if I just put it in because the each Sprite needs to be drawn in the loop.
Can anyone see how to restructure the function/loop for it to run right?
Thanks in advance!
// returns first day of the month
function theFirstDay(year:int, month:int):int
{
return new Date(year, month).getDay();
}
var firstDay = theFirstDay(theYear, theMonth);
// returns number of days in the month
function numDaysMonth(year:int, month:int):int
{
return new Date(year, month, 0).getDate();
}
var numDays = numDaysMonth(theYear, theMonth);
function makeGrid()
{
var box:Sprite;
var boxX:int = 80+(31*firstDay);
var boxY:int = 80;
for(var i:int=1; i<numDays+1; i++)
{
box = new Sprite;
box.graphics.lineStyle(1, 0x000000);
box.graphics.drawRect(0, 0, 25, 25);
box.x = boxX;
box.y = boxY;
addChild(box);
boxX += box.width + 5;
switch(i+firstDay)
{
case 7:
case 14:
case 21:
case 28:
case 35:
boxX = 80; boxY += box.height + 5; break;
}
}
}
makeGrid();