Skip to main content
JonnyDL-HV74P1
Inspiring
March 18, 2010
Question

Date to rotate objects?

  • March 18, 2010
  • 1 reply
  • 287 views

I created a script that will rotate a different ad every day, based on a comparison of the current date & the ads assigned day. The bug in the script is that the number of ads vs the number of days may be odd, so the last one or two ads will miss a (days) rotation every month. Is there a way I can get this to work so every ad hits its rotation.

var displayAdOne:Array=new Array;

var displayAdTwo:Array=new Array;

var displayAdThree:Array=new Array;

var displayAdFour:Array=new Array;

var showAd:Loader = new Loader();

displayAdOne=[adOne, 1, 5, 9, 13, 17, 21, 25, 29];

displayAdTwo=[adTwo, 2, 6, 10, 14, 18, 22, 26, 30];

displayAdThree=[adThree, 3, 7, 11, 15, 19, 23, 27, 31];

displayAdFour=[adFour, 4, 8, 12, 16, 20, 24, 28];

var loopit = displayAdOne.length + displayAdTwo.length + displayAdThree.length + displayAdFour.length

//trace(loopit)

var theDate:Date = new Date;

var triggerDate=theDate.getDate();

trace(triggerDate)

for (var i=1; i<=9; i++)

{

if (triggerDate==displayAdOne)

{

showAd.load(adOne);

break;

}

if (triggerDate==displayAdTwo)

{

showAd.load(adTwo);

break;

}

if (triggerDate==displayAdThree)

{

showAd.load(adThree);

break;

}

if (triggerDate==displayAdThree)

{

showAd.load(adFour);

break;

}

addChild(showAd);

}

Thanks for any help given!

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
March 18, 2010

This is just a thought that hasn't gone beyiond itself yet.  Rather than basing the rotation on the day of the month, you could base it on the number of days relative since some fixed starting day and then use the modulus operator to get the remainder relative to four day cycles:  daysSinceStart%4 will yield a value of 0 thru 3;  Each day it will advance one and reset to zero every 4th day.  I think with this approach every ad will get equal billing regardless of what day it is.  And if you add more ads you can just change the value from 4 to the number of ads.