gpt4 book ai didi

ios - SpriteKit touchesBegan 和 didBeginContact

转载 作者:行者123 更新时间:2023-11-29 00:21:24 25 4
gpt4 key购买 nike

我知道如何检测两个物体何时接触,并且我知道如何检测屏幕何时被触摸。但是,如果我想知道两个物体接触时屏幕是否被触摸怎么办?如果触摸发生在接触之前,但在接触期间则不起作用,touchesBegan 上的 bool 标志有效。

var screenTouch = false

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for _ in touches {
screenTouch = true
}
}

func didBegin(_ contact: SKPhysicsContact) {

let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

switch collision {
case PhysicsCategories.Ball | PhysicsCategories.Edge:

if screenTouch {
print("LAUNCH!")
}
etc.

最佳答案

您必须创建一个条件变量,该条件变量在 didBegin 时为 true,在 didEnd 时为 false。在您的 touchesBegin 中,当条件变量为 true 时完成您的操作。

var yourBodiesInContact = false

func didBegin(_ contact: SKPhysicsContact) {

let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

if collision == PhysicsCategory.Ball | PhysicsCategory.Edge {
yourBodiesInContact = true
}
}

func didEnd(_ contact: SKPhysicsContact) {
let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

if collision == PhysicsCategory.Ball | PhysicsCategory.Edge {
yourBodiesInContact = false
}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if yourBodiesInContact {
// while in contact
} else {
// ...
}
}

关于ios - SpriteKit touchesBegan 和 didBeginContact,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44158325/

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