swift - Start animation when node is near -


i have 2 nodes textures (skspritenode) , i'm trying create animation when node near node y-axis (with range 100 - 0) , animation start @ moment. i'm trying cut rope, when candy near om-nom , opening mouth catch candy. me code please( sorry english

you achieve kind of calculation few line of code:

class gamescene: skscene {     var sprite1 : skspritenode!     var sprite2 : skspritenode!     var range : cgfloat = 0.0     override func didmovetoview(view: skview) {         self.range = randomcgfloat(0.0, max: 100.0)         sprite1 = skspritenode.init(color: skcolor.bluecolor(), size: cgsizemake(40,40))         sprite2 = skspritenode.init(color: skcolor.redcolor(), size: cgsizemake(40,40))         addchild(sprite1)         addchild(sprite2)         // try generate 10 positions , check relative distance         print("the range check :\(range)")         in 0...9 {             print("\(i)) time:")             let isnear = checkdistancebtwsprites()             if isnear {                 // whatever want sprites             }         }     }     func checkdistancebtwsprites()->bool{         sprite1.position = getrandomposition(40, maxx: 40, miny: 0, maxy: 320)         sprite2.position = getrandomposition(40, maxx: 40, miny: 0, maxy: 320)         let distance = getdistance((sprite1.position), p2: (sprite2.position))         print("distance sprite1 , sprite2 : \(distance)")         if distance <= self.range {             print("# -> success: sprite1 near sprite2")             return true         }         return false     }     func getrandomposition(minx: cgfloat, maxx: cgfloat, miny:cgfloat, maxy:cgfloat)->cgpoint {         return cgpointmake(randomcgfloat(minx, max: maxx),randomcgfloat(miny, max: maxy))     }     func randomcgfloat(min: cgfloat, max: cgfloat)->cgfloat {         return (cgfloat(arc4random()) / cgfloat(uint32_max)) * (max - min) + min     }     func getdistance(p1:cgpoint,p2:cgpoint)->cgfloat {         let xdist = (p2.x - p1.x)         let ydist = (p2.y - p1.y)         return cgfloat(sqrt((xdist * xdist) + (ydist * ydist)))     } } 

output:

enter image description here


Comments