I have the following code that creates an appointment that recurs 6 times. This is working nicely, however, it would be great if I could access the number of the recurrence in a variable and print that in the title as "appointment 1", "appointment 2", etc. Is this possible with the addWeeklyRule method or would it only be possible with a loop?
var eventSeries = cal3.createEventSeries(name, startTime, endTime, CalendarApp.newRecurrence().addWeeklyRule().times(6), {
description: descriptionText
Found a way to do it without a method but just got the events and changed the title. Had to add a wait step though because it couldn't find the events right after creating them.
Utilities.sleep(5000);
var untilPlanned = new Date(startTime.getTime() + (1000 * 60 * 60 * 24 * 7 * 8));
var startPlanned = new Date(startTime.getTime() - (1000 * 60 * 60 * 12));
//search for events
var eventsPlanned = cal3.getEvents(startPlanned, untilPlanned, {
search: descriptionText
});
for (var k = 0; k < eventsPlanned.length; k++) {
var sessionNo = k + 1;
var title = Sessie: " + sessionNo + "
eventsPlanned[k].setTitle(title);
}
Related
I have a Timer that I am trying to get to countdown instead of up. I worked with this code years ago and can't seem to figure out how to make it count down from 5 minutes.
I've only tried messing with the variables.
Here are my variables etc:
tCountTimer = new Timer(100);
tCountTimer.addEventListener(TimerEvent.TIMER, timerTickHandler);
timerCount = 0;
tCountTimer.start();
Here are my Functions to Count:
private function timerTickHandler(e:Event):void
{
timerCount += 100;
toTimeCode(timerCount);
}
private function toTimeCode(milliseconds:int):void
{
this.milliseconds = milliseconds;
//create a date object using the elapsed milliseconds
time = new Date(milliseconds);
//define minutes/seconds/mseconds
minutes = String(time.minutes + 5);
seconds = String(time.seconds);
miliseconds = String(Math.round(time.milliseconds) / 100);
//add zero if neccecary, for example: 2:3.5 becomes 02:03.5
minutes = (minutes.length != 2) ? '0'+ minutes : minutes;
seconds = (seconds.length != 2) ? '0' + seconds : seconds;
//display elapsed time on in a textfield on stage
playScreen.timeLimitTextField.text = minutes + ":" + seconds;
}
It counts up perfect but just can't get it to time down from 5 minutes. All help is much appreciate.
Didn't test that, but I hope the idea is absolutely clear.
// Remember the time (in milliseconds) in 5 minutes from now.
var endTime:int = getTimer() + 5 * 60 * 1000;
// Call update function each frame.
addEventListener(Event.ENTER_FRAME, onFrame);
function onFrame(e:Event):void
{
// How many milliseconds left to target time.
var aTime:int = endTime - getTimer();
// Fix if we are past target time.
if (aTime < 0) aTime = 0;
// Convert remaining time from milliseconds to seconds.
aTime /= 1000;
// Convert the result into text:
// aTime % 60 = seconds (with minutes stripped off)
// aTime / 60 = full minutes left
var aText:String = ze(aTime / 60) + ":" + ze(aTime % 60);
// Do whatever you want with it.
trace(aText);
}
// Function to convert int to String
// and add a leading zero if necessary.
function ze(value:int):String
{
return ((value < 10)? "0": "") + value.toString();
}
I have a "clock" counting down the time of the game (from 9 seconds to 0 seconds). I have a button that pauses the game, so I want to freeze de clock and when I click to play, the game continuos from that second that I frozen. But I don't have any idea how to do this! Can you help me please?
this.addEventListener(Event.ENTER_FRAME, updateTimer);
function updateTimer(e:Event){
var t:uint = getTimer();
var dt:uint = 9000 - t;
var totseconds:uint = Math.floor(dt/1000);
var minutes:uint = Math.floor(totseconds/60);
var seconds:uint = Math.floor(totseconds%60);
var minsWithZ = minutes < 10 ? "0" + minutes : minutes;
var secsWithZ = seconds < 10 ? "0" + seconds : seconds;
var time = minsWithZ + ":" + secsWithZ;
this.tempo_txt.text = time;
}
pausa.addEventListener(MouseEvent.MOUSE_UP, menuPause);
function menuPause(e:MouseEvent){
...somegame code...
voltar.addEventListener(MouseEvent.MOUSE_UP, play);
}
function play (e:MouseEvent){
...some game code
}
You have two choices at least:
1-Andre's answer;
2-my answer:
import flash.events.Event;
var dt:int = 9000
var diff:Number=0;
var prevDiff:Number=0;
var currentTime:Number=0;
var t:uint=0;
var totseconds:int=0;
var minutes:int=0;
var seconds:int=0;
var minsWithZ;
var secsWithZ;
var running:Boolean=true;
var time;
this.addEventListener(Event.ENTER_FRAME, updateTimer);
function updateTimer(e:Event){
if(dt<1){dt=1;menuPause(null)}
trace(running);
totseconds= Math.floor(dt/1000);
minutes= Math.floor(totseconds/60);
seconds= Math.floor(totseconds%60);
minsWithZ = minutes < 10 ? "0" + minutes : minutes;
secsWithZ = seconds < 10 ? "0" + seconds : seconds;
time = minsWithZ + ":" + secsWithZ;
this.tempo_txt.text = time;
if(running){
t= getTimer()-diff-prevDiff;
dt=5000- t;
}else{
diff=getTimer()-currentTime;
}
}
pausa.addEventListener(MouseEvent.MOUSE_UP, menuPause);
voltar.addEventListener(MouseEvent.MOUSE_UP, reset);
function menuPause(e:Event){
running=false;
currentTime=getTimer();
prevDiff+=diff;
}
function menuPlay (e:Event){
running=true;
}
function reset(){
running=true;
prevDiff+=9000;
}
A little explaination on code:
getTimer() returns the miliseconds from when you opened the app and
You can't Pause "getTimer()"
but you can get the time that game is paused and subtract from getTimer().
variable "diff" does that here.
and "currentTime" is when you paused the game.
Andre's answer is more easy;
my answer is more accurate.
because getTimer() returns real miliseconds but timer is not ticking exactly accurate.ENTER_FRAME event and setInterval are also same as timer.
that means if you want a more simple code, use Andre's and if you want an accurate Timer, use Mine.
and removing eventListener (3vilguy's answer) is not enough
because getTimer won't Pause !
I hope my answer be useful.
Better using the Timer- Object
var seconds=9;
var t:Timer=new Timer(1000,seconds);
t.addEventListener(TimerEvent.TIMER, updateTimer);
t.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
updateTimer();
t.start();
function updateTimer(e:TimerEvent=null){
var t:uint = t.currentCount;
var dt:uint = seconds - t;
var totseconds:uint = dt;
var minutes:uint = Math.floor(totseconds/60);
var sec:uint = Math.floor(totseconds%60);
var minsWithZ = minutes < 10 ? "0" + minutes : minutes;
var secsWithZ = sec < 10 ? "0" + sec : sec;
var time = minsWithZ + ":" + secsWithZ;
this.tempo_txt.text = time;
}
function timerCompleteHandler(e:TimerEvent):void{
trace("Timer complete");
}
pausa.addEventListener(MouseEvent.MOUSE_UP, menuPause);
function menuPause(e:MouseEvent){
t.stop();
voltar.addEventListener(MouseEvent.MOUSE_UP, play);
}
function play (e:MouseEvent){
t.start();
}
Greetings
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!
I'm working with the youtube API and I'm getting the current time on the video as seconds.
What I want to do is to convert them into this: MM:SS
I've tried to google and try different things by myself but nothing seemed to work and be efficient.
I'll be glad to see how it can be done in a good and efficient way thanks in advance!
Something like:
var formattedTime =
(Math.floor(seconds/60)) + ":" + // minutes
(seconds % 60 >= 10 ? "": "0") + // padding for seconds if needed
(seconds % 60)); // seconds
here is a little class I use all the time just for what you need. I've also added a timecodes to seconds method. Simply use it like Timecodes.secondsToTimecode(634); which will output 00:10:34
package com.ronnieswietek.utils
{
public class Timecodes
{
public function Timecodes()
{
}
public static function timecodeToSeconds(tcStr:String):Number
{
var t:Array = tcStr.split(":");
return (t[0] * 3600 + t[1] * 60 + t[2] * 1);
}
public static function secondsToTimecode(seconds:Number):String
{
var minutes:Number = Math.floor(seconds/60);
var remainingSec:Number = seconds % 60;
var remainingMinutes:Number = minutes % 60;
var hours:Number = Math.floor(minutes/60);
var floatSeconds:Number = Math.floor((remainingSec - Math.floor(remainingSec))*100);
remainingSec = Math.floor(remainingSec);
return getTwoDigits(hours) + ":" + getTwoDigits(remainingMinutes) + ":" + getTwoDigits(remainingSec);
}
private static function getTwoDigits(number:Number):String
{
if (number < 10)
{
return "0" + number;
}
else
{
return number + "";
}
}
}
}
var timeStr:String;
//Video's length >= 1 hour
if( seconds >= 60*60 ){
//Format-> H:MM:SS
timeStr = (""+Math.floor(seconds/(60*60))) + //Hours
":"+
("0"+Math.floor((seconds%(60*60))/60)).substr(-2)+ //Minutes
":"+
("0"+(seconds%60)).substr(-2); //Seconds
}else{
//Format-> MM:SS
timeStr = ("0"+Math.floor(seconds/60)).substr(-2)+ //Minutes
":"+
("0"+(seconds%60)).substr(-2); //Seconds
}
Something a little easier to read to get your head around it:
var seconds:int = 200;
var minutes:int = 0;
while(seconds >= 60)
{
seconds -= 60;
minutes ++;
}
trace(minutes, seconds);
The leading zeros part can be done like so:
var secStr:String = String(seconds);
var minStr:String = String(minutes);
secStr = (secStr.length == 1) ? "0" + secStr : secStr;
minStr = (minStr.length == 1) ? "0" + minStr : minStr;
trace(minStr + ":" + secStr);
I'm trying to get a random date in a date range and this is what i have so far but it doesnt seem to be working ??
Where I'm I Going wrong ??
//Gets the date difference
private function differenceBetweenDates(date1:Date, date2:Date):Number{
var MS_PER_DAY:uint = 1000 * 60 * 60 * 24;
var tempDate:Date = new Date(date2.time - date1.time);
var difference:Number =
Math.abs(Math.round((tempDate.time / MS_PER_DAY)));
return difference; }
// gets a random number
function randomRange(max:Number, min:Number = 0):Number {
return Math.round(Math.random() * (max - min) + min); }
protected function getRandomDate:void {
// TODO Auto-generated method stub
var dat1:Date= new Date();
var dat2:Date = new Date(1989, 4, 16)
var num:Number = new Number(differenceBetweenDates(dat2,dat1));
var random:Number= new Number(randomRange(num));
currDate.setDate(dat2.date+random);
getComic(formatDate(currDate));
dat2 = new Date(1989, 4, 16)
}
I found a couple errors in your code.
currDate.setDate(dat2.date+random)
setDate sets the date in the month, not an arbitrary date in time. Also, you want to use dat2.time, not dat2.date.
Should be
currDate.setTime(dat2.time+random)
Here's a slightly different version you might want to try out. I removed the MS_PER_DAY computation, so, you may want to add that back in if you need it, but I found this easier to look at:
public function getRandomTimeBetweenDates(date1:Date, date2:Date):Number
{
return Math.round(Math.random() * (Math.abs(date2.time - date1.time)));
}
public function getRandomDate():Date {
var dat1:Date= new Date();
var dat2:Date = new Date(1989, 4, 16)
dat2.setTime(dat2.time + getRandomTimeBetweenDates(dat2,dat1));
return dat2;
}