Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 检测两个对象之间是否发生碰撞_Ios_Swift_Sprite Kit_Skspritenode - Fatal编程技术网

Ios 检测两个对象之间是否发生碰撞

Ios 检测两个对象之间是否发生碰撞,ios,swift,sprite-kit,skspritenode,Ios,Swift,Sprite Kit,Skspritenode,我开始研究iOS中的精灵套件游戏,我想知道当球进入篮筐时,如何检测是否发生碰撞 使事情更容易理解。在我的篮球圈里,我有两个角(它们是红色的,很容易看到,它们很突出),它们有碰撞,如果球击中它们,它就会被踩踏 我的问题:我想在球打到任何一个角或者球没有打到任何一个角就进入篮筐时引起注意。(就像一种区分正常投掷和完美投掷的得分方式) 更新: 如何在拐角处设置标志?你能指出我需要使用的功能吗 我有一个节点通知我,如果球进入篮圈,它是篮筐中间的钢筋,当它碰撞时,它增加了我的分数。 func didBeg

我开始研究iOS中的精灵套件游戏,我想知道当球进入篮筐时,如何检测是否发生碰撞

使事情更容易理解。在我的篮球圈里,我有两个角(它们是红色的,很容易看到,它们很突出),它们有碰撞,如果球击中它们,它就会被踩踏

我的问题:我想在球打到任何一个角或者球没有打到任何一个角就进入篮筐时引起注意。(就像一种区分正常投掷和完美投掷的得分方式)

更新:


如何在拐角处设置标志?你能指出我需要使用的功能吗

<>我有一个节点通知我,如果球进入篮圈,它是篮筐中间的钢筋,当它碰撞时,它增加了我的分数。

func didBegin(_ contact: SKPhysicsContact)
    {
        // check for the ball contacting the scoreZone (scoreZone is the steel bar)
        guard let ballBody = ball.physicsBody, let scoreBody = score_zone.physicsBody else {
            return
        }
        // it doesn't matter who touches who, so just use array "contains" to handle both cases
        let bodies = [contact.bodyA, contact.bodyB]
        if bodies.contains(ballBody) && bodies.contains(scoreBody) && should_detect_score {
            // add score
            add_score()
        }
    }
  • 当球碰到任何一个红色的取芯球时,设置一个标志
  • 将节点放置在网络的中间,设置为通知它何时 触球
  • 如果“网络节点”触发了一个联系人,则“红色角落”标志已关闭 没有被设定,那么你就有一个完美的投掷
  • 重置“红色角落”标志

  • 下面是一些用于应用和检测碰撞的代码,这是我使用的,似乎非常有效

    -SWIFT 3-

    enum bodyType: UIInt32 {
        case redDot1 = 1
        case redDot2 = 2
        case ball = 3
    }
    //do this to properly set up the physicsBodies of your detection otherwise the detection will not work at all
    yourBall.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(x: ball.size.width, y: ball.size.height)
    yourBall.physicsBody?.categoryBitMask = bodyType.ball.rawValue
    yourBall.physicsBody?.collisionBitMask = bodyType.redDot1.rawValue | bodyType.redDot2.rawValue 
    
    //repeat these steps for redDot1 and redDot2 and then put this in someplace (doesn't matter where)
    
    
    func didBegin(_ contact: SKPhysicsContact) {
        if contact.bodyA.categoryBitMask = bodyType.ball.rawValue && contact.bodyB.categoryBitMask = bodyType.redDot1.rawValue || contact.bodyB.categoryBitMask = bodyType.ball.rawValue && contact.bodyA.categoryBitMask = bodyType.redDot1.rawValue {
        //in here is where you will put the notification
        }
    if contact.bodyA.categoryBitMask = bodyType.ball.rawValue && contact.bodyB.categoryBitMask = bodyType.redDot2.rawValue || contact.bodyB.categoryBitMask = bodyType.ball.rawValue && contact.bodyA.categoryBitMask = bodyType.redDot2.rawValue {
        //in here is where you will put the notification
        }
    }
    
    一些解释

    因此,如果你看我声明你的球有一个物理体的地方,它说categoryBitMask=BlahBlah,这意味着你告诉代码你的球有一个唯一的整数,可以区分碰撞检测

    然后它说的是collisionBitMask,意思是你告诉SpriteKit你想让球接触到什么

    在底部,这个函数是一个物理处理程序,它可以使你准确地知道什么是联系人,什么时候发生,并且允许你在调用联系人时输入发生的事情(比如给一个提示)

    我建议您阅读更多关于它的内容,并研究有关SKPhysicsBody的Apple文档

    如果你有任何问题,请让我知道,因为我很乐意帮助你更多的东西

    就像史蒂夫·艾夫斯刚刚发布的那样,你想做四件事

  • 制作某种形式的整数,如果接触到红点,就会将其相加

  • 如果整数>0,则不是完美的抛出

  • 如果整数=0,这是一个完美的投掷

  • 添加分数后,重置整数并再次执行所有操作


  • 你能发布你当前的碰撞检测代码吗?我如何在拐角处设置标志?你能指出我需要使用的功能吗?