这里的新编码器,我制作了一个用于拖动/投掷对象的代码,我想知道如何制作它以便只有部分屏幕可用于触摸,即只能触摸屏幕的下半部分。这是我的gamecene代码,我使用的是Swift 2.0。
"post_id" "meta_value"
"46211" "7000"
"46212" "7000"
"46213" "7000"
"46214" "7000"
"46215" "7000"
"46216" "7000"
"46217" "7000"
"46218" "7000"
"46219" "7000"
"46220" "7000"
"46221" "7000"
"46222" "7000"
"46223" "7000"
"46224" "7000"
"46225" "7000"
"46226" "7000"
"46227" "7000"
"46228" "7000"
"46229" "7000"
"46268" "7000"
"30305" "7500"
"30435" "7500"
"30599" "7500"
"30603" "7500"
"30597" "7900"
"30971" "8000"
"46232" "8000"
"46236" "8000"
"30579" "8300"
"30433" "8500"
"30437" "8500"
"30755" "8500"
"30860" "8500"
"36192" "8500"
"46171" "8500"
"45026" "8900"
"30426" "8950"
"29909" "9000"
"30124" "9000"
"30384" "9000"
"30608" "9000"
"30436" "9500"
"46054" "9540"
"30344" "9750"
"30805" "9800"
"30014" "9900"
"30015" "9900"
"30016" "9900"
"30017" "9900"
"46153" "9900"
"46154" "9900"
"46155" "9900"
"46156" "9900"
"46288" "9900"
"29982" "10000"
"30038" "10000"
"30057" "10000"
"30223" "10000"
"30429" "10000"
"30434" "10000"
"30493" "10000"
"30698" "10000"
"30839" "10000"
"45558" "10000"
"45717" "10000"
"46241" "10000"
"46267" "10000"
"46287" "10900"
"36204" "11000"
"30529" "11390"
"30728" "11500"
"46250" "11500"
"46102" "11700"
"30371" "12000"
"30703" "12000"
"30858" "12000"
"46141" "12000"
"46142" "12000"
"46240" "12000"
"46260" "12000"
"30258" "12500"
"30308" "12500"
"45009" "12500"
"46094" "12900"
"43295" "13000"
"45908" "13000"
"46253" "13200"
"46254" "13200"
"30795" "13750"
"36199" "13900"
"46247" "13900"
"46292" "13900"
"30386" "14000"
"46251" "14200"
"36226" "14500"
"30580" "14900"
"30877" "14900"
"30887" "14900"
"46098" "14900"
"46151" "14900"
"43221" "14950"
"45477" "14950"
"30185" "14995"
"133" "15000"
"29902" "15000"
"29997" "15000"
"30019" "15000"
"30041" "15000"
"30042" "15000"
"30043" "15000"
"30044" "15000"
"30528" "15000"
"30583" "15000"
"30814" "15000"
"30849" "15000"
"30889" "15000"
"30918" "15000"
"30919" "15000"
"45462" "15000"
"45713" "15000"
"46010" "15000"
"46286" "15000"
"30732" "15500"
"46143" "15500"
"46144" "15500"
"46145" "15500"
"46146" "15500"
"46147" "15500"
"46148" "15500"
"30331" "15900"
"30275" "16000"
"30704" "16000"
"30944" "16100"
"30930" "16200"
"789" "16384"
"30927" "16400"
"30946" "16400"
"30465" "16500"
"46237" "16500"
"46239" "16500"
"30948" "16600"
"30712" "16700"
"44906" "16900"
"45173" "16900"
"45328" "16900"
"28" "17000"
"29975" "17000"
"30059" "17000"
"31031" "17000"
"30945" "17200"
"30947" "17200"
"30931" "17300"
"30314" "17500"
"30926" "17500"
"30968" "17500"
"30969" "17500"
"30888" "17800"
"30939" "17800"
"30941" "17800"
"30182" "17900"
"43136" "19900"
"44929" "19900"
"45458" "19900"
"46118" "19900"
"46152" "19900"
"30742" "19950"
"30265" "20000"
"30385" "20000"
"30388" "20000"
"30683" "20000"
"30832" "20000"
"30847" "20000"
"30848" "20000"
"31284" "20000"
"43156" "20000"
"45931" "20000"
"45943" "20000"
"46001" "20000"
"46187" "20000"
"46188" "20000"
"46191" "20000"
"46192" "20000"
"46193" "20000"
"46284" "20000"
"29977" "20500"
"29996" "21000"
"30582" "21000"
"30809" "21000"
"45779" "21000"
"45852" "21000"
"29851" "21400"
"29875" "21900"
"30380" "21900"
"32478" "21900"
"46150" "21900"
"30266" "22000"
"30267" "22000"
"30396" "22000"
"30864" "22000"
"32101" "22000"
"30135" "22500"
"30136" "22500"
"30334" "22500"
"30921" "22500"
"29109" "22528"
"30933" "22800"
"30029" "22900"
"30030" "22900"
"30031" "22900"
"43211" "22900"
"30885" "23000"
"164" "23200"
"45451" "23312"
"29907" "23500"
"29948" "23500"
"29951" "23500"
"30033" "23500"
"30035" "23500"
"30419" "23500"
"43125" "23500"
"45587" "23500"
"46244" "23500"
"46064" "23600"
"30937" "23800"
"29984" "23900"
"45970" "23990"
"30225" "24000"
"30227" "24000"
"30228" "24000"
"30229" "24000"
"30243" "24000"
"30280" "24000"
"30807" "24000"
"30899" "24000"
"36206" "24000"
"36207" "24000"
"36208" "24000"
"36209" "24000"
"36210" "24000"
"36211" "24000"
"36212" "24000"
"36213" "24000"
"36214" "24000"
"36215" "24000"
"36216" "24000"
"45532" "24000"
"45533" "24000"
"46003" "24000"
"46025" "24000"
"29983" "24500"
"30011" "24500"
"30338" "24500"
"30935" "24800"
"29167" "24900"
"29628" "24900"
"30156" "24900"
"30157" "24900"
"30158" "24900"
"30159" "24900"
"30161" "24900"
"30162" "24900"
"30163" "24900"
"30164" "24900"
"30186" "24900"
"30244" "24900"
"30348" "24900"
"36232" "24900"
"43416" "24900"
"45295" "24900"
"29897" "25000"
"29898" "25000"
"29899" "25000"
"29900" "25000"
"30034" "25000"
"30109" "25000"
"30260" "25000"
"30352" "25000"
"30377" "25000"
"30450" "25000"
"30531" "25000"
"30532" "25000"
"30533" "25000"
"30534" "25000"
"30535" "25000"
"30536" "25000"
"30537" "25000"
"30538" "25000"
"30539" "25000"
"30540" "25000"
"30541" "25000"
"30542" "25000"
"30543" "25000"
"30544" "25000"
"30545" "25000"
"30546" "25000"
"30547" "25000"
"30548" "25000"
"30549" "25000"
"30550" "25000"
"30551" "25000"
"30552" "25000"
"30559" "25000"
"30663" "25000"
"30692" "25000"
"30693" "25000"
"30694" "25000"
"30695" "25000"
"30769" "25000"
"30883" "25000"
"30959" "25000"
"30960" "25000"
"30961" "25000"
"30962" "25000"
"31037" "25000"
"31327" "25000"
"32339" "25000"
"36193" "25000"
"36194" "25000"
"36195" "25000"
"36510" "25000"
"43124" "25000"
"45017" "25000"
"29974" "29900"
"30032" "29900"
"30160" "29900"
"30165" "29900"
"30166" "29900"
"30194" "29900"
"30500" "29900"
"30574" "29900"
"30696" "29900"
"30715" "29900"
"30719" "29900"
"30757" "29900"
"30835" "29900"
"30875" "29900"
"30876" "29900"
"32515" "29900"
"36191" "29900"
"45020" "29900"
"45739" "29900"
"45781" "29900"
"45861" "29900"
"45920" "29900"
"46043" "29900"
"46093" "29900"
"46243" "29900"
"30745" "29950"
"356" "30000"
"29712" "30000"
"29912" "30000"
"29913" "30000"
"29914" "30000"
"29922" "30000"
"30173" "30000"
"30402" "30000"
"30949" "30000"
"31109" "30000"
"31339" "30000"
"45006" "30000"
"45012" "30000"
"45014" "30000"
"45720" "30000"
"46000" "30000"
"29879" "30900"
"179" "31000"
"180" "31000"
"46149" "31000"
"46166" "31300"
"32473" "31500"
"307" "31900"
"32562" "31950"
"998" "32000"
"30339" "32000"
"36324" "32000"
"29186" "32500"
"30657" "38000"
"31185" "38000"
"43017" "38000"
"43474" "38000"
"30074" "38300"
"30078" "38375"
"29877" "38400"
"30069" "38400"
"30070" "38425"
"30060" "38500"
"30061" "38500"
"30062" "38500"
"30067" "38500"
"30068" "38500"
"30076" "38500"
"30077" "38500"
"30395" "38500"
"43039" "38500"
"30075" "38550"
"30066" "38700"
"30071" "38700"
"29272" "38900"
"30065" "38900"
"30072" "38900"
"32404" "38900"
"29355" "39000"
"30073" "39000"
"30221" "39000"
"30492" "39000"
"30818" "39000"
"31330" "39000"
"31420" "39000"
"32414" "39000"
"45010" "39000"
"45314" "39000"
"45443" "39000"
"45464" "39000"
"46293" "39000"
"30825" "39367"
"30401" "39500"
"30424" "39500"
"30784" "39500"
"45538" "39500"
"45539" "39500"
"45654" "39500"
"45681" "39500"
"30886" "39800"
"49" "39900"
"97" "39900"
"748" "39900"
"1025" "39900"
"1095" "39900"
"29481" "39900"
"29686" "39900"
"29701" "39900"
"30045" "39900"
"30115" "39900"
"30153" "39900"
"30154" "39900"
"30171" "39900"
"30231" "39900"
"30232" "39900"
"30233" "39900"
"30234" "39900"
"30235" "39900"
"30239" "39900"
"30250" "39900"
"30251" "39900"
"30252" "39900"
"30253" "39900"
"30254" "39900"
"30288" "39900"
"30451" "39900"
"30575" "39900"
"30828" "39900"
"31009" "39900"
"36080" "39900"
"36704" "39900"
"36770" "39900"
"43405" "39900"
"45249" "39900"
"45525" "39900"
"45578" "39900"
"45619" "39900"
"45786" "39900"
"45874" "39900"
"45876" "39900"
"46092" "39900"
"46112" "39900"
"46266" "39900"
"30281" "39990"
"142" "40000"
"30003" "40000"
"30004" "40000"
"30119" "40000"
"30601" "40000"
"31421" "40000"
"32072" "40000"
"45835" "42000"
"45870" "42000"
"45001" "42064"
"30023" "42500"
"30236" "42500"
"30249" "42500"
"30333" "42500"
"30686" "42500"
"30737" "42500"
"30738" "42500"
"30762" "42500"
"30874" "42500"
"32499" "42500"
"46194" "42500"
"46195" "42500"
"46196" "42500"
"884" "42900"
"30662" "42900"
"30664" "42900"
"30666" "42900"
"30667" "42900"
"36529" "42900"
"43155" "42900"
"46106" "42900"
"46113" "42900"
"32619" "43000"
"43058" "43000"
"44904" "43000"
"45074" "43000"
"45194" "43000"
"45476" "43000"
"32570" "43400"
"30128" "43500"
"30000" "44000"
"30026" "44000"
"30741" "44000"
"46074" "44290"
"29883" "44300"
"30558" "44330"
"30896" "44500"
"46197" "44500"
"46198" "44500"
"45589" "44851"
"29279" "44900"
"30332" "44900"
"30841" "44900"
"44974" "44900"
"45307" "44900"
"29408" "44950"
"216" "45000"
"603" "45000"
"731" "45000"
"29887" "45000"
"29888" "45000"
"29889" "45000"
"29890" "45000"
"45857" "45000"
"45911" "45000"
"45983" "45000"
"46172" "45000"
"46173" "45000"
"46174" "45000"
"46175" "45000"
"46176" "45000"
"46177" "45000"
"46178" "45000"
"46202" "45000"
"46242" "45000"
"46271" "45000"
"36066" "45360"
"94" "45900"
"29979" "45900"
"30670" "45900"
"30671" "45900"
"30672" "45900"
"30673" "45900"
"30674" "45900"
"30675" "45900"
"30676" "45900"
"30677" "45900"
"31081" "45900"
"46099" "46000"
"29876" "46200"
"29733" "47000"
"30130" "47000"
"30740" "47000"
"36175" "47000"
"43143" "47000"
"44924" "47000"
"45126" "47000"
"45656" "47430"
"103" "47500"
"30649" "47500"
"30716" "47500"
"30964" "47500"
"31155" "47500"
"43414" "47500"
"45883" "47650"
"30678" "47900"
"30679" "47900"
"30680" "47900"
"30681" "47900"
"30682" "47900"
"43008" "47900"
"44883" "47900"
"739" "48000"
"30893" "48000"
"32232" "48000"
"32295" "48000"
"36607" "48000"
"43257" "48000"
"45872" "48000"
"30470" "48200"
"30594" "48500"
"31034" "48500"
"31064" "48500"
"31782" "48500"
"30826" "48520"
"43117" "50000"
"45411" "50000"
"45419" "50000"
"45584" "50000"
"45804" "50000"
"45805" "50000"
"46095" "50000"
"46134" "50000"
"46135" "50000"
"46264" "50000"
"46269" "50000"
"45815" "50500"
"36238" "50688"
"30089" "51000"
"30226" "51000"
"36732" "51000"
"45682" "51350"
"30005" "51500"
"30006" "51500"
"30090" "52000"
"30187" "52000"
"30776" "52000"
"30779" "52000"
"30782" "52000"
"45141" "52000"
"45418" "52000"
"45586" "52000"
"30116" "52250"
"43126" "52250"
"45479" "52250"
"29919" "52400"
"45436" "52400"
"30237" "52500"
"30238" "52500"
"30246" "52500"
"30248" "52500"
"30730" "52500"
"30731" "52500"
"30774" "52500"
"30775" "52500"
"30778" "52500"
"30780" "52500"
"30781" "52500"
"30789" "52500"
"30790" "52500"
"31730" "52500"
"32443" "52500"
"30602" "52680"
"31182" "52800"
"30036" "53000"
"30214" "53000"
"32325" "53000"
"45460" "53000"
"30709" "53528"
"29292" "53600"
"43470" "53600"
"29920" "53700"
"29921" "53700"
"29926" "53700"
"29927" "53700"
"29928" "53700"
"45437" "53700"
"45438" "53700"
"45439" "53700"
"45440" "53700"
"45441" "53700"
"1077" "54000"
"30085" "54000"
"36266" "54000"
"45881" "54000"
"44982" "54400"
"30646" "54500"
"30561" "54850"
"654" "54900"
"911" "54900"
"30347" "54900"
"31104" "54900"
"31564" "54900"
"32005" "54900"
"36469" "54900"
"43071" "54900"
"45834" "54900"
"29443" "54950"
"30616" "54960"
"81" "55000"
"30399" "70000"
"30880" "70000"
"31462" "70000"
"36314" "70000"
"36685" "70000"
"43219" "70000"
"45022" "70000"
"45621" "70000"
"45649" "70000"
"46201" "70000"
"30506" "70128"
"32580" "70500"
"46013" "70560"
"30515" "70750"
"30557" "70750"
"30204" "70875"
"30353" "71000"
"31079" "71000"
"32159" "71250"
"954" "71500"
"32358" "71500"
"45495" "71500"
"29792" "71900"
"215" "72000"
"31156" "72000"
"31324" "72000"
"32181" "72000"
"32586" "72000"
"36517" "72000"
"42994" "72000"
"44977" "72000"
"45736" "72000"
"45836" "72000"
"29162" "72400"
"886" "72500"
"29111" "72500"
"29670" "72500"
"43170" "99975"
"44921" "99999"
"86" "100000"
"255" "100000"
"29325" "100000"
"30411" "100000"
"30815" "100000"
"30904" "100000"
"31230" "100000"
"31874" "100000"
"36083" "100000"
"45042" "100000"
"45543" "100000"
"45657" "100000"
"46005" "100000"
"46044" "100000"
"46045" "100000"
"46046" "100000"
"46091" "100000"
"45115" "101000"
"46067" "101600"
"29794" "102000"
"46048" "102600"
"45933" "102933"
"31508" "103000"
"31731" "103000"
"31972" "110000"
"32141" "110000"
"32233" "110000"
"32342" "110000"
"36357" "110000"
"36474" "110000"
"36705" "110000"
"45292" "110000"
"30840" "110175"
"30406" "110187"
"45655" "110187"
"29138" "110900"
"313" "111000"
"45687" "111000"
"847" "111900"
"29466" "111900"
"45499" "111900"
"29698" "112000"
"30387" "112000"
"31117" "112000"
"31906" "112000"
"32517" "112000"
"36767" "112000"
"45134" "112000"
"45591" "112000"
"30861" "112500"
"30987" "112500"
"32550" "112500"
"36377" "112500"
"36641" "112500"
"31550" "112900"
"32149" "112900"
"45772" "112912"
"30404" "113000"
"45937" "113000"
"45214" "113900"
"517" "114000"
"31727" "114000"
"45704" "114432"
"101" "114500
"32493" "120000"
"36338" "120000"
"36340" "120000"
"36371" "120000"
"36561" "120000"
"44996" "120000"
"45155" "120000"
"45612" "120000"
"45757" "120000"
"46116" "120000"
"46061" "120215"
"614" "120500"
"31502" "120500"
"45737" "120897"
"751" "121000"
"1074" "121000"
"32190" "121000"
"46087" "121497"
"46123" "121500"
"30129" "121800"
"36183" "121800"
"29758" "151500"
"30643" "151588"
"45753" "151686"
"30122" "152000"
"44928" "152000"
"29221" "152500"
"31806" "152900"
"45917" "152900"
"36535" "153000"
"45854" "153000"
"30710" "153300"
"29597" "153400"
"36523" "153400"
"36502" "153700"
"46086" "153700"
"30519" "153900"
"43013" "153900"
"31303" "154000"
"43070" "154030"
"29328" "154500"
"36150" "154500"
"412" "154900"
"444" "154900"
"646" "154900"
"29260" "154900"
"29289" "154900"
"29717" "154900"
"29740" "154900"
"29819" "154900"
"31353" "154900"
"36364" "154900"
"32320" "199500"
"45729" "199500"
"253" "199800"
"254" "199800"
"19" "199900"
"369" "199900"
"629" "199900"
"630" "199900"
"636" "199900"
"877" "199900"
"891" "199900"
"958" "199900"
"30863" "340000"
"31207" "340000"
"43187" "340900"
"43190" "340900"
"43194" "340900"
"36673" "341000"
"43304" "341000"
"36669" "341500"
"31626" "342000"
"36202" "342000"
"36388" "342000"
"221" "342250"
"43476" "342500"
"160" "343000"
"31088" "343000"
"36672" "343900"
"36534" "343950"
"44958" "344000"
"30383" "344500"
"45638" "344500"
"29810" "344800"
"46" "344900"
"554" "344900"
"29435" "344900"
"31020" "344900"
"480" "345000"
"30697" "345000"
"31458" "595000"
"36406" "596900"
"45565" "598000"
"61" "599000"
"322" "599000"
"29603" "599000"
"30467" "599000"
"31174" "599000"
"31410" "599000"
"31521" "599000"
"31720" "599000"
"36745" "599000"
"43102" "599000"
"31582" "599500"
"850" "599800"
"31417" "599900"
"36545" "599900"
"45083" "600000"
"45425" "600000"
"45721" "600000"
"1151" "609900"
"32193" "609900"
"976" "612000"
"32424" "615000"
"43421" "615000"
"44975" "615000"
"298" "619500"
"419" "621212"
"30447" "621212"
"43313" "624900"
}
答案 0 :(得分:1)
听起来你想忽略屏幕上半部分的触摸。如果是这样,您可以将检查添加到touchesBegan
和touchesMoved
,如下所示:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first as UITouch!
let location = touch.locationInNode(self)
guard location.y < self.frame.midY else {
return
}
let sprite = StarNode.star(touch.locationInNode(self))
touchPoint = location
touching = true
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first as UITouch!
let location = touch.locationInNode(self)
guard location.y < self.frame.midY else {
// You might want set touching to false here, depends on the rest of your code
return
}
touchPoint = location
}
我添加的新代码检查触摸的Y位置是否小于场景高度的一半,假设原点位于左下角。如果检查失败,则退出该功能 - 您可能希望添加一些代码来取消那里的触摸操作。
如果您希望屏幕的某个部分是不可触摸的,则可以制作CGRect
并检查触摸位置是否始终在矩形之外,例如:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first as UITouch!
let location = touch.locationInNode(self)
let untouchableArea = CGRect(x: 100, y: 100, width: 200, height: 150)
guard !untouchableArea.contains(location) else {
return
}
// Could do something similar in touchesMoved to guard against moving into the untouchable area
}
答案 1 :(得分:0)
Alessandro Ornano的回应将起作用 - 分析触摸的位置。
另一种方法是使用“触摸屏阻挡器”。由于SpriteKit仅注册最顶部的触摸,因此您可以放置一个使用UIColor.clear颜色的SKSpriteNode,并将其isUserInteractionEnabled设置为true,并将其z位置设置得足够高,使其高于其他可触摸内容。这将形成一个“吃”触摸的透明区域。
(一种方便的调试/可视化技术,在开发过程中,给它一个微弱的透明色而不是完全清晰,所以你可以看到受影响的区域以及是否有任何精灵在阻挡区域上方偷偷摸摸。 )
这样做的优点是更简单,并且可以轻松关闭和打开功能或轻松更改受影响的区域。但它与亚历山德罗的方法也存在一些差异。例如,它不会阻止进入其区域的拖拽;一旦在触摸屏阻挡区域外建立了拖动,即使您将手指拖过触摸屏障区域,它也将继续接收touchesMoved事件。任何一种方法都可行。您只需选择最适合您特定需求的那个。