Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Spritkit:使用手指绘制线条时检测碰撞_Ios_Swift_Sprite Kit_Skphysicsbody_Skphysicscontact - Fatal编程技术网

Ios Spritkit:使用手指绘制线条时检测碰撞

Ios Spritkit:使用手指绘制线条时检测碰撞,ios,swift,sprite-kit,skphysicsbody,skphysicscontact,Ios,Swift,Sprite Kit,Skphysicsbody,Skphysicscontact,我正在尝试检测现有线路和用户当前使用手指绘制的线路之间的碰撞触点 这是我的密码: let padding: CGFloat = 100 override func didMove(to view: SKView) { physicsWorld.contactDelegate = self let startPoint1 = CGPoint(x: self.frame.minX + padding , y: self.frame.minY + padding) let

我正在尝试检测
现有线路
和用户
当前使用手指绘制的线路之间的碰撞
触点

这是我的密码:

let padding: CGFloat = 100
override func didMove(to view: SKView) {

    physicsWorld.contactDelegate = self

    let startPoint1 = CGPoint(x: self.frame.minX + padding , y: self.frame.minY + padding)
    let leftHorizontalPoint = CGPoint(x: self.frame.minX + padding, y: self.frame.maxY - padding)

    let line1 = SKShapeNode()
    let line_path:CGMutablePath = CGMutablePath()
    line_path.move(to: startPoint1)
    line_path.addLine(to: leftHorizontalPoint)
    line1.path = line_path
    line1.lineWidth = 3
    line1.strokeColor = UIColor.white
    addChild(line1)

    line1.physicsBody = SKPhysicsBody(edgeLoopFrom: line1.frame)
    line1.physicsBody?.isDynamic = true
    line1.physicsBody?.categoryBitMask = PhysicsCategory.solidLine
    line1.physicsBody?.collisionBitMask = PhysicsCategory.currentLine
    line1.physicsBody?.contactTestBitMask = PhysicsCategory.currentLine
}
然后在touchBegin、touchMove和touchEnd上,我有以下代码:

var currentLineNode: SKShapeNode!
var startPoint: CGPoint = CGPoint.zero
func touchDown(atPoint pos : CGPoint) {
    startPoint = pos
}

func touchMoved(toPoint pos : CGPoint) {
    if currentLineNode != nil {
        currentLineNode.removeFromParent()
    }
    currentLineNode = SKShapeNode()
    currentLineNode.zPosition = 1
    let line_path:CGMutablePath = CGMutablePath()
    line_path.move(to: startPoint)
    line_path.addLine(to: pos)
    currentLineNode.path = line_path
    currentLineNode.lineWidth = 3
    currentLineNode.strokeColor = UIColor.red
    addChild(currentLineNode)

    currentLineNode.physicsBody = SKPhysicsBody(edgeLoopFrom: currentLineNode.frame)
    currentLineNode.physicsBody?.isDynamic = true
    currentLineNode.physicsBody?.categoryBitMask = PhysicsCategory.currentLine
    currentLineNode.physicsBody?.collisionBitMask = PhysicsCategory.solidLine
    currentLineNode.physicsBody?.contactTestBitMask = PhysicsCategory.solidLine
}

func touchUp(atPoint pos : CGPoint) {
    if currentLineNode != nil {
        currentLineNode.removeFromParent()
    }
    currentLineNode = SKShapeNode()
    let line_path:CGMutablePath = CGMutablePath()
    line_path.move(to: startPoint)
    line_path.addLine(to: pos)
    currentLineNode.path = line_path
    currentLineNode.lineWidth = 3
    currentLineNode.strokeColor = UIColor.red
    addChild(currentLineNode)

    currentLineNode.physicsBody = SKPhysicsBody(edgeLoopFrom: currentLineNode.frame)
    currentLineNode.physicsBody?.isDynamic = true
    currentLineNode.physicsBody?.categoryBitMask = PhysicsCategory.currentLine
    currentLineNode.physicsBody?.collisionBitMask = PhysicsCategory.solidLine
    currentLineNode.physicsBody?.contactTestBitMask = PhysicsCategory.solidLine
}

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

    for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches { self.touchMoved(toPoint: t.location(in: self)) }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}
这是我的输出,即使从白色线条经过,也不会检测到碰撞


有什么不对劲吗?任何关于这方面的建议都会很有帮助。

您使用的是两个基于边缘的实体。基于边的实体将始终为
isDynamic=false
,无论是否设置它。您至少需要一个基于卷的主体才能执行接触

此外,您还不断地删除和添加节点,这是一个糟糕的想法


我建议您只在touchsbegind上添加节点,然后只更新touchsmoved上的路径

您正在创建带有边循环的物理体。苹果将边循环定义为

边没有体积或质量,并且始终将其视为isDynamic特性等于false。边只能与基于体积的物理体碰撞

把你的物理身体改成这样行得通

currentLineNode.physicsBody = SKPhysicsBody(rectangleOf: currentLineNode.frame.size, center: CGPoint(x: startPoint.x + currentLineNode.frame.size.width / 2, y: startPoint.y + currentLineNode.frame.size.height / 2)) 

还应该注意的是,在TouchSended中更改物理体是多余的,不会增加任何内容。我把它从touchesEnded上取下来了,它工作得很好

该死,比我快
currentLineNode.physicsBody = SKPhysicsBody(rectangleOf: currentLineNode.frame.size, center: CGPoint(x: startPoint.x + currentLineNode.frame.size.width / 2, y: startPoint.y + currentLineNode.frame.size.height / 2))