Page 1 of 2

Count up timer over multiple cards

Posted: Thu Jan 01, 2015 1:20 pm
by CammyGray21
Hi guys,

I have a timer in my game that I am trying to implement over multiple cards so that when the card switches it takes the time from the field previous and puts it into the next to continue (Or so in theory) this will then be placed into a leaderboard at the end of the game along with Name etc that you would normally find there. Just wondering if anyone could give me a kick start on how to get the timer to work over the cards and will see how it goes from there!

Cheers!

Re: Count up timer over multiple cards

Posted: Thu Jan 01, 2015 4:17 pm
by jmburnod
Hi,
You can make a group with your objects timer
This group should have "behave like background" option to true.
Then you can place this group in each cd.
When you create a new cd this group is automaticaly placed onto the new cd. :D
Best regards
Jean-Marc

Re: Count up timer over multiple cards

Posted: Thu Jan 01, 2015 5:34 pm
by CammyGray21
Hey thanks for the reply but it is more getting the time to carry over from one card to another that I am stuck on at the moment. So on card one when the timer starts, say it gets to 12 when the card switches 12 will be placed into the next timer and will continue to count up from there, continuing to take the number and place it into the next timer until the game completes therewith the time being placed into the leaderboard. Also on another note I am not an avid livecoder we were given a school project to complete livecode was our means of doing so. So not to sound needy but as much in depth help as possible would be appreciated.

Thanks!

Re: Count up timer over multiple cards

Posted: Thu Jan 01, 2015 6:30 pm
by Martin Koob
You can take advantage of script locals and the fact that references in the stack script to objects will apply to the objects of the current card.

On each card put a field named "timeDisplay".
On at least one card put a button "start Timer" that calls the command 'startTimer'
On at least one card put a button 'stop Timer' that calls the command 'stopTimer'

in your stack script put the following:

Code: Select all


local sStartTime, sTimerOn

on startTimer
   put true into sTimerOn
   put the seconds into sStartTime
   send "incrementTime" to me in 1 second
end startTimer

on incrementTime
   put the seconds - sStartTime into field "timeDisplay"
   if sTimerOn is true then
      send "incrementTime" to me in 1 second
   end if
end incrementTime

on stopTimer
   put false into sTimerOn
end stopTimer

on preopencard
   put the seconds - sStartTime into field "timeDisplay"
end preopencard
So the script local variable sStartTime contains the time the timer was started and then each time time increment is called it calculates the elapsed time and puts it in the field "timeDisplay" of the current card.

The preopenCard script will ensure that the proper elapsed time is put into the field as soon as the card opens.

Hope this helps. Good luck with the project.

Martin

Re: Count up timer over multiple cards

Posted: Thu Jan 01, 2015 7:04 pm
by CammyGray21
Thanks again for the quick reply.

I assume that in the case of using a button you could also have it so that when the original card that begins the timer opens that it can be initiated in the same way? Or is that a misconception on my part.
Thanks again.

Re: Count up timer over multiple cards

Posted: Thu Jan 01, 2015 7:25 pm
by Martin Koob
Yes you are correct, it does not have to be called in a button script. You could have a call to startTimer in the openCard script of the card you want to start the time. (Don't put it in the stack script though because that would re start the timer with each card that opened).

In the card script you would have

Code: Select all

on opencard
     startTimer
end opencard
The stopTimer could be called by anything as well, a closeCard command, a mouseUp command in a button or by some action occurring in the game. Whenever you want just call 'stopTimer' and it will stop the timer.

Re: Count up timer over multiple cards

Posted: Thu Jan 01, 2015 10:58 pm
by CammyGray21
Hey I have this as my timer script, it calls on each card from opencard, only thing is when I complete on one card and go back to the next the timer resets to 0 any help? Or would it actually be a good thing if I could send the program to someone?

Code: Select all

on updateTimer
   if time_used>=0 then
      put time_used into field "Timer"
      put time_used +1 into time_used
      send "updateTimer" to me in 1 second
   end if
end updateTimer

Re: Count up timer over multiple cards

Posted: Fri Jan 02, 2015 1:56 am
by Newbie4
Have you declared time_used as a global everywhere that you reference it?

E.g.

Code: Select all

global time_used

Re: Count up timer over multiple cards

Posted: Fri Jan 02, 2015 12:40 pm
by CammyGray21
I have it declared as global yes but when I return to what is essentially my main card where the other cards can be accessed from the timer resets itself back to 0 again.

Re: Count up timer over multiple cards

Posted: Fri Jan 02, 2015 2:30 pm
by Newbie4
Do you have it declared global there too? There has to be a global statement everywhere you use that variable.

Where do you reset it to zero? Could you be executing that code every time you return to the main card?

Re: Count up timer over multiple cards

Posted: Fri Jan 02, 2015 8:02 pm
by jacque
I like the grouped object approach better than putting a copy of the field on each card. It's cleaner and uses fewer resources.

Create the field that will hold the text. Group it. In the group property inspector (not the field's inspector) choose "behave as background". Then in the field's property inspector, select "shared text". That will allow the same text to be shown on every card, and when you change cards, the field will retain its content without any globals or scripting.

Place the group on any cards that need to display that field. You can use the Object menu to place the group.

Re: Count up timer over multiple cards

Posted: Sat Jan 03, 2015 10:12 pm
by CammyGray21
Is there a way for me to upload the program onto the forum page? Cause I think that it would certainly help me a lot more if you guys could physically see the program too. Upload attachment doesn't seem to like it so anything please thanks.

Re: Count up timer over multiple cards

Posted: Sat Jan 03, 2015 10:49 pm
by SparkOut
Zip the file first, and you should be able to upload the attachment, but you will need a minimum number of posts first (1 think 10?)

Re: Count up timer over multiple cards

Posted: Sat Jan 03, 2015 11:30 pm
by CammyGray21
Alright thanks for that, will try to get my posts up to 10 in the meantime.

Re: Count up timer over multiple cards

Posted: Sun Jan 04, 2015 2:24 pm
by CammyGray21
Can I just ask actually, I have a maze as part of my program as sort of a minigame of sorts but it works for my windows PC but not on my Mac is it because some of the functionality doesn't port over or is there any other reason? Cheers again.