swift - Why are objects in the same SKNode layer not interacting with each other? -
i have less 1 year using spritekit didn't use sknodes layers before until recently.
i have sknode layer holds of fish , user's position, example:
var layermaingame = sknode() layermaingame.zposition = 50 layermaingame.addchild(userposition) layermaingame.addchild(pipfish) addchild(layermaingame)
the interaction whether user touched fish or not handled function, checking if frames crossed:
if cgrectintersectsrect(cgrectinset(node.frame, delta.dx, delta.dy), self.userposition.frame) { print("you got hit \(name).") gameover() }
it works. interaction between userposition , pipfish works. doesn't work fish added game progresses. have function spawning different types of fish in intervals this:
func spawnnew(fish: skspritenode) { layermaingame.addchild(fish) }
the interaction between user , fish added same layer later in game not work. can pass right through them , no game on happens. when remove entire layermaingame variable , add them scene normal, interactions work. adding them same sknode layer doesn't work.
this function creates hit collision every fish.
func createhitcollisionfor(name: string, gameover gameover: string!, delta: (dx: cgfloat, dy: cgfloat), index: int = -1) { enumeratechildnodeswithname(name) { [unowned me = self] node, _ in if cgrectintersectsrect(cgrectinset(node.frame, delta.dx, delta.dy), self.userposition.frame) { me.gameoverimage.texture = sktexture(imagenamed: gameover) didgethitactions() me.runaction(audio.playsound(hit)!) if index != -1 { me.trophyset.encountertrophy.didencounter[index] = true } print("you got hit \(name).") } } }
and call this:
createhitcollisionfor("goldpiranha", gameover: model.gameoverimage["gold"], delta: (dx: 50, dy: 50), index: 1)
it works when fish not in layer, doesn't work when added layer.
when node placed in node tree, position property places within coordinate system provided parent.
sprite kit uses coordinate orientation starts bottom left corner of screen (0, 0), , x , y values increase move , right.
for skscene, default value of origin –
anchorpoint
(0, 0), corresponds lower-left corner of view’s frame rectangle. change center can specify (0.5, 0.5)for sknode, coordinate system origin defined
anchorpoint
default (0.5, 0.5) center of node.
in project have layermaingame
added example scene, anchorpoint default (0.5,0.5) origin children fish center, can see if change fish positions like:
func spawnnew(fish: skspritenode) { layermaingame.addchild(fish) fish.position = cgpointzero // position 0,0 = parent center }
hope understand how solve issue.
update: (after changes main question)
to better understand happens give example right away:
override func didmovetoview(view: skview) { var layermaingame = sknode() addchild(layermaingame) let pipfish = skspritenode(color: uicolor.yellowcolor(), size: cgsizemake(50,50)) pipfish.name = "son" self.addchild(pipfish) let layerpipfish = skspritenode(color: uicolor.yellowcolor(), size: cgsizemake(50,50)) layerpipfish.name = "son" layermaingame.addchild(layerpipfish) enumeratechildnodeswithname("son") { [unowned me = self] node, _ in print(node) } }
output:
now change line:
layermaingame.addchild(layerpipfish)
with:
self.addchild(layerpipfish)
output:
what happened?
as can see enumeratechildnodeswithname written , code print childs directly added self
(because launch enumeratechildnodeswithname
equal launch self.enumeratechildnodeswithname
)
how can search in full node tree?
if have node named "goldpiranha" you can search through descendants putting // before name. search "//goldpiranha":
enumeratechildnodeswithname("//goldpiranha") { [unowned me = self] ...
Comments
Post a Comment