简体   繁体   中英

Swift - SpriteKit - distance between a moving node and a fixed node

I want to set the width of a node to be the distance between a moving node and a fixed node. In other words, I hope that the width of the node changes as another node moving. Is there a function to do this ?

Use the hypot function:

let distance = hypot(movingNode.position.x - fixedNode.Position.x, movingNode.position.y - fixedNode.Position.y)

This returns the length of the hypotenuse of a right-angle triangle of width x and height y, which we can envisage as being drawn with your two nodes being on the non-right angled vertices.

https://developer.apple.com/documentation/coregraphics/1456251-hypot

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM