gpt4 book ai didi

swift - 如何检测节点上的触摸

转载 作者:搜寻专家 更新时间:2023-11-01 06:30:55 24 4
gpt4 key购买 nike

我有一个应用程序,它每 1 秒在屏幕上生成一次球。现在,我希望用户触摸那些使它们消失的球(removeFromParent())。据我所知,我必须通过 touchesBegan 设置触摸功能,我这样做了,这是我的代码:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches{
let positionOfTouch = touch.location(in: self)
enumerateChildNodes(withName: "BALL") { (node: SKNode, nil) in
if positionOfTouch == node.position {
print("just touched the ball")
}
else{
print("error")
}
}
}

问题是,当我触摸屏幕/球时,控制台打印错误而不是刚刚触摸球,这意味着我的代码不起作用。此外,控制台将错误消息打印为我认为的球数。我不明白我做错了什么以及如何真正设置此功能。这是我的 createBall 函数,它从我的 BallNode 类(类型 SKShapeNode)实现:

    func createBall(){
let ball = BallNode(radius: 65)
print(ball.Name)
print(ball._subName!)

ball.position.y = ((frame.size.height) - 200)
let ballXPosition = arc4random_uniform(UInt32(frame.size.width)) // set the ball a randon position from the top of the screen
ball.position.x = CGFloat(ballXPosition)

ball.physicsBody?.categoryBitMask = PhysicsCategory.ball // ball's category bitMask
ball.physicsBody?.collisionBitMask = PhysicsCategory.ball // prevent objects from intersecting
ball.physicsBody?.contactTestBitMask = PhysicsCategory.topBorder // when need to know if two objects touch each other

addChild(ball)

}

你能帮我吗?因为我是 swift 的新手,所以我也想获得有关此触摸检测的一些解释(以及一般的触摸 - 苹果文档很差)。

最佳答案

每次您触摸屏幕时,您都会在所有球之间循环,看看您是否正在触摸其中一个球。如果屏幕上有 50 个球,它会遍历所有球以查看您是否正在触摸 1。这不是确定您是否正在触摸 1 的有效方法。

有很多方法可以做到这一点,但我会做的是处理 Ball 类内部的触摸。这样你就不必弄清楚你是否正在触摸一个球以及它可能是哪个球。

Explanation of protocol (to the best of my ability) this may seem a little much right now, but the faster you learn and understand protocols that better off you will be (IMO).

In this example we will use a protocol to setup a delegate of the BallNode class. A protocol is a set user defined "rules" that must be followed by any class that you designate compliant to that protocol. In my example I state that for a class to be compliant to the BallNodeDelegate protocol it must contain the didClick func. When you add the BallNodeDelegate after GameScene you are stating that this class will be compliant to that protocol. So if in GameScene you did not have the didClick func it will cause an error. All this is put in place so that you have an easy way to communicate between your BallNode instances and your GameScene class (without having to pass around references to your GameScene to each BallNode). Each BallNode then has a delegate (GameScene) which you can pass back the information to.

在你的 BallNode 类中确保你有 isUserInteraction = true

在 BallNode 类之外创建一个将触摸信息发送回 GameScene 的协议(protocol)

protocol BallNodeDelegate: class {
func didClick(ball: BallNode)
}

在您的 BallNode 类中创建一个委托(delegate)变量

weak var delegate: BallNodeDelegate!

移动触摸开始给你 BallNode 类

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

self.delegate?.didClick(ball: self)
}

在 GameScene 中添加对 BallNode 协议(protocol)的遵守

class GameScene: SKScene, BallNodeDelegate

在 GameScene 中创建 Ball 时确保设置它的委托(delegate)

let ball = BallNode()
ball.delegate = self

在 GameScene 中添加嵌套。处理点击的函数

func didClick(ball: BallNode) {
print("clicked ball")
}

关于swift - 如何检测节点上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47399572/

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