Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose...
Transcript of Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose...
![Page 1: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/1.jpg)
MakingaTimerinAlice
ByJennaHayesunderthedirec8onofProfessorSusanRodger
DukeUniversityJuly2008
www.cs.duke.edu/csed/alice/aliceInSchools
![Page 2: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/2.jpg)
This tutorial will teach you how to make a 2mer in Alice. Timers can be very useful if you are interested in making 2med games and have many other uses.
StartanewAliceworld,andaddatextobjecttothatworld.Whenitasksyouwhatyouwantthetextobjecttosay,typein0.0.
Nowinyourobjecttreerightclickon0.0andrenameit8mer.Let’sgetstartedcodingour8mer.
Step 1: Adding the Text Object
![Page 3: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/3.jpg)
Clickon8merinyourobjecttree,andthengototheproper8estab.ClickonthecreatenewvariablebuRon.CreateaNumbervariablenamed8meLeS.Fornow,setitsvalueto0.
That 0 is just a placeholder. We will write code in the method editor so that we can enter in whatever value we want before we play the world.
Step 2: Creating a Variable
![Page 4: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/4.jpg)
Createaclass‐levelmethodfor8mercalledini8alize.Theonlycommandwe’llneedinthismethodisonethatsetsthevalueof8meLeS.Soclickon8meLeSanddragitintotheini8alizemethod.Setitsvalueto1fornow.
Step 3: Initialize Method
![Page 5: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/5.jpg)
Nowcreateanumberparameterinini8alizecalledamountOfTime.Draganddropitoverthe1inyoursetvaluetocommand.Nowwecansetthenumbertoadifferentvalueevery8meweusea8mer,withouthavingtochangetheini8alizecode.
Nowdragyourini8alizemethodintoworld.myfirstmethodsothatithappensrightwhenyourworldstarts.SetamountOfTimetoanynumberyouwant.
Step 3: Initialize Method
![Page 6: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/6.jpg)
Nowweneedtowriteamethodthatwilldecrementthe8meLeSvariable,andhaveourtextobjectdisplaythesecondsasthey8ckdown.Createanotherclass‐levelmethod,calledcountDown.DragaDoinorderinsidethemethod,andthendragaWhileloopinsidethat.
Step 4: Count Down Method
![Page 7: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/7.jpg)
Clickonworldinyourobjecttreeandthenclickonthefunc8onstab.Findthea>bbuRonundermath.
DragthatbuRonoverthetruepartofyourWhileloop;chooseanyvalues,wearegoingtoreplacethem.Nowfind8meLeSinthe8mer’sproper8estab,anddragitovera.
Step 4: Count Down Method
![Page 8: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/8.jpg)
DropaDoinorderinsidetheWhileloop.Nowweneedtochangethetextofourtextobjectevery8me8meLeSchanges.Clickon8merintheobjecttreeandthenclickontheproper8estab.YoushouldseethetextbuRon.
ClickthatbuRonanddragitintotheDoinorderinsidetheWhileloop.Setittodefaultstringfornow.
Step 4: Count Down Method
![Page 9: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/9.jpg)
Nowweneedtoturn8meLeSintoastring,sowecandisplayitwithourtextobject.Todothis,clickonworldandthenthefunc8onstab,andscrolldownun8lyouseewhatasastring.
Draganddropthatoverdefaultstring,andwhenthemenupopsup,selectexpressionsand8mer.8meLeS.
Setthedura8onofthiscommandto0secondssothatit’svalueissetinstantaneously.
Step 4: Count Down Method
![Page 10: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/10.jpg)
Nowweneedtomakesurethatittakesexactlyonesecondbeforethevalueof8meLeSisreset.DragtheWaitcommand,whichislocatedunderyourmethodeditor,intoyourDoinorderinyourWhileloopandsetitto1second.
Step 4: Count Down Method
![Page 11: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/11.jpg)
Clickon8merintheobjecttree,andthengototheproper8estab.Clickon8meLeSanddragitintoyourmethodeditorrightunderyourWaitcommand.Onthemenuthatpopsup,chosedecrement8mer.8meLeSby1.
Now,sothatthe8merisdecrementedinstantaneously,setthedura8onofthedecrementcommandto0seconds.
NowdragyourcountDownmethodintoworld.myfirstmethodunderyourini8alizemethodandplayyourworldtoseewhathappens.
Step 4: Count Down Method
![Page 12: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/12.jpg)
No2ce anything strange about your 2mer? No maAer how many seconds it starts with, it always stops at 1!
Thisiswhy:LookatyourWhilestatement.Itwillonlyrepeatitselfif8meLeSisgreaterthanzeroatthebeginningofthestatement.When8meLeSgetsdowntozero,theWhilestatementstopsandthetextobjectisneverreset.SoweneedtoaddacommandAFTERyourWhilestatementsothe8mergoesallthewaydownto0.
Step 5: Finishing Up
![Page 13: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/13.jpg)
Goto8merontheobjecttreeandthengototheproper8estab.FindthetextbuRonanddragitintoyourcountDownmethodunderyourWhilestatement.Reproducethesame8mersettexttocommandthatyouhaveinsideyourWhilestatement.Yourfinalcodewilllooklikethis:
Nowplayyourworldagain,andobservethatsweetsweet8merac8on!
Step 5: Finishing Up
![Page 14: Making a Timer in Alice - Duke Computer Science · command. On the menu that pops up, chose decrement mer.meLe by 1. Now, so that the mer is decremented instantaneously, set the duraon](https://reader034.fdocuments.net/reader034/viewer/2022042214/5eba916dd0210d5ea912f90b/html5/thumbnails/14.jpg)
This8mercanbeveryusefulforgamesinwhichyouhavetobeattheclock.Your8merwillneedtoberuninaDoTogetherwiththeothercodeforyourgame,orasaseparateeventinyourgame.
Youcanalsousetheseconceptstocreateascorekeeper(seemyscorekeepertutorialformoreinforma8on).
Step 5: Finishing Up