making a timer in alice - duke university

14
Making a Timer in Alice By Jenna Hayes under the direc8on of Professor Susan Rodger Duke University July 2008 www.cs.duke.edu/csed/alice/aliceInSchools

Upload: others

Post on 09-Feb-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

MakingaTimerinAlice

ByJennaHayesunderthedirec8onofProfessorSusanRodger

DukeUniversityJuly2008

www.cs.duke.edu/csed/alice/aliceInSchools

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

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

Createaclass‐levelmethodfor8mercalledini8alize.Theonlycommandwe’llneedinthismethodisonethatsetsthevalueof8meLeS.Soclickon8meLeSanddragitintotheini8alizemethod.Setitsvalueto1fornow.

Step 3: Initialize Method

Nowcreateanumberparameterinini8alizecalledamountOfTime.Draganddropitoverthe1inyoursetvaluetocommand.Nowwecansetthenumbertoadifferentvalueevery8meweusea8mer,withouthavingtochangetheini8alizecode.

Nowdragyourini8alizemethodintoworld.myfirstmethodsothatithappensrightwhenyourworldstarts.SetamountOfTimetoanynumberyouwant.

Step 3: Initialize Method

Nowweneedtowriteamethodthatwilldecrementthe8meLeSvariable,andhaveourtextobjectdisplaythesecondsasthey8ckdown.Createanotherclass‐levelmethod,calledcountDown.DragaDoinorderinsidethemethod,andthendragaWhileloopinsidethat.

Step 4: Count Down Method

Clickonworldinyourobjecttreeandthenclickonthefunc8onstab.Findthea>bbuRonundermath.

DragthatbuRonoverthetruepartofyourWhileloop;chooseanyvalues,wearegoingtoreplacethem.Nowfind8meLeSinthe8mer’sproper8estab,anddragitovera.

Step 4: Count Down Method

DropaDoinorderinsidetheWhileloop.Nowweneedtochangethetextofourtextobjectevery8me8meLeSchanges.Clickon8merintheobjecttreeandthenclickontheproper8estab.YoushouldseethetextbuRon.

ClickthatbuRonanddragitintotheDoinorderinsidetheWhileloop.Setittodefaultstringfornow.

Step 4: Count Down Method

Nowweneedtoturn8meLeSintoastring,sowecandisplayitwithourtextobject.Todothis,clickonworldandthenthefunc8onstab,andscrolldownun8lyouseewhatasastring.

Draganddropthatoverdefaultstring,andwhenthemenupopsup,selectexpressionsand8mer.8meLeS.

Setthedura8onofthiscommandto0secondssothatit’svalueissetinstantaneously.

Step 4: Count Down Method

Nowweneedtomakesurethatittakesexactlyonesecondbeforethevalueof8meLeSisreset.DragtheWaitcommand,whichislocatedunderyourmethodeditor,intoyourDoinorderinyourWhileloopandsetitto1second.

Step 4: Count Down Method

Clickon8merintheobjecttree,andthengototheproper8estab.Clickon8meLeSanddragitintoyourmethodeditorrightunderyourWaitcommand.Onthemenuthatpopsup,chosedecrement8mer.8meLeSby1.

Now,sothatthe8merisdecrementedinstantaneously,setthedura8onofthedecrementcommandto0seconds.

NowdragyourcountDownmethodintoworld.myfirstmethodunderyourini8alizemethodandplayyourworldtoseewhathappens.

Step 4: Count Down Method

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

Goto8merontheobjecttreeandthengototheproper8estab.FindthetextbuRonanddragitintoyourcountDownmethodunderyourWhilestatement.Reproducethesame8mersettexttocommandthatyouhaveinsideyourWhilestatement.Yourfinalcodewilllooklikethis:

Nowplayyourworldagain,andobservethatsweetsweet8merac8on!

Step 5: Finishing Up

This8mercanbeveryusefulforgamesinwhichyouhavetobeattheclock.Your8merwillneedtoberuninaDoTogetherwiththeothercodeforyourgame,orasaseparateeventinyourgame.

Youcanalsousetheseconceptstocreateascorekeeper(seemyscorekeepertutorialformoreinforma8on).

Step 5: Finishing Up