如何检测没有物理的碰撞

时间:2015-07-24 07:25:08

标签: swift sprite-kit game-physics cgrectintersectsrect

我想检测两个身体何时接触而不使用didBeginContact()函数

我尝试使用CGRectIntersectsRect()而没有任何影响。

class GameScene: SKScene, SKPhysicsContactDelegate {

    // Player
    var _player = SKNode()
    var platform = SKNode()

    let heroCategory: UInt32 = 1 << 0
    let platformCategory: UInt32 = 1 << 1

    override func didMoveToView(view: SKView) {

        /* Setup your scene here */

        // Setup
        self.anchorPoint = CGPointMake(0.5, 0.5)
        self.physicsWorld.gravity = CGVectorMake(0.0, -2.0);
        self.physicsWorld.contactDelegate = self

        // Start populating
        _player = creatPlayer()
        self.addChild(_player)

        platform = creatPlatform(1, atPosition: CGPoint(x: 0, y: -200))
        self.addChild(platform)


        let platformTwo = creatPlatform(2, atPosition: CGPoint(x: 0, y: 200))
        self.addChild(platformTwo)

    }

    func creatPlatform(type: Int, atPosition: CGPoint) -> PlatformNode {

        let node = PlatformNode()
        node.platformType = type
        node.position = atPosition
        let sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 200, height: 50))
        node.addChild(sprite)

        node.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
        node.physicsBody?.dynamic = false;
        node.physicsBody?.categoryBitMask = platformCategory
        node.physicsBody?.contactTestBitMask = heroCategory
        node.physicsBody?.collisionBitMask = 0;

        return node

    }

    func creatPlayer() -> SKNode {

        let playerNode = SKNode()
        let sprite = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 50, height: 50))
        playerNode.addChild(sprite)

        playerNode.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
        playerNode.physicsBody?.dynamic = false
        playerNode.physicsBody?.allowsRotation = false
        playerNode.physicsBody?.restitution = 1.0
        playerNode.physicsBody?.friction = 0.0
        playerNode.physicsBody?.angularDamping = 0.0
        playerNode.physicsBody?.linearDamping = 0.0
        playerNode.physicsBody?.categoryBitMask = heroCategory
        playerNode.physicsBody?.contactTestBitMask = platformCategory
        playerNode.physicsBody?.collisionBitMask = 0;

        return playerNode

    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

        /* Called when a touch begins */
        _player.physicsBody?.dynamic = true

    }

    override func update(currentTime: CFTimeInterval) {

        /* Called before each frame is rendered */
        if(CGRectIntersectsRect(platform.frame, _player.frame)){
            println("Collision")
        }
    }

}

使用CGRectIntersectsRect可能是一种错误的方法,但我可以使用它吗?

1 个答案:

答案 0 :(得分:1)

框架属性

  

frame属性是父坐标系中的一个矩形   包含节点内容,忽略节点的子节点。

同样来自docs:

  

如果节点的类绘制内容,则帧为非空。

因为SKNode类不执行任何自己的绘图,并且你的平台可能是SKNode的子类,而播放器是SKNode,所以frame属性总是(0,0,0,0)。

您可以通过几种方式访问​​框架的实际尺寸:

关于来自docs的calcualteAccumulatedFrame():

  

节点的累积帧,通过调用来检索   calculateAccumulatedFrame方法,是最大的矩形   包括节点的框架和所有后代的框架。