Swift,sprite kit 游戏 : Have circle disappear in clockwise manner? 定时器?

标签 swift timer xcode6 sprite-kit

好吧,所以我不知道它的名字,但我有一个 sprite kit 游戏(跑酷游戏),当游戏结束时,会有一个“拯救我”按钮和一个用完的计时器因此。当计时器用完时,您将无法再单击按钮来保存角色。

不过,我不想在文本中显示此计时器 - 我想要一个“自行展开”的圆圈,如果您愿意的话,它会以计时器用完的速度消失。 IE。当计时器到达 0 时,圆圈已完全消失。根据计时器,圆圈以顺时针运动的方式逐渐消失。

这里有一些图片来解释我在说什么。 enter image description here

我该怎么做?

最佳答案

通过以固定间隔更改 SKShapeNodepath 属性,您可以创建逐帧动画序列。要创建动画,请将 path 属性设置为一系列形状,这些形状以圆圈开始,什么都没有结束。您可以使用 UIBezierPathCGPath 的包装器,使用以下步骤为动画创建形状:

  1. 将路径的“笔”移到圆心
  2. addArcWithCenter 向路径添加弧线从 startAngleendAngle
  3. 在路径上添加一条线,从圆上对应终止角的点到圆心
  4. endAngle 改变一个固定的量
  5. 重复步骤 1-4

下面是上述步骤的实现:

override func didMove(to:SKView) {

    let circle = SKShapeNode(circleOfRadius: 50)
    circle.fillColor = SKColor.blue
    circle.strokeColor = SKColor.clear
    circle.zRotation = CGFloat.pi / 2
    addChild(circle)

    countdown(circle: circle, steps: 20, duration: 5) {
        print("done")
    }
}

// Creates an animated countdown timer
func countdown(circle:SKShapeNode, steps:Int, duration:TimeInterval, completion:@escaping ()->Void) {
    guard let path = circle.path else {
        return
    }
    let radius = path.boundingBox.width/2
    let timeInterval = duration/TimeInterval(steps)
    let incr = 1 / CGFloat(steps)
    var percent = CGFloat(1.0)

    let animate = SKAction.run {
        percent -= incr
        circle.path = self.circle(radius: radius, percent:percent)
    }
    let wait = SKAction.wait(forDuration:timeInterval)
    let action = SKAction.sequence([wait, animate])

    run(SKAction.repeat(action,count:steps-1)) {
        self.run(SKAction.wait(forDuration:timeInterval)) {
            circle.path = nil
            completion()
        }
    }
}

// Creates a CGPath in the shape of a pie with slices missing
func circle(radius:CGFloat, percent:CGFloat) -> CGPath {
    let start:CGFloat = 0
    let end = CGFloat.pi * 2 * percent
    let center = CGPoint.zero
    let bezierPath = UIBezierPath()
    bezierPath.move(to:center)
    bezierPath.addArc(withCenter:center, radius: radius, startAngle: start, endAngle: end, clockwise: true)
    bezierPath.addLine(to:center)
    return bezierPath.cgPath
}

和一段视频:

enter image description here

关于Swift,sprite kit 游戏 : Have circle disappear in clockwise manner? 定时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32038703/

相关文章:

ios - 将 String 转换为 Double,然后再转换回 String

azure - 计时器触发器 Azure Function 停止工作,没有任何更改

ios-simulator - 紧凑型和常规尺寸等级之间的区别

XCode 接口(interface)构建器缺少 View 层次结构

ios - UIScrollView 和自动布局不起作用

ios - Facebook SDK 是否支持 Swift 2.3/3.0?

swift - 为什么在从 Int 形成字符串时会出现 optional

ios - Swift:无法将类型 '[Any]' 的值转换为指定类型 'NSString'

c# - 在 Windows 7 中更改屏幕保护程序设置时应用程序卡住 - System.Threading.Timer 是罪魁祸首?

java - 定时器触发后唤醒 Android 屏幕