gpt4 book ai didi

ios - didBeginContact 未被调用

转载 作者:可可西里 更新时间:2023-11-01 01:44:03 24 4
gpt4 key购买 nike

我有 2 个 SKSpriteNode 需要检测其接触。我尝试了各种方法并查找了很多东西,但似乎无法得到答案。下面是我的代码。 controlCircle 是一个类级别的变量,因为它需要在其他方法中使用。对象生成良好。

class GameScene: SKScene, SKPhysicsContactDelegate {

var controlCircle = SKSpriteNode()
var mainCategory : UInt32 = 1 << 0
var dropCategory: UInt32 = 1 << 1



override func didMoveToView(view: SKView) {

self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0,0)

//CREATING FIRST OBJECT AND ADDING PHYSICS BODY
var mainRadius = 20.0;
var controlCircle = SKSpriteNode(color: UIColor.clearColor(), size:CGSizeMake(mainRadius * 2, mainRadius * 2))

var circleBody = SKPhysicsBody(circleOfRadius: mainRadius)
circleBody.dynamic = false
circleBody.usesPreciseCollisionDetection = true

controlCircle.physicsBody = circleBody

var bodyPath = CGPathCreateWithEllipseInRect(CGRectMake((controlCircle.size.width/2), controlCircle.size.height/2, controlCircle.size.width, controlCircle.size.width),
nil)

var circleShape = SKShapeNode()
circleShape.fillColor = UIColor.brownColor()
circleShape.lineWidth = 0
circleShape.path = bodyPath
controlCircle.addChild(circleShape)
controlCircle.position = CGPointMake(self.frame.width/2, self.frame.height/2)

self.addChild(controlCircle)
controlCircle.physicsBody.categoryBitMask = mainCategory
controlCircle.physicsBody.contactTestBitMask = dropCategory


//CREATING SECOND OBJECT AND ADDING PHYSICS BODY
var radius = 10.0;
var drop = SKSpriteNode(color: UIColor.clearColor(), size:CGSizeMake(radius * 2, radius * 2))

var dropBody = SKPhysicsBody(circleOfRadius: radius)
dropBody.dynamic = false
dropBody.usesPreciseCollisionDetection = true

drop.physicsBody = dropBody

var dropPath = CGPathCreateWithEllipseInRect(CGRectMake((drop.size.width/2), drop.size.height/2, drop.size.width, drop.size.width),
nil)

var dropShape = SKShapeNode()
dropShape.fillColor = UIColor.blackColor()
dropShape.lineWidth = 0

drop.name = "dropMask"
dropShape.path = dropPath


drop.addChild(dropShape)
drop.position = CGPointMake(CGFloat(xValue), self.frame.height-5)

self.addChild(drop)
drop.physicsBody.categoryBitMask = dropCategory
drop.physicsBody.contactTestBitMask = mainCategory
}

func didBeginContact(contact: SKPhysicsContact) -> Void{
NSLog("Hello")

}
}

最佳答案

大家好消息,

SKPhysicsBody 文档:

动态属性控制基于体积的物体是否受到重力、摩擦、与其他物体的碰撞以及您直接施加到物体上的力或冲量的影响。

所以这里有一个想法,可以在不拖动时完成静止对象,但接收碰撞事件。在 SKSpriteNode 上的 touchesBegan 上,设置其 physicsBody.dynamic = true,在 touchUp 上设置 physicsBody.dynamic = false

或者,您可以从系统中移除引力并让所有节点都具有 physicsBody.dynamic = true

关于ios - didBeginContact 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007037/

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