Ale*_*lex 3 ios sprite-kit swift
我正在尝试使用SpriteKit模拟眼睛。
眼睛的瞳孔在屏幕上移动时跟踪用户的手指,但必须保持在眼睛的范围内。
我尝试使用SKConstraint解决此问题,但未成功。
编辑
我的想法是将SKConstraints应用于瞳孔以将其边界限制在眼睛范围内。任何触摸(即touchesMoved()等)都将以SKAction.moveTo()的形式应用于瞳孔,并且SpriteKit手柄将瞳孔保持在视线范围内。
let touchPoint = CGPoint()
SKAction.moveTo( touchPoint, duration: 2)
Run Code Online (Sandbox Code Playgroud)
该视频的代码可用:https://gist.github.com/anonymous/f2356e07d1ac0e67c25b1940662d72cb
一张图片胜过千言万语...
想象瞳孔是白色的小圆圈。蓝色框模拟用户在屏幕上移动手指。
理想情况下,瞳孔遵循屏幕周围的蓝色框,并遵循由黄色圆圈定义的路径。
iOS 10 | 雨燕3 | Xcode 8
您可以使用方向约束来旋转节点以使其面向触摸位置,而无需按距离进行约束。通过将具有x偏移量的“瞳孔”节点添加到受约束的节点,瞳孔将朝着触摸点移动。这是如何执行此操作的示例:
let follow = SKSpriteNode(color:.blue, size:CGSize(width:10,height:10))
let pupil = SKShapeNode(circleOfRadius: 10)
let container = SKNode()
let maxDistance:CGFloat = 25
override func didMove(to view: SKView) {
addChild(container)
// Add the pupil at an offset
pupil.position = CGPoint(x: maxDistance, y: 0)
container.addChild(pupil)
// Node that shows the touch point
addChild(follow)
// Add an orientation constraint to the container
let range = SKRange(constantValue: 0)
let constraint = SKConstraint.orient(to: follow, offset: range)
container.constraints = [constraint]
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let location = t.location(in: self)
follow.position = location
adjustPupil(location: location)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let location = t.location(in: self)
follow.position = location
adjustPupil(location: location)
}
}
// Adjust the position of the pupil within the iris
func adjustPupil(location:CGPoint) {
let dx = location.x - container.position.x
let dy = location.y - container.position.y
let distance = sqrt(dx*dx + dy*dy)
let x = min(distance, maxDistance)
pupil.position = CGPoint(x:x, y:0)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
414 次 |
| 最近记录: |