Copy link to clipboard
Copied
I'm trying to make vpet like game on flash for class, but I need to work with time and dates for events like feeding, sleep, etc.
At least for now I need help figuring out how to make the game check like every 5 minutes to bring up an alarm about the pet needing food.
Sadly all i have is this:
stop();
const millisecondsPerMinute:int = 1000 * 60;
const millisecondsPerHour:int = 1000 * 60 * 60;
const millisecondsPerDay:int = 1000 * 60 * 60 * 24;
var date1:Date = new Date();
trace(date1);
I get the actual date, but don't know exactly what to do to make it check every 5 minuts or so. Don't know if it's with "if" or "do/while".
don't use the date class, use the timer class.
var feedTimer:Timer=new Timer(5*60*1000,0);
feedTimer.addEventListener(TimerEvent.TIMER,feedF);
feedTimer.start();
// whenever vpet is fed, apply a reset() and start() to feedTimer:
function beenfedF():void{
feedTimer.reset();
feedTimer.start();
}
function feedF(e:TimerEvent):void{
// a feeding was missed
}
Copy link to clipboard
Copied
don't use the date class, use the timer class.
var feedTimer:Timer=new Timer(5*60*1000,0);
feedTimer.addEventListener(TimerEvent.TIMER,feedF);
feedTimer.start();
// whenever vpet is fed, apply a reset() and start() to feedTimer:
function beenfedF():void{
feedTimer.reset();
feedTimer.start();
}
function feedF(e:TimerEvent):void{
// a feeding was missed
}
Copy link to clipboard
Copied
Awesome! Got it to work! Thanks.
Sorry to ask a question slightly out of the topic... How is it that you can save up the current progress? For example if I close the swf it'll go back from the start and I'd have to start it from a baby. I hope I'm not asking for too much. Thanks in advance.
Copy link to clipboard
Copied
you're welcome.
you can use the sharedobject to save game progress. you'll need to set your game up to check the sharedobject when your swf starts, it checks the variables you stored (eg, how big your pet is and what tricks it knows) and applies them so you don't start with a newborn pet that knows nothing.
Copy link to clipboard
Copied
OK, so I looked shareobject. Once it starts to play, it checks for data and since there is none it creates one.Now where I am stuck is how to make it store data in it.
For example I found this online:
var so:SharedObject = SharedObject.getLocal("test");
if (so.size == 0)
{
trace("created...");
so.data.now = new Date().time;
}
trace(so.data.now);
trace("SharedObject is " + so.size + " bytes");
so.flush();
The game starts, the pet hatches after some time. How do I use or modifiy this to make the shareobject go straight into the newborn instead of the egg? Do I have to do something with the "so.data.now"?
Copy link to clipboard
Copied
typically you would use something like this at game session start:
// game_duration will store the number of milliseconds since the game started
var game_duration:int;
var so:SharedObject=SharedObject.getLocal("test","/");
if(so.data.game_duration){
game_duration=so.data.game_duration;
} else {
game_duration=0;
}
// then when a game session ends (and if you cannot trust your user to save the game, you might do this periodically, too).
so.data.game_duration=game_duration+getTimer();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now