框架的接触方法

时间:2016-02-07 00:21:44

标签: ios swift sprite-kit

我正在尝试实现一种联系方法,以了解我的播放器块何时触及黄色框架。下面是我到目前为止的代码。当我将播放器块拖到黄色框架时没有任何反应。

enter image description here

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
    }
}

1 个答案:

答案 0 :(得分:0)

目前您正在尝试检查两个静态实体之间的联系,但SpriteKit不会生成任何联系事件,除非至少有一个实体是动态的。

在你的情况下,其原因与基于边缘的物理体在默认情况下是静态的相关,并且你的播放器也是静态的,意味着没有接触。

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

player.physicsBody!.dynamic = true