ios - 框架的接触方法

标签 ios swift sprite-kit

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

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

最佳答案

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

就您而言,其原因与基于边缘的物理体默认情况下是静态的这一事实有关,并且您的玩家也是静态的,意味着没有接触。

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

player.physicsBody!.dynamic = true

关于ios - 框架的接触方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248245/

相关文章:

ios - 模仿马车线最好的方法是什么?

ios - 更新商店中的应用程序时的 SQLite 数据库

objective-c - UITableView 圆形自定义单元格(带/图像)

ios - 如何滑出侧边菜单栏?在 swift

swift - 如何转义 Swift 文档注释标注?

swift - 当 bool 值为 true 时如何删除该节点?

ios - "pull up"拖动动画

ios - 何时在 Objective-C 中使用 NS_INLINE 函数

ios - 如何根据 Sprite Kit 中的设备更改屏幕大小?

ios - 类似愤怒的小鸟游戏的Spritekit ios游戏弹弓机制