gpt4 book ai didi

ios - 二元运算符 '<' 不能应用于类型为 'CGPoint' 和 'Int' 的操作数使用 Guard

转载 作者:行者123 更新时间:2023-11-28 12:15:18 25 4
gpt4 key购买 nike

在我的游戏中,我有一个可以滑动的球。我试图做到这一点,如果用户不能将球拖到某个点以上。但是,如果他们滑动并意外进入该点,则球仍会移动。每当我放 guard location < 100 else {return} ,错误:

"Binary operator '<' cannot be applied to operands of type 'CGPoint' and 'Int'"

出现了,我不知道如何解决。顺便说一下,guard使球在用户不小心越过线时仍保持其速度?

这是我的代码:

for touch in touches {
let location = touch.location(in: self)
let node = self.atPoint(location)

guard location < 100 else { return }
if ball.frame.contains(location) {
touchPoint = location
touching = true
}
}

最佳答案

错误是告诉你哪里出了问题。 CGPoint 是一个具有 xy 值的结构。将一个点与单个整数值进行比较没有意义。

您需要将点的 x 或 y 位置与某个值进行比较,而不是整个点。

    guard location < 100 else { return }

可能会读

    guard location.y < 100 else { return }

相反。

关于ios - 二元运算符 '<' 不能应用于类型为 'CGPoint' 和 'Int' 的操作数使用 Guard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46841065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com