Timer
SWSGKloss

Got another question,

In my Memory game there should be a timer as well as a highscore.

The more important thing is a working timer. So is there an easy way coding it?

Have read this answer: Click

But I just want to stop how long the users will need to find all pairs. So the timer should start when the first card is flipped and stop when the last pair is found. Refering to your YT-Tutorial the timer should stop when your cards "fade out".

Don't know if it is easier to import some java lib's and code it that way or if there is a quick solution in your framework.

 

Blessings,

Flo

All 6 Comments
Gio

I would do it this way:

  • Create a SceneObject and call it timer
  • Add a TextSprite to it, and remove the default Sprite
  • Set the TextSprite to your preferred font, color and size, and set the text value to 0
  • Go to the Functions tab, add a function called start
    this.time = 0;
    this.running = true;
    this.interval(1000, 'tick');

     

  • Add a function called tick
    this.getSprite().setText(++this.time);
    

     

  • Add a function called stop
     
    this.running = false;
    this.unschedule('tick');

     

  • In your card's onMouseDown function, add this:
    if (!_.timer.running)
    {
        _.timer.start();
    }

     

  • Finally, wherever we have the code that calls fadeOut  (can't remember exactly where it is off the top of my head), add this:
     
    _.timer.stop();

     

 

SWSGKloss

Okay, this solution works quite well.

I've changed it to an 100ms interval so I am able to measure .1 secs.

Last Question to this topic:

Any way to format the written text? Checked a way where I've added .1 on the time in the tick function, but it's a text, no float ...

So I got literally no idea how to format the count.

Gio

If you are doing 100ms intervals, it makes sense to increment the time variable by 0.1 rather than 1. In other words, I'd change your tick function to this:

this.time += 0.1;
this.getSprite().setText(this.time);

Note that this.time is a number, it gets automatically converted to a string when you pass it to setText. You could do anything you like to it before passing it to setText, for example the argument of setText could be this.time.toFixed(1) to force 1 decimal digit, or this.time.toString(16) to convert it to a hex string, or you know, any sort of formatting that you like :)

SWSGKloss

Tried it with 0.1 in "tick", but then I get results like 4.99999999 or 5.000000001...

Checked it also with:

this.time += 0.1;
this.time.toFixed(1,2); //Also tried (1)
this.getSprite().setText(this.time);

No changes.

 

Is ther any documentation of wade where I can read through all functions and stuff like that? Maybe then I don't have to ask that much questions :-P

Gio

The documentation for all WADE functions is here:

WADE API reference

However it's more a JavaScript question than a WADE question. The problem with the code above is that toFixed() doesn't modify the original number, it returns a new string, so you want

this.time += 0.1;
this.getSprite().setText(this.time.toFixed(1));

 

SWSGKloss

Oh, okay. That worked.

Again: Thank you for your fast responses and effort you put in this project and the questions around it!

Post a reply
Add Attachment
Submit Reply
Login to Reply