//////////////////////// Initialize variables ///////////////////////
//make Local Shared Object named myLocalSO(in as) called "myflashcookie" on disk
var myLocalSO:SharedObject = SharedObject.getLocal("myflashcookie");
//speed var for easing
var speed = 3;
var w = myCircle._width/2;
//toggle var for easing
var ease = true;
//as var to store alltime cookie
var allTimeVisitCount=0;
countVisit();
cookieFeedback();
//line style for tracing movement
lineStyle(1, 0, 50);
//////////////////////// Functions ///////////////////////
//set key's value to specified value in SO
//key is the name of the data
//val is key's value
function setVal(key, val) {
myLocalSO.data[key] = val;
trace(key +" set to "+val);
//flush the SO, write the SO to disk
myLocalSO.flush();
}
//get key's value from SO
function getVal(key) {
return myLocalSO.data[key];
trace(myLocalSO.data[key] +" received from "+key);
}
function countVisit() {
//if first visit
if (getVal('visitCount') == undefined) {
//create date for now and store in cookie
var todayDate:Date = new Date();
setVal('date', todayDate);
trace("creating date");
//start/reset counting visits
var visitCount = 0;
//notice allTimeVisitCount is not reset, but stored still as a var in actionscript
}
//not first visit
else {
visitCount = getVal('visitCount');
allTimeVisitCount = getVal('allTimeVisitCount');
}
//increment visit counter
setVal('visitCount', visitCount+1);
setVal('allTimeVisitCount', allTimeVisitCount+1);
//feedback of visit counting
visitsFeedback.text = getVal('visitCount');
allTimeVisitsFeedback.text = getVal('allTimeVisitCount');
}
//feedback of cookie contents
function cookieFeedback() {
//in defined print coordinate contents
cookiex.text = getVal('circleX') == undefined ? "no cookie" : getVal('circleX');
cookiey.text = getVal('circleY') == undefined ? "no cookie" : getVal('circleY');
//if not easing assign coordinates from cookie
if (!ease) {
myCookie._x = getVal('circleX');
myCookie._y = getVal('circleY');
}
//set target to cookie coordinates
else {
ctargetx = getVal('circleX');
ctargety = getVal('circleY');
}
//if name then trace cookie contents
if (getVal('name') != undefined) {
visitorFeedback.text = "Returning Visitor";
nameInput.text = getVal('name');
ageInput.text = getVal('age');
}
//no name then a new visitor
else {
visitorFeedback.text = "First Time Visitor";
nameInput.text = "";
ageInput.text = "";
}
calculateCookieAge();
}
function calculateCookieAge() {
//make a date now
todayDate = new Date();
//get the cookie's stored date
cookieDate = getVal('date');
//difference between two dates
cookieDateAge = Math.floor(todayDate - cookieDate);
//convert miliseconds to a timecode
cookieAge.text = msToTimeCode(cookieDateAge);cookieDateAge;
}
//convert miliseconds to a hh:mm:ss
function msToTimeCode(ms) {
//make sure value is within bounds. if a number grater than zero and less than the duration of video
if (isNaN(ms) || ms< 0) {
ms = 0;
}
//find seconds
var sec = ms/1000;
//find minutes
var min = Math.floor(sec/60);
//adjust seconds
sec = sec - min*60;
//find hours
var hour = Math.floor(min/60);
//adjust minutes
min = min - hour*60;
//floor seconds down to whole number
sec = Math.floor(sec);
//make time code with hours
if (hour == 0) {
if (sec < 10) {
sec = "0"+sec;
}
if (min < 10) {
min = "0"+min;
}
var tc = min+":"+sec;
}
//make time code without hours
else {
if (sec < 10) {
sec = "0"+sec;
}
if (min < 10) {
min = "0"+min;
}
var tc = hour+":"+min+":"+sec;
}
return tc;
}
////// Actionscript attached to Objects/handlers //////////
//place data on stage into cookie (circle coordinates and input text)
setCookieButton.onRelease = function() {
setVal('circleX', myCircle._x);
setVal('circleY', myCircle._y);
setVal('name', nameInput.text);
setVal('age', ageInput.text);
//update the display on stage
cookieFeedback();
}
//make random coordinates on stage
randomButton.onRelease = function() {
//if not easing assign coordinates to myCircle
if (!ease) {
myCircle._x = Math.random() * (Stage.width - w);
myCircle._y = Math.random() * (Stage.height - w);
}
//if easing assign coordinates to myCircle's target coords
else {
targetx = Math.random() * (Stage.width - w);
targety = Math.random() * (Stage.height - w);
}
}
myCircle.onPress = function() {
this.startDrag();
dragging = true;
lineStyle(1, 200, 30);
}
myCircle.onRelease = myCircle.onReleaseOutside = function() {
targetx = this._x;
targety = this._y;
lineStyle(1, 0, 50);
dragging = false;
this.stopDrag();
}
myCircle.onEnterFrame = function() {
//print position feedback
currentx.text = this._x;
currenty.text = this._y;
//if eas move to target
if (ease) {
if (!dragging) {
moveTo(this._x+w, this._y+w);
this._x+=(targetx-this._x)/speed;
this._y+=(targety-this._y)/speed;
}
//draw line
lineTo(this._x+w, this._y+w);
}
}
myCookie.onEnterFrame = function() {
//if ease move cookie to target
if (ease) {
this._x+=(ctargetx-this._x)/speed;
this._y+=(ctargety-this._y)/speed;
}
calculateCookieAge();
}
//Position from Cookie
cookieButton.onRelease = function() {
//if not easing set coordinates from cookie
if (!ease) {
myCircle._x = getVal('circleX');
myCircle._y = getVal('circleY');
}
//if easing set target coordinates from cookie
else {
targetx = getVal('circleX');
targety = getVal('circleY');
}
}
easeBtn.onRelease = function () {
//toggle easing
ease = !ease;
//advance the frame of this button...
this.play();
}
clearCookieBtn.onRelease = function() {
//clear the cookie (swipe all data)
myLocalSO.clear();
//restart visit count
countVisit();
//read cookie and give feedback
cookieFeedback();
}