Right-click the text sprite and select Script from the context menu. Delete the default mouseUp and create the following:
on beginSprite me
timerText = member("timeDisplay").text
spaceLoc = offset(" ", timerText)
myTime = timerText.char[spaceLoc + 1..timerText.length]
myText = "Congratulations!" & return & "You finished in" && myTime && ¬
"seconds."
sprite(me.spriteNum).member.text = myText
end
What this short handler does is parse the actual time out of the timeDisplay member in order to use it in the congratulatory text. First, the full string is retrieved from the member and placed in the timerText variable. Next, Lingo's offset method is used to find the location of the space within the string. This is needed because everything after the space is the time: Let's assume timerText is "Time: 42.50" You can count and find that the space is at position 6 within the string. Therefore characters 7 through 11 represent the time: "42.50" Once you have the position of the space, within spaceLoc, the time is pulled from the string using the char property of the string:
myTime = timerText.char[spaceLoc + 1..timerText.length]
By using someString.char[x..y], you can pull out a section of the string from character position x to position y. In this case, because spaceLoc is 6, this line could be rewritten as:
myTime = timerText.char[7..timerText.length]
So we're pulling out characters 7 through the length of the string, which is 11, so you are left with the time: "42.50". This is stored in the myTime variable, then used to replace the text in the member. But if you use Lingo to replace the entire contents of the text, why bother to enter it when creating the text sprite in the first place? Why not just create an empty text member, place that on Stage, and use Lingo to change the text? There are reasons for entering it into the member to begin with. First, this way you can visualize the placement, color, font, size, etc. Second, empty text members don't retain their formatting, so placing text in there (even a space) keeps your formatting options intact. |