Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using...

26
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

Transcript of Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using...

Page 1: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

MakingObjectsMoveinUnison:UsingLists

ByDeborahNelsonDukeUniversity

ProfessorSusanRodgerJuly13,2008UpdatedJune2014byYossra Hamid

EditedforAlice3usebyAnhTrinhJuly2016

Page 2: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Overview•Thistutorialwillteachyouhowtouselists.Alistisasortofholdingplaceforarowofobjects.

•Withalist,youcanmakeseveralobjectsdothesamethingatthesametimewithouthavingtowriteoutcodeforeachone.Youcanalsohavethemdothesamethingoneobjectatatime.

•Wewillusealisttomakeagroupofstuffedtigerstoperformsomeactionsatthesametime,andthenoneatatime.

Page 3: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

LoadingtheWorld

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

• ClickontheCameraMarkersmenu.ClickAddCameraMarker.NamethecameravieworiginalPosition

Page 4: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

AddingtheObjects

• GototheBipedclassesthenaddfourStuffedTiger objectstoyourworld.

• Addtwootherpeopletoyourworld.

• I’veaddedtheSnowMan andSnowWomanfromtheGallery.

Page 5: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Part1:MakingaList

• Wewillusealist tomakeallfourofthetigersjumpupanddowntogetherandthenkickandspinoneatatime.

• ClickonScenetabinthetopoftheeditor,andthenclickonAddSceneProperty… undertheunmanagedsection.

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

Page 6: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list
Page 7: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Makingalist(cont1)

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

ØSeethescreenshotonthenextslideforanillustration.

Page 8: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list
Page 9: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Part2:Writingthemethod• Dragaeachin_togetherintomyFirstMethod.• Foritemtype:ChooseGalleryClassandcheckonBipedinthemiddlecolumnlikepreviouslythenclickOK.

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

Page 10: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list
Page 11: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Writingthemethod(cont1)

Ø Eachin_togethermeansthatalloftheitemsinthelistwillperformtheinstructionsthatwewilladdatthesametime.

Page 12: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

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

• Clickonadddetail…tochangethedurationto0.25seconds.

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

• Playyourworld.Yourmethodshouldlooklikethis:

Page 13: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

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

Page 14: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

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

• Wewantthetigerstoeachturntheirleftlegandspinoneatatime.

• However,theonetiger isjustaplaceholder,sowecannottellonetiger toturnitsleftleg.

• Wewillhavetouseaclass-levelbuiltinfunctiontoturnonlyapartoftheobject.

Page 15: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Foreachin_(cont 2)

• Selectonetiger intheobjecttree.• Hoveryourmouseattherightarrowsignnexttoonetiger intheobjecttree,thenclickononetiger getLeftHip

Page 16: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list
Page 17: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

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

Ø Seethescreenshotonthenextslideforanillustration.

Page 18: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list
Page 19: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

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

Page 20: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

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

• YourForeachin_codeshouldlooklikethissofar:

Page 21: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Thecompletemethod• Hereisthecompletemethod.Don’tforgettocommentyourcode!

Page 22: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Playyourworldnow

Page 23: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Step4:Objectswithdifferentparts• Sofar,ourlistonlyhastigersinit.Addthetwootherpeopleobjectstothelist:

1) ClickonScenetabonthetopoftheeditor2) Underunmanagedsection, clickonthebox

nexttothetigerslist3) ClickonCustomArray… andselecttheobject

youwanttoadd.Ø Seethescreenshotonthenextslideforan

illustration.

Page 24: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list
Page 25: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Warning• Youcan’taddanon-bipedobjecttoalistofBipedobjects

• Addingthesnowman andsnowwoman tothelistworksbecausetheyareBipedandbothhaveleftHip asthelabelforoneoftheirbodyparts.

• IfyourcharacterdoesnothaveapartnamedleftHip youmightgetanerrorbecausetheworldissearchingforaleftHip parttomovethatisnotthere.

Page 26: Making Objects Move in Unison: Using Lists · 13/7/2008  · Making Objects Move in Unison: Using Lists By Deborah Nelson Duke University Professor Susan Rodger July 13 ... A list

Recap• Listsareusefulforhavingmultiplecharactersperform

thesamesetofactions.• Eachin_together meanseverythinginthelistwill

performatthesametime.• Foreachin_meanseverythinginthelistwillperforman

actiononerightaftertheother.• Tofindthesub-partsofanobject,clickonthearrownext

totheobjectintheobjecttree.Makesureallofthecharactershavethesameparts,labeledthesameway.

• WARNING:Donotaddtoomanyobjectstoyourlist,becausedoingsowillslowyourworlddownsignificantly.