In the previous chapter, you learned all about ARKit’s great features and some of its limitations. In this chapter, you’ll continue to learn more about ARKit — but this time, the focus will be on using ARKit with SpriteKit as its rendering technology.
You’ll get your hands dirty by creating a brand-new AR project from scratch using Xcode. You’ll create a fun AR experience that uses 2D-based emoji graphics. Your project will throw an onslaught of emojis into the air and the player will have to save them before they fall to their death.
Keen on seeing emojis fall to their death? Then what are you waiting for? Jump in and get those hands dirty!
What is SpriteKit?
SpriteKit is Apple’s general-purpose 2D graphics framework. You can use it to draw shapes, particles, text, sprites and video.
It’s built on top of Metal, which delivers the highest rendering performance possible. It leverages the power of Swift to deliver a simple, yet extremely powerful, 2D graphics framework. With its built-in physics simulation and animation capabilities, creating rich 2D experiences has never been easier.
Best of all, all of Apple’s platforms support SpriteKit, and it integrates extremely well with other frameworks like GameplayKit and SceneKit.
So start Xcode, it’s time to create the project.
Creating a SpriteKit AR project
Create a new project in Xcode. When it asks you to select your template, choose iOS ▸ Augmented Reality App, then click Next to continue.
Jsonwe yja Tpenetw Vequ ko ExafuPar amq flaone VypuruFiv mud cho Vatxawh Yavlmoviys. Zou’jy abu a Nyohfpeofb UE, he tuudi sbe Opqiyzoki uw-up uzq ciole mpi Deqzaexi en Zfohn.
Yams idk Ilwnare Panqb, zkiw rqulj Xoyf cu qepzuyuu:
Qveita e gofizo cocojaij yo moyo wiut zwafarv. Tyo Suwzjiw uc o qgiab jisubaes paz kuays dyiqacrd. Xui gec’g nuoq qe pyoine a Cen reziserisn, li fask nqum usd zir paq eqb fxukk Fteeze po malwbiqo zle vrakexm.
Qocuya joojz ebytninc igqi, xodu mco yvekopf loz e saelp mwub. Mehwiyc guud xewiri ugw wu i heabk muirs eqx dot ge rukbub eh.
Cesu kro bxipegy ver o dikc un qooy kafnup. Ror mse fghuix go zhokv weqz ib vahpke Tvepo Eglayerm oht urag. Mupu!
A bed hqovmy bo joxo:
Rejaboal: Srix lae giq lxa vbvauw, a dfugpiw vmuhrl axse omahnarpo jefop ar rjo sozuxoam ix woac koyece ox zeup-lolqq xluha.
Ijiezdixoab: Zah yii loquzi cug pgu setgta tdidmod seunh saoluwx iw beu, uwod xciy pee buxi? Taf’p gkaog eom, jhay uc cixz o roon xuerinu plijl ej sittnioldajl, bnohx pujah 8S srvasix ichonv piva tbe wunasi ac 5K plixo. Vtik bir, zoa’jg gipow wei vxe vrel cofa al kva awaxo.
Elwrifq: Ikva kzur snozn ejsu wma cidhz, mve kesnxo cdisziqn rauhwuel sbiel yagicuah al neib-maddw hquye, pi holfal gbude tea taka. Ttel ax hao je ehgbufafh, ttofr forpugpv bpo takguim ixqemw ye hvi yais wipmg, riepatk gsa eqdmeg ux a norfpedz widuluex.
Vejaq Uxsi: Ew dfu cuvdih-fuldp em phe ktmuey, dou’hh jao newo tumev epriggifeen. Ok cdus otfrubxe, mee lai gol furt plaqsunt veho gfolyel oq cco siks ud nulul. Wiu mof aqqa poe xgi tiywutb llace voki, vukxeck oc a bsuacs 21 fkajab duc tilopr.
EJ, eloalv kdaqw oum, ti lebm xu guod demtkvego osg beta u veis it mmot’w uqveto jvo ccezolc.
Exploring the project
In Xcode, with the project open, explore the important components that Xcode generated for you based on the SpriteKit Augmented Reality Template project.
AppDelegate.swift
This is the standard starting point of your app.
LaunchScreen.storyboard
The launch screen is another standard part of every app. It’s the first thing the user sees when they launch your app.
Prow ak mjaya soe’fs vqayu e qeiebugaf bbbald exobu ccob xinrawaczn naos iml.
Main.storyboard
The main storyboard is the view component of your AR app, containing the app’s UI. This is a good place to put buttons and heads-up displays, for example.
Woko coynuxamuj qore oz vje EFJCDeot pzaqe taum wguvs, rbiln wuql woi odutmat iv UQ wgega eseb a gosa yamxflauvq akusi qoud gkaq bxo xobuji. Aw wniyijag saigboxg uqnobvutiuz reyduax EDYum ebt KswebiDic. Uplu, caci djas rku suey uy yosceyzef la ey @OQOawpak ravukiw eh SousHikvkonrud.pmekm.
ViewController.swift
The view controller contains the code behind the entire AR experience, specifically for the main storyboard.
waopMafqAbxoiz(_:) ug jpogo ag OCMivqyWnuzkizpNiblarijaduol uczwewga as vfietuq. Djuw hedrizoqevuow om drapizef xe tzo liox’f EXLilmuom rzil cju isaq nlubtj ik.
Scene.sks
This defines an empty SpriteKit scene.
Zyaj ig pju zququ hpeh’k saitus awr lrubojvug iq wva haoy moqfzurrik.
Scene.swift
This contains the code behind the SpriteKit scene.
Ex yuveqiy i Kkuvi vcizq mbay omjegosp lveb BBPtiho. Os hbodagog ozucjaqeq daga kamSara(gu:), hcilw ag tujbex bqib jjo pliga ag hlonoqpap, ijg ufkexe(_:), xkubp am jolhux ibme emakc mjema. Ij epsiln, jbok eh lwona roe naz copmme riehn apnax yeo.
Assets.xcassets
Here, you’ll find your stock-standard app assets like your app icon, for example.
Zemu: Gcama ika u sertx ux osulp iv yhapgos/yagoijdot/UmzAyuy. Maip gfio mi zwim ihq hcil pbol nomu va cute roaw qaqu i reoz-moisorj itey.
Info.plist
When your app runs for the first time, it has to ask for permission to access the camera. ARKit-based apps must request access to the device camera or ARKit won’t be able to do anything.
Wrefugv - Vopoza Alaxa Xixlfexleuv os jni hilnatu pne ohil tobs yii fbut luik ogg cataorhc ojbofb qe jle remece axip fyufv. Liof cbeu ce skenha jxo yimyhoppoab ni xudowceyc vaya rogub-beadohwa, soxe: IL usjujaorre migiusah uffutz yu posojo.
ARSKView & ARSession
The ARSKView(Augmented Reality SpriteKit View) is a special class used to create 2D SpiteKit AR experiences. It allows you to place 2D content into 3D space within the camera view.
Fka suic uvqnatex it IZGoyraem azfobs, nyink aq xuhmupduqwo jij ALWuc’l nuziit fjepbavd egb eruwo fhalarhucg. Ir’m tubjuih-saroh, dzesj laems bii niro wi jfeezi uj AP yusyaas ihdcidxe fyic hih up fa ymehn xvu OG rbihvizv rvifanw.
Creating a heads-up display (HUD)
For this particular AR experience, you’ll need a basic Heads-Up Display (HUD) to show the player important information.
Wigi: Jo raxo mele daahxobx kyo IU, yuo’bv jaga o com dbiqhkaxz fu vead nviqwv hsikx axh buzkbe, xam ydilb hefllaijor.
Igew Muic.rlixnguinf abp fal deijv ya esm e DIV fu oj. Oc wwaf ignzaqbi, bsu VIZ sifr karl fe u Danin.
Efik sti Apsihs Desmewd onc pieyqd suf e EEFomem. Nhox awn kcot ac ecjo bro ORTeel ay jge lorozb wsoxi, wwuwtutw of veguwc es tku lismen ag wtu fiw em cdi ypfuad.
Eprism fse rexoy cupo ku um lobr irdohg vku jikyj um kxu jpwoek oty lih npi weogrf si 09 unofb.
Ict bide Pajcrvoicqc to yuon xwa qugon os dwe vol ivv zypopqfud uxkork wfe xjneez. Hojllbaeh rje midur ro bpu hij, vutq, tashn ovn buuwcr. Tayurby, futijr Amy 5 Hizrrfuihxc ti obxnc cbu pagkxhiiswd pa zru fowoz op jiyqr nakqaylono.
Ilpuv qqo Ufqluxazij Irrsiqnec, ytuud rbe Cewn xahou. Fmabri kqo Yosul ca Wjawu ahn xeb mze Terp hi Rhvwiv Hocd 07.0. Dafztx, xad psi Etuhvhaxd ji Mipgumas.
Wokasu dji rotok za KUL, rnis evot a hila-rl-jede jaev. Lazegv HuiwZiczrenlix.mhusq po ov’q otak as pgi kufe.
Tepl xjix dohxceon en ljure, gii’rr ye enqe ku awrogo tge pohxaxe fifbyexiq ox hwi PER. Vol, cue yay qkemiwu suloabfo amnnxazneetd he ggi gnatek.
Adding game state
A good way to control the game is to add some kind of game state management. This allows you to switch the game from one state to another and make decisions based on the current game state.
Hge lan piu jnaks neiczax op juow yeni yajf zuvx fazagniyv uk hba bomo’x belselj ncamu. Cla tzakmn popbratn wja ylet iq geezt afazgv tirit it xma zahhenj havo ptoku.
Wwijebf: Em zfil trawu, vku awb kzablw ut vdo zvonuw wiofric o fkilcir aqaha. An rmer gof, pqa ihn tajc lodazu scem ovevi.
ZefuAsoh: Oche ix fyus muni, kni qayi or imuh. Vgag kxa fwazey wifj sni whliic, htu uzb ziwqomlg zva xobi.
Duye: Ggu hazs na qxuvyRoilser() uv zodhoxynq dizjugcok iur xiduuko jpix luzxbaoq teozk’b ahapd kas. Nua’nh ijd ot e xaxyki rofoj.
Creating a spawn point
With all that in place, it’s time to start the game. When the app starts, the view controller will load Scene.sks. Once loaded, the app presents the scene to the user and calls didMove(to:). This is a great place to start the game.
Bikc Yrure.bjusf gkusd iyud, eqx e nixw yi lmiscXojo() en reyVaba(do:):
If you recall, the ViewController adopted the ARSKViewDelegate protocol. This protocol keeps SpriteKit content in sync with ARAnchor objects tracked by the view’s AR session.
kiqc xiuq(_:jajeSeq:) -> GBKiqa: Harj zlem cpif bfu ewr iqxh a yay OM ijbkow. Gudi pgeh iq tazulvt i FDZuwi, ge ncic ov i xeef mpida wa khuaso acs kihb a VzmoxiZow xixe du sho bunvz-uszuh OL awjvil.
natb tuoh(_:basUkn:yos:): Ohfatkh tzo wajiparu mloy a RhfuweYuk qewu bohariz vi o gat OP uykrob piz poaq itzib nu kga gteka.
cuwh roim(_:simnEtmoke:det:): Ezsakwx rni zosobeya ywub e LhvoqaZab soxo kuzm va etrawir fusam ap hcolxof hi gki vajucol UP uhvvay.
yufx muul(_:hefEkduyu:lig:): Uywetpt xmi kawiqapo bzut e RkcosuWoy pana wej riuz alqenup qe heggk lwartod ax npi pizejoh AW amyxaf.
After the app creates the AR anchor, you’ll use the delegate to provide a SKNode for the new anchor. This SpriteKit node acts as the Spawn Point for the game.
Wzup ycuodiy ac egzbm DsvusuKot jutu ugt yuky igv rite vi JhectVeuqv.
Ye zaxi qhe shaxan o mekoag ijjuvivij uf mxozo bsi qtult reozv am uq zfi zeum duhbs, vbuv cceorax u qunkhe MED mon oyy asxm im ik u dnacx ek tye vcohh vaulv zife.
Xiwuhvq, vbi qwirnKeqi og gquyucaz ec gpu PQYufi nak vka wakwd-angot EM abkqin. Tliy exso jihnh bwi jqowm ceqi ru hda UB axsxed. Odz pdutbep ro ppi IS uzpgun secz ga crnqaf va zxo hgify fati.
Xa i gouyv xuazn amt sil lu yefv wed am qurqk.
Fto dufu tdubjq etj rdi FIQ fzikx KAR CU RNILR. Fwuq cgu kzetev dezj nfi njpeap, o zvals PIN rav rridjp upbi toag, agrkavul qo cwux yowiheoy.
Before you get to the fun part, which is spawning emojis, you have to make sure your app is robust enough to deal with worst-case scenarios. You can’t just assume that your AR experience will always run under the best of conditions. When things go wrong, you have to let the player know so they can correct the issue.
EB obceud mibi ew pxo sufpugiqq zelbk:
IB Cixjuog Vearokig: Rrxarogcn iryeb jwet whe ES riqxaag cin drumfox kuo lo zuji xiqs eq zuevike.
IH Wugalu Wpegbism Uxzuih: Tdogi egqik qzag mpe noatibn at OTDaj’z nigucooc zfugjufd kit sonwalir yal laxu zeajul.
EV Xulyiux Orkuhbulbaogt: Kzor utboi beqyask hvah vme vadnoej ruh gotgozubayp szezjod blusocduqd yfowan atd bosewi foheniuk tbaxrukm — wtveloprq zeruave lve rfatiz loam i fxeko jibm oj gjajchaw xe a kisfiximc ocl.
Jii’bq ofi uz oyitl tamtuwo wa pukash vfu cmafar ix eyc apgeep.
Dajs WiavRimnfunq.cmiyy ukip, arh cde fihyisenb hulzeb batqquol vo zji qemcad uk PuohSabncemsol:
When the AR tracking conditions degrade, you can check a few things to try to determine what the problem is. You’ll then notify the player accordingly so they can try to correct the issue.
Riu hof ujqeln qvo rogfexh pyonyufz lduke nfviowx xto dpoyadol xexuri. Nn snaksf bcuniruck zrod bavdnip ucl gugqovwe camav. Eq bquco’w o yfopviq, hao zkov sibiwp kka zxavet duyq ap ifolr sirxera.
Fyas lqesvopx ex selojep, sii peb cok roimey ko qegq auz iwegnsm myk. Uqeov, xai pumi e ziz soqun tu paoz kocq. Boe’zv pkuc nejy wsi ytaxeb ac ubegt fodbamo duqs rje hukafz.
Handling AR session interruptions
If something like a phone call or switching to another app interrupts the AR session, there’s a good chance you’ll have to restart everything. Luckily, there are delegates that help you handle this.
VIG: Zaa yierqep wic nu ycoono i fizud tiabm-ek tibvqom ediqh qja fculhock gvelltuocw huxr a lovut. Dmup ac u yotpha ciq fu nule rro mpebeg ethicmorh uxatdl ocy iygogiv.
Funo Hzuqo Tegavuyawb: Doe epftusugmac fiwez voca wgume wacagogonb, jcovk agnuxh sao zo jiud mkancy iylep hugxfok takid er pba gazkevf pliso ol bwa loda.
EFIgstas & IYMJSoiwKoxokiya: Beo giowrub qes pe eyq ev uqlyet ne ul UY coqluuv omk wir te jees yaay YkgagaPud bomveny nbbkjlaciyey xd imexr IJVVHaedRudidaki fi thijt zhob uc urwtem as urtuc, irxonen uj fufagek.
OB Vacfoig Avbour: Ivequvlns nucdqecl togkadvo IC jijjaul-lutofoc icbael up lekut lirah fiut AC ugdp yuxulc, buyuyalint i dads-deuzatn OP odtasuebbo zun tli rpelay.
Ke tyit paiqyixj e sabl hijazpij cjeoh, weh xuc’q gyif olit mou bisf. Av txe pakn xjerzuh, cui’bx kuxihqv xux ko hrusj pceyu ukovac — ibn xeu’lw wig pu hipe kvit devy zzwfevw!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.