块快速移动时出现意外的碰撞行为 - 精灵通过

时间:2016-02-20 22:08:27

标签: ios swift sprite-kit

我有一个方框,允许用户移动块但不触摸框架。如果用户触摸方框,则游戏结束。目前我遇到一个问题,即如果用户快速滑动块,它会通过框架而不会发生任何碰撞/接触检测。如果玩家缓慢移动块并触摸帧,则它会检测到碰撞。我的代码如下,我不能让它停止通过框架。

// setup play scene
let playScreen = SKSpriteNode(color: .clearColor(), size: CGSize(width: 370, height: 370))
playScreen.position = CGPoint(x: frame.midX, y: frame.midY - CGFloat(yFrame))
// create the rectangle which will represent physics body
let rect = CGRect(origin: CGPoint(x: -playScreen.size.width/2, y: -playScreen.size.height/2), size: playScreen.size)
// apply physics conditions to the play scene
playScreen.physicsBody = SKPhysicsBody(edgeLoopFromRect: rect)
playScreen.physicsBody!.friction = 0
// add play scene to the frame
addChild(playScreen)
playScreen.physicsBody!.categoryBitMask = BitMask.frame
playScreen.physicsBody!.contactTestBitMask = BitMask.player
playScreen.physicsBody!.collisionBitMask = BitMask.player
playScreen.physicsBody!.usesPreciseCollisionDetection = true;

// set up player block
player.position = CGPoint(x:frame.size.width/2, y: frame.size.height/2  - CGFloat(yFrame))
player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 30, height: 30))
player.physicsBody!.dynamic = true
player.physicsBody!.friction = 0
player.physicsBody!.affectedByGravity = false
player.physicsBody!.allowsRotation = false
addChild(player)
player.physicsBody!.categoryBitMask     = BitMask.player
player.physicsBody!.contactTestBitMask  = BitMask.obstacle | BitMask.frame
player.physicsBody!.collisionBitMask    = BitMask.obstacle | BitMask.frame

我有以下联系人检测:

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 == BitMask.player) && ( (secondBody.categoryBitMask == BitMask.obstacle) || (secondBody.categoryBitMask == BitMask.frame) )  {

        self.view?.paused = true

        // check if this stops it
        player.physicsBody!.velocity.dx = 0
        player.physicsBody!.velocity.dy = 0

        playerCollided = true

    }
}

所以这是我触摸开始功能的代码。如果用户触摸了玩家块:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first! as UITouch
    let touchLocation = touch.locationInNode(self)
    let touchedNode = self.nodeAtPoint(touchLocation)
    let name = touchedNode.name

    if name == "player" {

        // first touch
        if playerFirstTouch {
            print("New Game")
            print("Finger is on player block")
            isFingerOnPlayer = true

            NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "printDuration:", userInfo: NSDate(), repeats: true)

            playerFirstTouch = false
            print("\(player.physicsBody!.velocity.dy)")

        } else {
            print("Finger is on player block after game started")
            isFingerOnPlayer = true
            print("\(player.physicsBody!.velocity.dy)")
        }
    }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first! as UITouch
    let touchLocation = touch.locationInNode(self)

    // check if user touched the player block
    if isFingerOnPlayer {
        // get touch location
        if let touch = touches.first {
            let prevTouchLoc = touch.previousLocationInNode(self)
            // calculate new position along x and y
            var newXPos = player.position.x + (touchLocation.x - prevTouchLoc.x)
            var newYPos = player.position.y + (touchLocation.y - prevTouchLoc.y)
            //
            newXPos = max(newXPos, self.player.frame.size.width/2)
            newXPos = min(newXPos, self.size.width - self.player.frame.size.width/2)
            //
            newYPos = max(newYPos, self.player.frame.size.height/2)
            newYPos = min(newYPos, self.size.height - self.player.frame.size.height/2)
            // update player block position
            player.position = CGPointMake(newXPos, newYPos)
        }
    }

}

1 个答案:

答案 0 :(得分:1)

让玩家使用精确的碰撞检测,而不是场景。玩家是正在移动的节点,需要逐步完成它的物理计算。如下所示:

player.physicsBody!.usesPreciseCollisionDetection = true;

你可以从playScene中删除精确的碰撞检测,因为启用它会使物理引擎陷入困境。

修改

我对此进行了扩展,以涵盖如何使用SKAction移动节点而不是设置位置。我很确定这会奏效,但请告诉我。查看我将要使用的the documentation for the moveBy method

您要做的第一件事是删除此行:

player.position = CGPointMake(newXPos, newYPos)

我们将用这一行替换它:

player.runAction(SKAction.moveTo(CGPointMake(newXPos, newYPos), 0.01))

这会让玩家快速移动到所需的位置 (我认为)。