MakingObjectsMoveinUnison: UsingLists ByDeborahNelson DukeUniversity ProfessorSusanRodgerJuly13,2008 UpdatedJune2014byYossra Hamid EditedforAlice3usebyAnhTrinhJuly2016 Overview •Thistutorialwillteachyouhowtouselists.Alist isasortofholdingplaceforarowofobjects. •Withalist,youcanmakeseveralobjectsdothe samethingatthesametimewithouthavingto writeoutcodeforeachone.Youcanalsohave themdothesamethingoneobjectatatime. •Wewillusealisttomakeagroupofstuffedtigers toperformsomeactionsatthesametime,and thenoneatatime. LoadingtheWorld • Openanewworld,withanytemplate. • Saveitinadirectorythatyoucanfindagain. • Afteryouhaveopenedthefilegointothe layoutmodebyclickingontheSetupScene button(bottomrightcornerofanimation screen). • ClickontheCameraMarkersmenu.ClickAdd CameraMarker.Namethecameraview originalPosition AddingtheObjects • GototheBipedclasses thenaddfour StuffedTiger objectsto yourworld. • Addtwootherpeople toyourworld. • I veaddedthe SnowMan and SnowWomanfromthe Gallery. Part1:MakingaList • Wewillusealist tomakeallfourofthetigersjump upanddowntogetherandthenkickandspinoneat atime. • ClickonScenetabinthetopoftheeditor,andthen clickonAddSceneProperty… undertheunmanaged section. • Onceaboxappears,setthevaluetypetoGallery Class,thenclickonBipedintheSelection columnin themiddle.Youwillsee6checkmarksintheobject columnontheleft.Thatmeansthelistcancontains anybipedcharacterswehaveadded.ClickOK. Makingalist(cont1) • Checktheisarraybox • Namethelistastigers. • Clickontheinitializer ->CustomArray… ->add thefourtigersin ØSeethescreenshotonthenextslideforan illustration. Part2:Writingthemethod • Dragaeachin_togetherintomyFirstMethod. • Foritemtype:ChooseGalleryClassandcheck onBipedinthemiddlecolumnlikepreviously thenclickOK. • Nametheitematiger • Forarray:choosethis.characters,itwillshow asthis.tigers Writingthemethod(cont1) Ø Eachin_togethermeansthatalloftheitemsinthe listwillperformtheinstructionsthatwewilladdat thesametime. Eachin_together(cont 1) • Choose atiger inthedropdownmenuontheleft,drag turntofacetotheeachintogetherblock, andthen selectthis.camera. • Clickonadddetail…tochangethedurationto0.25 seconds. • Enterinthecodebelowbyselectingthemove command,firstup1meter,thendown1meter.Make eachcommand sduration.25seconds. • Playyourworld.Yourmethodshouldlooklikethis: Step2: inorder • Nowdragaforeachin_underneaththeeach in_together block, chooseBiped asitemtype, onetiger astheitemname,andthis.tigers asthe array Forallinorder(cont1) ØForeachin_meansthetigerswillperformall oftheinstructionsthatweaddinsideofit, onerightaftertheother(intheorderthat theywereaddedtothelist). • Wewantthetigerstoeachturntheirleftleg andspinoneatatime. • However,theonetiger isjustaplaceholder,so wecannottellonetiger toturnitsleftleg. • Wewillhavetouseaclass-levelbuiltin functiontoturnonlyapartoftheobject. Foreachin_(cont 2) • Selectonetiger intheobjecttree. • Hoveryourmouseattherightarrowsignnext toonetiger intheobjecttree,thenclickon onetiger getLeftHip • Draginturn,thenbackward,then¼ revolution.Setthedurationto.25seconds. Ø Seethescreenshotonthenextslideforan illustration. • Selectonetiger fromtheobjecttreeagain. • Drag intotheForeachin_ againbelowthe leftHip command,andselectturn,then right, then 1revolution.Setthedurationto.25 seconds. Foreachin_(cont 2) • Thenchooseonetiger andselectonetiger getLeftHip likewedidpreviouslyanddrag anotherturn,then forward,then ¼ revolution.Setthedurationto.25seconds. • YourForeachin_codeshouldlooklikethisso far: Thecompletemethod • Hereisthecompletemethod.Don tforgetto commentyourcode! Playyourworldnow Step4:Objectswithdifferentparts • Sofar,ourlistonlyhastigersinit.Addthetwo otherpeopleobjectstothelist: 1) ClickonScenetabonthetopoftheeditor 2) Underunmanagedsection, clickonthebox nexttothetigerslist 3) ClickonCustomArray… andselecttheobject youwanttoadd. Ø Seethescreenshotonthenextslideforan illustration. Warning • Youcan’taddanon-bipedobjecttoalistof Bipedobjects • Addingthesnowman andsnowwoman tothe listworksbecausetheyareBipedandboth haveleftHip asthelabelforoneoftheirbody parts. • Ifyourcharacterdoesnothaveapartnamed leftHip youmightgetanerrorbecausethe worldissearchingforaleftHip parttomove thatisnotthere. Recap • Listsareusefulforhavingmultiplecharactersperform thesamesetofactions. • Eachin_together meanseverythinginthelistwill performatthesametime. • Foreachin_meanseverythinginthelistwillperforman actiononerightaftertheother. • Tofindthesub-partsofanobject,clickonthearrownext totheobjectintheobjecttree.Makesureallofthe charactershavethesameparts,labeledthesameway. • WARNING:Donotaddtoomanyobjectstoyourlist, becausedoingsowillslowyourworlddownsignificantly.
© Copyright 2026 Paperzz