BuildingaScorekeeperAlice3TricksinMiniTrivia (1/4)
ByVickiZhangDukeUniversity
UnderthedirectionofProfessorSusanRodger
June2017
0à -5à 20à 85
AbouttheMiniTriviaChallengeMiniTriviaisafour-questiongamecreatedbyVickiZhang.Theauthorexplainsfourusefultopicsinvolvedinseparatetutorials:1. scorekeeper2. askinguserforananswer3. billboard4. multi-layeredobject
clicking
Thistutorialexplainshowtobuildascorekeeper.StartbydownloadingMini Trivia_Challenge 1 Scorekeeper_Start
Togetstarted >>FindandcreateanewTextModel.
Setname toscore,andinitialvalue to0.
Dragthetextontoaclearspot(shownonnextpage)usingtheDefault handlestyle.
Resizeitby- changingthedatadirectly- orusingtheResize handlestyle.
Tweakittoroughlythissizeandlocation.
Colorcouldbechanged,ifsoinclined:----------------------------Whensatisfied,clickon
OnenewProperty forScore
•Goal:Createapropertyusedtostorethevalueofcurrentscore•Purpose:Toaccessthevalueeasily•How:Addnewproperty for TextModel• Valuetype:wholenumber• Name:currentScore• Initializer:0
NotethatweareenablingALLtextmodelsthispropertyandtheproceduresthatwewillcreate.
AfteryouaddedthecurrentScore property,Aliceautomaticallycreatesaprocedure(setCurrentScore)andafunction(getCurrentScore).Wewillusethemsoon.
currentScore isapropertyoftheTextModel,andusedaparameterofthisprocedure
ThreenewProcedures forScore
1. setScore
2. addScore(#)
3. reduceScore(#)
Notes💡 :• addScore(5) will add 5 to the score,andreduceScore(10) will subtract 10from the score.• The # sign in the procedure is aplaceholder demanding an input.Consider: If addScore did not havethis input, Alice would not know howmuch to add.Thisiscalledaparameter.
SetScore
• AddTextModel ProcedureandnameitsetScore
• Thisprocedureiscreatedforthepurposeofsettingthecontentofdisplay
DraginthesetValue procedure>>Select“customTextString”>>Press“Ok”directly.Theresultisshownbelow.Thenclickthetinytriangleontherightandselect
“”+???>>wholenumber>>currentScore
SetScore
SetScoreCompleted
AddScore
• CreateasecondProcedurefortheTextModel• NameitaddScore• Addaparameterforthisprocedure• **sothatAliceknowshowmuch to add**• NametheparameterhowMuch
>>
AddScore
•DraginthesetCurrentScore procedureandselectcurrentScore astheparameterinthedropdownmenu.•WewanttoaddtothecurrentScore byhowMuch• Theresultingformulashouldbe:
setCurrentScore to(currentScore +howMuch)• Processshownonnextpage.
AddScoreCompleted
ThepointofaddScore istoupdatethevalueofcurrentScore andupdatethescoredisplayedonthescreenbycallingsetScore.
ReduceScore
• ThisismerelyanumericallyflippedversionofaddScore.• Tryityourselffirst!• Step-by-stepguideonthenextslide.
ReduceScore
• CreateasecondProcedurefortheTextModel• NameitreduceScore• Addaparameterforthisprocedure• **sothatAliceknowshowmuch to reduce**• NametheparameterhowMuch
>>
ReduceScore
•DraginthesetCurrentScore procedureandselectcurrentScore astheparameterinthedropdownmenu.•WewanttosubtracthowMuch fromthecurrentScore• Theresultingformulashouldbe:
setCurrentScore to(currentScore-howMuch)• Processshownonnextpage.
ReduceScoreCompleted
ThepointofreduceScore istoupdatethevalueofcurrentScore andupdatethescoredisplayedonthescreenbycallingsetScore.
ScorekeeperCompleted!
• Nowwejusthavetoplugintheprocedurestoappropriateplacesinthegame.• YouwillhavecommentsinQuestionA,QuestionC,QuestionD,andinitializeEventListenersforguidance.
Example:QuestionA
Step1:Gotoscene andyoushouldfindthesceneprocedures.
SelectquestionA procedure.
Example:QuestionA
Step2:SelectonthelefttoolbarTextModel—this.score,whichisthevisualizedformofourscorekeeper
Example:QuestionA•WecannowseeallproceduresofTextModel ontheleft,includingtheoneswecreated.• DraginaddScore intothedoTogether box,eitherbeloworabovethecomment.• Selectcustomwholenumberandtypein10.
Example:QuestionA• Similarly,dragreduceScore in.Makesureyouplaceitatthesamelevelofthecomment,i.e.under“else”andoutsideofdotogether.• Selectcustomwholenumberandtypein5.
Tryitout!
• Runthegameandclickonthehammer,whichcorrespondstoQuestionA.
• CommentsforQuestionsB,CandDarefound,respectively,ininitializeEventListeners,questionC,questionD.