Swift 中的 Flappy Bird 编码错误 [英] Flappy Bird Coding Error in Swift

查看:17
本文介绍了Swift 中的 Flappy Bird 编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xcode 6.1 中遇到了我的飞扬鸟项目的编码错误.

代码是允许小鸟旋转,它是:

bird.zRotation = self.acotarMinMax(-1, max: 0.3, valor:bird.physicsBody?.velocity.dy * (bird.physicsBody?.velocity.dy <0 ? 0.003 : 0.001))

错误发生在dy下,内容如下:

可选类型CGFloat?"的值未解开;你的意思是使用!"或者 '?'?

我该如何纠正错误,或者有其他方法可以生成旋转吗?

我完全是 Swift 的初学者,所以我很难弄清楚这是语法问题还是与更新版本有关.

我从在线教程中获得了代码,它在视频中有效.

解决方案

阅读 Swift Optional 类:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html>

所以看起来 dy 是一个 Optional 值,这意味着它要么有一个值,要么没有,但直到你研究它(如 Schroedinger's Cat)你才知道.所以如果你需要这个值并且你知道它不会是 nil,你可以通过编写 bird.physicsBody?.velocity.dy! 来打开它,它会查看box"并取出值但如果里面没有任何东西就会崩溃.如果你写dy?,它会查看盒子,但如果盒子是空的,它会忽略它.

关于可选链的更多信息(这就是你所做的):https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html

I've encountered a coding error for my flappy bird project, in Xcode 6.1.

The code is to allow rotation of the bird, and it reads:

bird.zRotation = self.acotarMinMax(-1, max: 0.3, valor: bird.physicsBody?.velocity.dy * (bird.physicsBody?.velocity.dy < 0 ? 0.003 : 0.001))

the error occurs under dy, it reads:

value of optional type 'CGFloat?' not unwrapped; did you mean to use '!' or '?'?

How can I correct the error, or is there another way to generate rotation?

I am a total beginner of Swift, so I'm having a hard time figuring out if this is a syntax problem or something to do with the updated version.

I obtained the code from a online tutorial, and it worked in the video.

解决方案

Read up on the Swift Optional class: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html

So it seems that dy is an Optional value, which means that it either has a value or it does not, but you don't know until you look into it (like Schroedinger's Cat). So if you need this value and you know it will not be nil, you unwrap it by writing bird.physicsBody?.velocity.dy! which will look into the "box" and take the value out but crash if there is nothing inside. If you write dy? it will look into the box but ignore it if the box is empty.

More on Optional Chaining (which is what you do) here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html

这篇关于Swift 中的 Flappy Bird 编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆