Showing Scores

If you have your scores saved in a file, you can show them either in an answer box or in a field on the screen. We will look at both ways:

1. Getting the data from the file:
First we need to open the file and get the data from it. Then we need to sort it numerically by high score. The file I will use has the data as follows:

    name (tab) score
    john    100
    Sue    150
    Wally    50

It is in a file called "scores.txt" in my "Documents" folder

   put specialFolderPath("documents") & "/scores.txt" into myFile
   set itemDelimiter to tab
   put  url ("file:" & myFile)    into    x
   sort lines of x  numeric by item 2 of each


2a. The Code to show it in a Dialog Box:

                          


        Add a button "ShowTimes" with the following code:

on mouseUp
   put specialFolderPath("documents") & "/scores.txt" into myFile
   set itemDelimiter to tab
   put  url ("file:" & myFile)    into    x
   sort lines of x numeric by item 2 of each
   answer x
end mouseUp


2b.  The Code to show it in a field on the card:
           Here you can show the scores in a field on the card. Use either a "scrolling text field" or a "table" field. You might like a table field because the scores are lined up nicely in columns. (take off the vertical and horizontal table lines and it looks great

                          


        Add a new Card - called "myTimes"

        Add a new button called "Scorecard"

                         

        put code on it
            on mouseUp
                go to card "myTimes"
            end mouseUp

        On the card script of the new card add code to read the scores from the file and put it into a table field

           on openCard
                   put specialFolderPath("documents") & "/scores.txt" into myFile
                   set itemDelimiter to tab
                   put  url ("file:" & myFile)    into    field "times"
                   sort lines of field "times"  numeric by item 2 of each
            end openCard

        Add a button to go back to the game:

           on mouseUp
                go to card "myCard"
           end mouseUp
 

   Another way (more compact) that might work:
          Here you can show the scores in a field on the card. Use either a "scrolling text field" or a "table" field. You might like a table field because the scores are lined up nicely in columns. (take off the vertical and horizontal table lines and it looks great)

           on mouseUp
   put specialFolderPath("documents") & "/scores.txt" into myFile
   set itemDelimiter to tab
   put  url ("file:" & myFile)    into field "myData"
   sort lines of  field "myData" numeric by item 2 of each
end mouseUp


That's all there is to it. 

Comments