2017년 8월 30일 수요일

GameClock Visual Countdown for AI2 - Simplified (tutorial)




**Edit  In the mainstartBUTTON the variable called is suppose to be TimeLeft (just realized i didnt set that)
This is from an already working program just copy/pasted and shown what was needed for this, this is why there is red flags....for duplicates.)

Here is a simple count down timer that displays the countdown and stops at 0 for your games or other programs.

1a-  (optional) create a Variable called TimeLeft.  This probably is not even required for the countdown but if your doing more then one board it gives an easy common referral for copy/pasting scripts...or different aspects of control for time when dealing with several clocks that work with or against each other.   You can do without this thou and just do the math with just your TimerValueLabel.
1b- create your count down displayed label.  I called mine TimerValueLabel.  you can establish the starting number while creating the label or define it in screen1 or the start button.
2- create 2 procedures....one for clocks turned on and one for them being off.   (ClockOn) (ClockOff).   Using your clock now.. (Mine is called TimerClock)  I did this after personally having background clocks for other boards conflicting.  Turning all clocks off when it starts and only activating what you need for that board will save you a lot of headaches in larger complex games.  Plus you can create a pause button if needed with this in place.  if your counting down by seconds, then don't even change your clocks interval setting.  1000 = 1 second.
3- Screen1.Initialize - call the clocksoff procedure to make sure it dont start until your ready to begin your game.
4-within your games Start button... call the ClocksOn procedure to start the timer, also reset your time with the starting time you count down from.  I used 120 which means that my TimeLeft variable will have the value of 120.
5-with your clock now, (when its turned on) it will read the script like this:  my TimeLeft variable of 120 is now equal to 120-1 every time the clock fires.   Since we did not change the speed of the clock firing, the clock will fire every 1 second meaning the count down will be every 1 second as well.
The next block under that is to display that count down.  TimerValueLeft is the label we created to display it, and now were defining it to follow suit with the TimeLeft count down.  So now everything changes together as the clock fires every 1 second.
6- the if loop:  Make the TimeLeft less then or equal to 0....not just equal to.   If you have a slight delay, or a jump in the program and it misses 0 for some odd reason...it will go into the negitives and keep going.  make the if results turn the clock off as well.  This is the second backup plan.  by turning off the clock it cannot possibly keep counting.  Also it will not create a background issue in other boards you have.

Notice how each time you start the program, the start button re-defines the clock at its starting value.  This is important too so that it does not start were you left off from a previous game.

--
Thanks Richard, Not had time to try this out yet. Looks like it will do the job. I'll post back later when I have tried it out. Appreciate your help.

--
If You use onlu clocks to countdown seconds after awhile you will get a slight difference in time. you have to use system timer to do a proper and accurate countdowns or time ridings. I have added a picture of a simple example. When the button is clicked it checks the current system time and sets it to a variable, then You can compare system time after a while to that variable to get precise time.The division by 1000 et the end is to get seconds insted of milis.


--
Any chance of uploading the file. For some reason, when I do the TimerClock.Timer control and then add the next blocks I can select the Get Variable block but I am unable to select global TimeLeft. Is this a bug.
also in any other blocks that have drop downs I can't change them.
Will reboot laptop and try again.
Running Windows Vista.
and using App Inventor 2 web version.

--
@casa: which browser are you using?
you might want to provide a screenshot of your relevant blocks

--
using firefox 26. Just tried Chrome and it works ok. So it looks like a Firefox problem.
--
How do you compare this system time to the clock time?  Can you show more, 

--

댓글 없음:

댓글 쓰기