小编eco*_*guy的帖子

调用可以抛出,但不能从全局变量初始化程序中抛出错误

我正在使用Xcode 7 beta,在迁移到Swift 2后,我遇到了一些代码问题:

let recorder = AVAudioRecorder(URL: soundFileURL, settings: recordSettings as! [String : AnyObject])
Run Code Online (Sandbox Code Playgroud)

我得到一个错误说"调用可以抛出,但错误不能从全局变量初始化程序中抛出".我的应用程序依赖于它recorder是一个全局变量.有没有办法让它保持全球化,但解决这些问题?我不需要高级错误处理,我只是想让它工作.

swift swift2

10
推荐指数
2
解决办法
5574
查看次数

为什么update()首选游戏逻辑而不是didFinishUpdate?

在SpriteKit的编程指南中,update()函数被称为实现自己的游戏逻辑的最佳位置.但是当我意识到在一个节点上居中相机在didFinishUpdate()中工作得更好(避免延迟)时,我一直在使用该选项.

camera.position.y = node.position.y
Run Code Online (Sandbox Code Playgroud)

害怕其他延迟问题我也实现了其余的游戏逻辑.有用.因此,他们为什么推荐update()?使用这两种方法是否有性能优势?

谢谢.

ios sprite-kit swift

5
推荐指数
1
解决办法
137
查看次数

检测SKAction是否已完成运行,Swift

我想在运行一个动作后更改变量的值,而不是在它运行期间.这是我的代码:

   override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    if gameNotStarted {
        if firePosition.x != 320 || firePosition.y > 280 {  
            fire.runAction(fireButtonReturn)
            iapButton.runAction(iapButtonReturn)
            aboutButton.runAction(abtButtonReturn)

            if fire.hasActions() == false {
                println("has no actions")
                firePosition.x = 320
                firePosition.y = 280
            } 
        }       
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在运行的三个操作具有完全相同的持续时间,所以我只需要一个来检查它们是否已经完成运行.他们已经运行完毕之后,我想改变的数值firePosition.xfirePosition.y.非常重要的是,火灾位置值在动作运行后完全改变,绝对不是在动作完成运行之前.

但是根据我目前的代码,我从不if fire.hasActions() ...根据结果​​和控制台运行该部分.我发现了类似的问题,他们是在obj-c.谢谢你的帮助.

ios sprite-kit swift

3
推荐指数
1
解决办法
2494
查看次数

用CGPaths和CAShapeLayers动画绘制字母

我目前正在使用此代码为角色的绘制设置动画:

var path = UIBezierPath()


    var unichars = [UniChar]("J".utf16)
    var glyphs = [CGGlyph](count: unichars.count, repeatedValue: 0)

    let gotGlyphs = CTFontGetGlyphsForCharacters(font, &unichars, &glyphs, unichars.count)
    if gotGlyphs {
        let cgpath = CTFontCreatePathForGlyph(font, glyphs[0], nil)
         path = UIBezierPath(CGPath: cgpath!)

    }
Run Code Online (Sandbox Code Playgroud)

从字符创建bezierpath(在本例中为"J").获取跟踪iOS UIFont中的角色的路径

然后我创建一个CAShapeLayer()并添加动画.

 let layer = CAShapeLayer()
   layer.position = //CGPoint
    layer.bounds = //CGRect()
    view.layer.addSublayer(layer)

    layer.path = path.CGPath

    layer.lineWidth = 5.0
    layer.strokeColor = UIColor.blackColor().CGColor
    layer.fillColor = UIColor.clearColor().CGColor
    layer.geometryFlipped = true

    layer.strokeStart = 0.0
    layer.strokeEnd = 1.0

    let anim = CABasicAnimation(keyPath: "strokeEnd") …
Run Code Online (Sandbox Code Playgroud)

core-animation uikit cgpath ios swift

2
推荐指数
1
解决办法
1534
查看次数

标签 统计

swift ×4

ios ×3

sprite-kit ×2

cgpath ×1

core-animation ×1

swift2 ×1

uikit ×1