swift - SKSpriteNode won't position after didBeginContact -
i'm working collision detection spritekit , swift. have skscene responds collision didbegincontact function:
func didbegincontact(contact: skphysicscontact) { if (contact.bodya.categorybitmask == collidertype.food && contact.bodyb.categorybitmask == collidertype.head) { placefoodinrandomgridlocation() } } func placefoodinrandomgridlocation() { let randomx = arc4random_uniform(uint32(mygrid.columncount)) let randomy = arc4random_uniform(uint32(mygrid.rowcount)) foodspriteholder.position = cgpoint(x: collines[int(randomx)], y: rowlines[int(randomy)]) }
the problem can adjust position of foodspriteholder before didbegincontact function fires. not move foodspriteholder when placefoodinrandomgridlocation called.
it seems scope issue. i'm not sure how isolate why position won't update. can make foodspriteholder visibility hidden in flow...so, know can access it.
for reference here how physics body setup food class item within foodspriteholder:
self.physicsbody = skphysicsbody(rectangleofsize: self.size, center: cgpointmake(reducedsize/2, reducedsize/2)) self.physicsbody?.affectedbygravity = false self.physicsbody?.categorybitmask = collidertype.food
lastly placefoodinrandomgridlocation function gets called...the position won't update.
thanks ideas, josh
the answer in case remove node when collision detected:
contact.bodya.node?.removefromparent()
now can create new node in new position. since affectedbygravity set false node wouldn't update position.
Comments
Post a Comment