Pass to the next image everyday - actionscript-3

I'm developing a small app in AS3.
Everyday, you can see a new image.
I've got all my image on a server.
Here is my code for the moment :
var imageLoader:Loader = new Loader();
var days:Array = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
var my_date:Date = new Date();
trace(days[my_date.day]);
startBtn.addEventListener(MouseEvent.CLICK, LoadImage, false, 0, true);
function LoadImage(event:MouseEvent):void{
var image:URLRequest = new URLRequest ("http://www.blabla.com/images/12.jpg");
imageLoader.load(image);
addChild (imageLoader);
}
Is it possible to tell the code the go the next image (13.jpg) the next day ? and everyday like that (next day = 14.jpg , next day = 15.jpg....ect).
Do you know a way to do that ?
Thank you very much for your help,

The code below should do it. I grabbed the getDayOfYear function from here: siafoo.net
I will post some optimizations to the function in a little while.
function getDayOfYear(date:Date):Number {
var monthLengths:Array = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
// A leap year is divisable by 4, but not by 100 unless divisable by 400.
if (((date.getFullYear() % 4 == 0) && (date.getFullYear() % 100 != 0)) || (date.getFullYear() % 400 == 0)) {
monthLengths[1] = 29;
}
var dayInYear = 0;
// get day of year up to month
for (var i:Number = 0; i < date.getMonth(); i++) {
dayInYear += monthLengths[i];
}
// add day inside month
dayInYear += date.getDate();
// Start counting on 0 (optional)
// dayInYear--;
return dayInYear;
}
function LoadImage(event:MouseEvent):void{
var image:URLRequest = new URLRequest ("http://www.blabla.com/images/"+getDayOfYear(new Date())+".jpg");
imageLoader.load(image);
addChild (imageLoader);
}

Related

trace the name of the day when clicking on calendar

I've downloaded a free code for a calendar that I've modified a little bit.
Currently, when I click on a square on the calendar, the "trace" tells me the date like "19 november 2015".
I would like to be able to trace the day of the week too. (like Thursday 19 novembre 2015).
Here's my code :
if(nM == 0){
var nM:int = 0;
}
var monthsOfYear:Array = new Array("Janvier", "February", "March", "April", "May", "June", "July", "August", "September", "October", "novembre", "decembre");
var daysOfMonths:Array = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var addMonths:int = (currentDate.getMonth()+nM);
var myDate = new Date(currentDate.getFullYear(), currentDate.getMonth()+nM);//provided by Andrei Firoiu
var startDay = myDate.getDay();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void{
calendar.month_txt.text = monthsOfYear[addMonths];
calendar.year_txt.text = " " + myDate.fullYear;
}
while (addMonths > 11)
{
addMonths = addMonths - 12;
}
while (addMonths < 0)
{
addMonths = addMonths + 12;
}
calendar.lM_mc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseClickEvent1);
function onMouseClickEvent1(event:Event){
nM = nM-1;
removeChild(holder_txt);
play();
calendar.month_txt.text = monthsOfYear[addMonths];
calendar.year_txt.text = " " + myDate.fullYear;
}
calendar.nM_mc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseClickEvent);
function onMouseClickEvent(event:Event){
nM = nM+1
removeChild(holder_txt);
play();
calendar.month_txt.text = monthsOfYear[addMonths];
calendar.year_txt.text = " " + myDate.fullYear;
}
function getDays(date:Date):uint {
return (myDate.getFullYear()%4 == 0 && myDate.getMonth() == 1 ? 29 : daysOfMonths[myDate.getMonth()]);
}
//-----------------------------
var myArray:Array = new Array();
//var spacing:int = 50;
var row:Number = 0;
var holder_txt:MovieClip = new MovieClip;
addChild(holder_txt);
holder_txt.x = 35;
holder_txt.y = 10;
//addChild(squares);
for (var t:int = 0; t < getDays(myDate); t++) {
myArray[t] = (t+1);
var textNum:String = myArray[t];
import box;
var square:MovieClip = new box();
holder_txt.addChild(square);
square.name = textNum
square.texter.text = textNum;
square.x = startDay *75
square.y = (row+1)*65
startDay++;
if(startDay >= 7){
startDay = 0;
row++;
}
square.mouseChildren = false;
square.addEventListener(MouseEvent.CLICK, squareClicked);
//square.addEventListener(MouseEvent.MOUSE_OVER, tab1Over);
square.addEventListener(MouseEvent.MOUSE_OUT, tab1Out);
//square.addEventListener(MouseEvent.MOUSE_DOWN,tab1Down);
//function tab1Over (e:Event):void{
// e.currentTarget.play();
// trace(e.currentTarget.name +" "+ monthsOfYear[addMonths]+" "+myDate.fullYear);
// }
function squareClicked (e:Event):void{
e.currentTarget.play();
trace(e.currentTarget.name +" "+ monthsOfYear[addMonths]+" "+myDate.fullYear);
mySharedObject.data.theDate = e.currentTarget.name +" "+ monthsOfYear[addMonths]+" "+myDate.fullYear;
trace(mySharedObject.data.theDate);
mySharedObject.data.theCity = "Noumea";
}
function tab1Out (e:Event):void{
e.currentTarget.gotoAndStop(1);}
}
Thx for your help,
This is very often asked and search should show many answers for it. But, you can use getDay():
var weekDayLabels:Array = new Array("Sunday","Monday", "Tuesday","Wednesday","Thursday","Friday","Saturday");
var someBirthday:Date = new Date(1974, 10, 30, 1, 20);
trace(someBirthday); // Sat Nov 30 01:20:00 GMT-0800 1974
trace(someBirthday.getDay()); // 6
trace(weekDayLabels[someBirthday.getDay()]); // Saturday

Create AS3 countdown clock without using users system Clock

I need to create an as3 countdown clock for a TV display with no system clock.
How can I utilize the Date object for this?
Here is my code so far:
var targetDate:Date = new Date(2015, 6, 5, 19, 00, 00);
var dateStr:Date = new Date(2015, 5, 25, 18, 56, 00);
addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event):void{
var nowDate:Date = new Date(dateStr);
var ms:Number = targetDate.getTime() - nowDate.getTime();
var sec:Number = Math.floor(ms/1000);
var min:Number = Math.floor(sec/60);
var hr:Number = Math.floor(min/60);
var day:Number = Math.floor(hr/24);
sec = sec % 60;
min = min % 60;
hr = hr % 24;
daytxt.text = day.toString();
hrtxt.text = (hr < 10) ? "0"+hr.toString() : hr.toString();
mintxt.text = (min < 10) ? "0"+min.toString() : min.toString();
sectxt.text = (sec < 10) ? "0"+sec.toString() : sec.toString();
//sec--;
trace(dateStr);
}
I'm trying to pass in the date parameters to the Date() constructor, but I cannot get it to count down.
Instead of using enterframe it's better to use Timer. You should set it for a 1000 millisecond ie one second . Here is a good tutorial for creating a timer.

AS3 day of week shuffle

Having some issues in code. I am trying to shuffle days of week in four dynamic text boxes so if today is thursday other box shows friday and other after that shows saturday, sunday... And the days shuffle but when its comes to sunday my code shows null instead day name, where did i go wrong? here is a code:
var dayOfWeek_array:Array = new Array("Sunday", "Monday", "Tuesday", "Wensday", "Thursday", "Friday", "Saturday");
var today_date:Date = new Date();
var day_str:String = dayOfWeek_array[today_date.getDay()+0];
var day_str1:String = dayOfWeek_array[today_date.getDay()+1];
var day_str2:String = dayOfWeek_array[today_date.getDay()+2];
var tmp1 = today_date.getDay() + 3;
if(tmp1 > 6) tmp -= 7;
var day_str3:String = dayOfWeek_array[tmp];
var tmp = today_date.getDay() + 4;
if(tmp > 6) tmp -= 7;
var day_str4:String = dayOfWeek_array[tmp];
myTextField1.text = (""+day_str1);
myTextField2.text = (""+day_str2);
myTextField3.text = (""+day_str3);
myTextField4.text = (""+day_str4);
I would point you to the modulo operator: % http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#modulo
Using that you can do something like dayOfWeek_array[(today_date.getDay()+0) % 7];
This will always return number between 0 and 6, so, you won't get nulls in your text fields
Different story is, that I would put your textfields in array, so you can manage them using cycles. With that, you can easily change behaviour or number of fields in the future.
You should learn how to reuse code. You don't have logic for situations, when index is out of bounds.
Here is small example how to create utility function, that will return element of the array with offset:
var days:Array = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var today:Date = new Date();
//Test
trace(offset(today.day, days)); //Thursday
trace(offset(today.day, days, 1)); //Friday
trace(offset(today.day, days, -1)); //Wednesday
trace(offset(today.day, days, 8)); //Friday
trace(offset(today.day, days, -8)); //Wednesday
function offset(position:int, items:Array, offset:int = 0):Object {
var size:int = items.length;
//Apply offset
position += offset % size;
if (position < 0) {
position += size;
} else if (position >= size) {
position %= size;
}
return items[position];
}

Countdown timer not working in AS3

Since Im new in AS3 and I just converted AS2 to AS3. The countdown doesnt work. Right now the 4 digits are looping all in the same time very fast (The three digits animation is fine - ignore it)
See countdown - http://magnixsolutions.com/clients/OT/media-buys_scoreboard-redux_160x600_5-8-2009.html
AS3
// get the current date and time as it exists at
// this instance in time when the frame is entered
var currentDate:Date = new Date();
var thisYear:int = currentDate.getFullYear();
var thisMonth:int = currentDate.getMonth();
var thisDate:int = currentDate.getDate();
var thisHour:int = currentDate.getHours();
var thisMinute:int = currentDate.getMinutes();
var thisSecond:int = currentDate.getSeconds() + 12;
var thisMSecond:int = currentDate.getMilliseconds();
// Date( year, month-1, date [, hour [, minute [, second [, millisecond]]]])
var eventDate = new Date(thisYear, thisMonth, thisDate, thisHour, thisMinute, thisSecond, thisMSecond);
var eventMillisecs = eventDate.getTime();
// get the current date and time as it exists at
// this instance in time when the frame is entered
this.addEventListener(TimerEvent.TIMER, enterFrameHandler);
function enterFrameHandler() {
currentDate = new Date();
var currentMillisecs = currentDate.getTime();
this.msecs = eventMillisecs - currentMillisecs;
if (this.msecs <= 0){
play();
return;
}
// if the date hasn't been reached, continue to
// devise seconds, minutes, hours and days from
// the calculated milliseconds
this.secs = Math.floor(this.msecs/1000); // 1000 milliseconds make a second
this.mins = Math.floor(this.secs/60); // 60 seconds make a minute
this.hours = Math.floor(this.mins/60); // 60 minutes make a hour
this.days = Math.floor(this.hours/24); // 24 hours make a second
this.msecs = int(this.msecs % 1000);
this.secs = int(this.secs % 60);
this.mins = int(this.mins % 60);
this.hours = int(this.hours % 24);
this.days = int(this.days);
while (this.msecs.length < 3) this.msecs = "0" + this.msecs;
if (this.secs.length < 2) this.secs = "0" + this.secs;
if (this.mins.length < 2) this.mins = "0" + this.mins;
if (this.hours.length < 2) this.hours = "0" + this.hours;
while (this.days.length < 3) this.days = "0" + this.days;
for(var movie in this){
if (this[movie]._parent == this) this[movie].evaluateFrameFrom(this);
}
};
MovieClip.prototype.evaluateFrameFrom = function(variableClip){
var nameArray = this._name.split("_");
var numberSet = variableClip[nameArray[0]];
var character:int = parseInt(nameArray[1]);
var frame = 1 + parseInt(numberSet.charAt(character));
if (this._currentframe != frame) this.gotoAndStop(frame);
};
This is probably what you mean:
// There is no number type in AS3. Use parseInt to cast string to int
var character:int = parseInt(nameArray[1]);
var frame = 1 + parseInt(numberSet.charAt(character));
Also, there's no such thing as _root in ActionScript 3.0. Try this:
this.avgscore_mc.gotoAndPlay(2);
And you need to add your enterFrame like this:
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
function enterFrameHandler() {
// Stuff in your enter frame
}
Sounds like you're still thinking AS 2.0!

1 Series with multiple not connected lines?

How could be achieved the chart drawn bellow with 1 Chart Series, which can represent multiple not connected, and if possible not sequential ( on the X axis ) lines ?
var sprite:Sprite = new Sprite();
drawChart([ [0,50], [70, 100], [120, 190] ], sprite);
private function drawChart(data:Array, sprite:Sprite):void {
var grachics:Graphics = sprite.graphics;
grachics.beginFill(0xffff00);
for (var i:int = 0; i < data.length; i++) {
var obj:Array = data[i] as Array;
var startX:int = obj[0];
var endX:int = obj[1];
grachics.drawRect(startX, 0, endX - startX, 30);
}
graphics.endFill();
}