Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Date/Time events help

Community Beginner ,
Aug 06, 2014 Aug 06, 2014

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".

TOPICS
ActionScript
266
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 06, 2014 Aug 06, 2014

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

}

Translate
Community Expert ,
Aug 06, 2014 Aug 06, 2014

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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 06, 2014 Aug 06, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 07, 2014 Aug 07, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 07, 2014 Aug 07, 2014

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"?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 07, 2014 Aug 07, 2014
LATEST

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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines