【问题标题】:contacts not recognized when body is changed from circle to rectangle身体从圆形更改为矩形时无法识别联系人
【发布时间】:2015-10-01 15:51:08
【问题描述】:

here 的帮助下,我已经让一个圆形体穿过给定的路径。我在一些路径点有一些尸体,并在didBeginContact 中记录了联系方式。当身体与特定身体接触时,圆形身体将变为矩形。假设这个矩形体与原始圆形体穿过相同的路径,但由于没有记录接触,它没有到达路径点。我也尝试将radiusPoint 更改为矩形的宽度或高度,但这不起作用。矩形体也比圆形体大。如何让矩形穿过识别出接触的点?请看下面的代码。

路径遍历相关代码:

    let repeats: Bool = true //Whether to repeat the path.
    var pathIndex = 0 //The index of the current point to travel.
    var pointRadius: CGFloat = SKTexture(imageNamed: "circle").size().width //How close the node must be to reach the destination point.
    let travelSpeed: CGFloat = 250 //Speed the node will travel at.
    let rate: CGFloat = 0.9 //Motion smoothing. 0.5

    circlePath = [
    CGPoint(x:screenSize.width , y: screenSize.height/3),
    CGPoint(x: screenSize.width/2, y: platform.sprite.frame.height),
    CGPoint(x: 0.0, y: screenSize.height/3),
    CGPoint(x: CGFloat(pos1) + screenSize.width/20, y: upperSpearPosHeight)]

    final func didReachPoint() {
    //reached point!
    pathIndex++

    if pathIndex >= ballPath.count && repeats {
    pathIndex = 0
    }
    }

    func updatePath() {

    if pathIndex >= 0 && pathIndex < circlePath.count {
    let destination = circlePath[pathIndex]
    //currentPosition = destination
    let displacement = CGVector(dx: destination.x-circle!.sprite.position.x, dy: destination.y-circle!.sprite.position.y)
    let radius = sqrt(displacement.dx*displacement.dx+displacement.dy*displacement.dy)
    let normal = CGVector(dx: displacement.dx/radius, dy: displacement.dy/radius)
    let impulse = CGVector(dx: normal.dx*travelSpeed, dy: normal.dy*travelSpeed)
    let relativeVelocity = CGVector(dx:impulse.dx-circle!.sprite.physicsBody!.velocity.dx, dy:impulse.dy-circle!.sprite.physicsBody!.velocity.dy);
    circle!.sprite.physicsBody!.velocity=CGVectorMake(circle!.sprite.physicsBody!.velocity.dx+relativeVelocity.dx*rate, circle!.sprite.physicsBody!.velocity.dy+relativeVelocity.dy*rate);

    if radius < pointRadius {
        didReachPoint()
        }
        }
    }

联系方式:

        func didBeginContact(contact: SKPhysicsContact) {
    var firstBody : SKPhysicsBody
    var secondBody : SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask  {
    firstBody = contact.bodyA
    secondBody = contact.bodyB
    }
    else    {
    firstBody = contact.bodyB
    secondBody = contact.bodyA
    }

            if firstBody.categoryBitMask == circleCategory && secondBody.categoryBitMask == bonusCategory  {
    let img = SKTexture(imageNamed: "rectangular")
   (firstBody.node! as? SKSpriteNode)?.size = img.size()       
   firstBody.node!.physicsBody = SKPhysicsBody(texture: img, size: img.size())
   firstBody.node!.physicsBody?.allowsRotation = false 
   changeCircleAction = SKAction.setTexture(img)   
   firstBody.node!.runAction(changeCircleAction)
 }

    if firstBody.categoryBitMask == circleCategory && secondBody.categoryBitMask == platformCategory  {

    print("touched platform")
    }

    if firstBody.categoryBitMask == circleCategory && secondBody.categoryBitMask == smallStarCategory  {

    removeStar(secondBody.node!)
    }

【问题讨论】:

    标签: swift sprite-kit skphysicsbody


    【解决方案1】:

    似乎当您将firstBody 更改为矩形时(在didBeginContact 方法中),您没有设置位掩码。根据您的描述,您似乎想将其设置为circleCategory

    firstBody.node!.physicsBody?.categoryBitMask = circleCategory
    

    我会把它放在firstBody.node!.physicsBody?.allowsRotation = false 的正下方。

    【讨论】:

      猜你喜欢
      • 2013-12-13
      • 2018-12-03
      • 1970-01-01
      • 2015-08-04
      • 2017-03-10
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多