我正在尝试实现一种接触方法,以了解我的玩家方块何时触及了黄框。下面是我到目前为止的代码。当我将播放器块拖到黄色框时,什么也没有发生。

ios - 框架的接触方法-LMLPHP

import SpriteKit
import iAd

struct BitMask {
    static let player:   UInt32 = 0x1 << 0
    static let obstacle: UInt32 = 0x1 << 1
    static let frame:    UInt32 = 0x1 << 2
}

class GameScene: SKScene, SKPhysicsContactDelegate {

let player = SKShapeNode(rectOfSize: CGSize(width: 30, height: 30))

override func didMoveToView(view: SKView) {
    super.didMoveToView(view)

    // setup scene's physics body (setup the walls)
    physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)

    // get current screen size
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    print("screenWidth:  \(screenSize.width) screenHeight: \(screenSize.height)")

    // setup play scene
    let playScreen = SKSpriteNode(color: .clearColor(), size: CGSize(width: 370, height: 370))

    // y position adjustment
    let yFrame = 65
    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)

    let playerScreenFrame = SKShapeNode(rectOfSize: CGSize(width: playScreen.size.width, height: playScreen.size.height))
    playerScreenFrame.position = CGPoint(x: frame.midX, y: frame.midY  - CGFloat(yFrame))
    playerScreenFrame.fillColor = .clearColor()
    playerScreenFrame.strokeColor = UIColor(rgba: "#E9BD00")
    playerScreenFrame.lineWidth = 3;
    addChild(playerScreenFrame)

    let bottomRect = CGRectMake(frame.midX, frame.midY - CGFloat(yFrame), playScreen.size.width, playScreen.size.height)
    let bottom = SKNode()
    bottom.physicsBody = SKPhysicsBody(edgeLoopFromRect: bottomRect)
    addChild(bottom)
    bottom.physicsBody!.categoryBitMask = BitMask.frame
    bottom.physicsBody!.contactTestBitMask = BitMask.player
    bottom.physicsBody!.collisionBitMask = BitMask.player

    // set up player block
    player.name = "player"
    player.fillColor        = UIColor(rgba: "#E9BD00")
    player.strokeColor      = .blackColor()
    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 = false
    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

    // set gravity to 0
    physicsWorld.gravity = CGVectorMake(0,0)

    // set the scene as the delegate to be notified when two bodies collide
    physicsWorld.contactDelegate = self

}


我的联系方式如下:

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) {
        // do your thing
        print("Player contacted with obstacle")
        self.view?.paused = true
    }

    if (firstBody.categoryBitMask == BitMask.player) && (secondBody.categoryBitMask == BitMask.frame) {
        // do your thing
        print("Player contacted with frame")
        self.view?.paused = true
    }
}

最佳答案

当前,您正在尝试检查两个静态物体之间的接触,但是SpriteKit不会生成任何接触事件,除非至少一个物体是动态的。

在您的情况下,其原因与以下事实有关:默认情况下,基于边缘的物理物体为静态,并且播放器也为静态,这意味着没有接触。

要解决此问题,您应该将玩家物理身体上的dynamic属性设置为true,如下所示:

player.physicsBody!.dynamic = true

10-08 08:47