making objects move in unison: using lists...jul 13, 2008  · for each in_(cont2) • then choose...

13
Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13, 2008 Updated June 2014 by Yossra Hamid Edited for Alice 3 use by Anh Trinh July 2016 Overview This tutorial will teach you how to use lists.A list is a sort of holding place for a row of objects. With a list, you can make several objects do the same thing at the same time without having to write out code for each one. You can also have them do the same thing one object at a time. We will use a list to make a group of stuffed tigers to perform some actions at the same time, and then one at a time.

Upload: others

Post on 07-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

MakingObjectsMoveinUnison:UsingLists

ByDeborahNelsonDukeUniversity

ProfessorSusanRodgerJuly13,2008UpdatedJune2014byYossra Hamid

EditedforAlice3usebyAnhTrinhJuly2016

Overview•Thistutorialwillteachyouhowtouselists.Alistisasortofholdingplaceforarowofobjects.

•Withalist,youcanmakeseveralobjectsdothesamethingatthesametimewithouthavingtowriteoutcodeforeachone.Youcanalsohavethemdothesamethingoneobjectatatime.

•Wewillusealisttomakeagroupofstuffedtigerstoperformsomeactionsatthesametime,andthenoneatatime.

Page 2: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

LoadingtheWorld

• Openanewworld,withanytemplate.• Saveitinadirectorythatyoucanfindagain.• AfteryouhaveopenedthefilegointothelayoutmodebyclickingontheSetupScenebutton(bottomrightcornerofanimationscreen).

• ClickontheCameraMarkersmenu.ClickAddCameraMarker.NamethecameravieworiginalPosition

AddingtheObjects

• GototheBipedclassesthenaddfourStuffedTiger objectstoyourworld.

• Addtwootherpeopletoyourworld.

• I�veaddedtheSnowMan andSnowWomanfromtheGallery.

Page 3: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Part1:MakingaList

• Wewillusealist tomakeallfourofthetigersjumpupanddowntogetherandthenkickandspinoneatatime.

• ClickonScenetabinthetopoftheeditor,andthenclickonAddSceneProperty… undertheunmanagedsection.

• Onceaboxappears,setthevaluetypetoGalleryClass,thenclickonBipedintheSelection columninthemiddle.Youwillsee6checkmarksintheobjectcolumnontheleft.Thatmeansthelistcancontainsanybipedcharacterswehaveadded.ClickOK.

Page 4: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Makingalist(cont1)

• Checktheisarraybox• Namethelistastigers.• Clickontheinitializer ->CustomArray… ->addthefourtigersin

ØSeethescreenshotonthenextslideforanillustration.

Page 5: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Part2:Writingthemethod• Dragaeachin_togetherintomyFirstMethod.• Foritemtype:ChooseGalleryClassandcheckonBipedinthemiddlecolumnlikepreviouslythenclickOK.

• Nametheitematiger• Forarray:choosethis.characters,itwillshowasthis.tigers

Page 6: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Writingthemethod(cont1)

Ø Eachin_togethermeansthatalloftheitemsinthelistwillperformtheinstructionsthatwewilladdatthesametime.

Eachin_together(cont 1)• Choose atiger inthedropdownmenuontheleft,dragturntofacetotheeachintogetherblock, andthenselectthis.camera.

• Clickonadddetail…tochangethedurationto0.25seconds.

• Enterinthecodebelowbyselectingthemovecommand,firstup1meter,thendown1meter.Makeeachcommand�sduration.25seconds.

• Playyourworld.Yourmethodshouldlooklikethis:

Page 7: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Step2: inorder• Nowdragaforeachin_underneaththeeachin_together block, chooseBiped asitemtype,onetiger astheitemname,andthis.tigers asthearray

Forallinorder(cont1)ØForeachin_meansthetigerswillperformalloftheinstructionsthatweaddinsideofit,onerightaftertheother(intheorderthattheywereaddedtothelist).

• Wewantthetigerstoeachturntheirleftlegandspinoneatatime.

• However,theonetiger isjustaplaceholder,sowecannottellonetiger toturnitsleftleg.

• Wewillhavetouseaclass-levelbuiltinfunctiontoturnonlyapartoftheobject.

Page 8: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Foreachin_(cont 2)

• Selectonetiger intheobjecttree.• Hoveryourmouseattherightarrowsignnexttoonetiger intheobjecttree,thenclickononetiger getLeftHip

Page 9: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

• Draginturn,thenbackward,then¼revolution.Setthedurationto.25seconds.

Ø Seethescreenshotonthenextslideforanillustration.

Page 10: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

• Selectonetiger fromtheobjecttreeagain.• Drag intotheForeachin_ againbelowtheleftHip command,andselectturn,then right,then 1revolution.Setthedurationto.25seconds.

Foreachin_(cont 2)• Thenchooseonetiger andselectonetigergetLeftHip likewedidpreviouslyanddraganotherturn,then forward,then ¼revolution.Setthedurationto.25seconds.

• YourForeachin_codeshouldlooklikethissofar:

Page 11: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Thecompletemethod• Hereisthecompletemethod.Don�tforgettocommentyourcode!

Playyourworldnow

Page 12: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Step4:Objectswithdifferentparts• Sofar,ourlistonlyhastigersinit.Addthetwootherpeopleobjectstothelist:

1) ClickonScenetabonthetopoftheeditor2) Underunmanagedsection, clickonthebox

nexttothetigerslist3) ClickonCustomArray… andselecttheobject

youwanttoadd.Ø Seethescreenshotonthenextslideforan

illustration.

Page 13: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Warning• Youcan’taddanon-bipedobjecttoalistofBipedobjects

• Addingthesnowman andsnowwoman tothelistworksbecausetheyareBipedandbothhaveleftHip asthelabelforoneoftheirbodyparts.

• IfyourcharacterdoesnothaveapartnamedleftHip youmightgetanerrorbecausetheworldissearchingforaleftHip parttomovethatisnotthere.

Recap• Listsareusefulforhavingmultiplecharactersperform

thesamesetofactions.• Eachin_together meanseverythinginthelistwill

performatthesametime.• Foreachin_meanseverythinginthelistwillperforman

actiononerightaftertheother.• Tofindthesub-partsofanobject,clickonthearrownext

totheobjectintheobjecttree.Makesureallofthecharactershavethesameparts,labeledthesameway.

• WARNING:Donotaddtoomanyobjectstoyourlist,becausedoingsowillslowyourworlddownsignificantly.