2017년 8월 21일 월요일

Fun with the clock


The clock in App Inventor has many uses.  You can use it to animate sprites on a canvas, create a timer, design a calendar, and much more.

Here's a couple things I've done with it:

Display text on a label as it's typed in a text box
For this example, add a text box, a label, and a clock to the design screen. Remove the initial text from the label, and set the clock's interval to 50 milliseconds (this will cause the clock to fire every quarter of a second - yeah, very fast).  In the blocks editor, drag out he Clock.Timer event, place inside it a set Label.Text to block and plug the Textbox.Text into that.  Now, anytime you enter text into the text box, it should automatically display on the label as you type.

Determine whether a given year is a leap year
I've been playing around with making a calendar app, and although I don't know if I'll ever finish it, I did need to figure out how to determine whether a given year the user selects is a leap year, and thus alter the number of days available in the month of February.  I was lost at first with how to manipulate the blocks available for the clock to determine a leap year.  I asked myself "How do I determine a year is a leap year?" and then I asked Google.  It turns out that a leap year is any year evenly divisible by 4 but not by 100, unless that same year is evenly divisible by 400.  Confusing enough?  It was for me.  I then stumbled across the Wikipedia leap year article which listed out the psuedocode for programming and I also learned about modular division at the same time.  The combination of the two gave me the answer I needed.  Using the modulo block in App Inventor was able to easily determine whether a given year is evenly divisible by 4 but not 100 unless by 400.  In the picture to the left I have a Clock1.Now block plugged into the instant plug for the Clock1.Year but this can easily be replaced by creating any instant you want with any year.  If you were to choose 1914 or 2763 it would determine for you whether those years are leap years.

Enable a button when text is entered in a text box
This example is similar to the one above about displaying text, except that I want to enable a submit button only after the user has entered a username into the text box of a loginscreen.  I prefer to do it this way so I don't have to worry about error handling or warning the user that they haven't entered their username before trying to log in.  I could handle it by popping up a notification if they pressed submit and saying "Hey!  You didn't enter your username!"  but this creates unnecessary blocks in my opinion.  With this example, I just have to check whether the username is already created and if so then I do my log in procedures and stuff.  If it's not, they have to try again.  Very simple.  Give it a shot.

Calculate milliseconds in minutes, hours and days
When dealing with instants and such with the clock, there are a number of procedures that calculate using milliseconds, so it can be advantageous to know how to find milliseconds in a given time block. To prepare for this, we figure out how many milliseconds in one second (1000), then just multiply that number by how many seconds in a minute (60), then multiply that by minutes in an hour (60) and finally hours in a day (24).  Since we can't make these calculations when defining our variables, we simply initialize all but seconds as 0 and do the multiplication in the Screen.Initialize event.



댓글 없음:

댓글 쓰기