如何在没有物理学的情况下检测碰撞

How to detect collision without physics

我想在不使用 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 是一种错误的方法,但我可以代替它使用?

框架属性

The frame property is a rectangle in the parent’s coordinate system that contains the node’s content, ignoring the node’s children.

也来自文档:

The frame is non-empty if the node’s class draws content.

而且因为SKNode class不执行任何自己的绘图,你的平台可能是SKNode的子class,播放器是SKNode的框架属性总是 (0,0,0,0).

您可以通过以下几种方式获取框架的实际大小:

关于文档中的 calcualteAccumulatedFrame():

A node’s accumulated frame, retrieved by calling the calculateAccumulatedFrame method, is the largest rectangle that includes the frame of the node and the frames of all its descendants.