首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拼接检测Sprite-Kit位掩码

拼接检测Sprite-Kit位掩码
EN

Stack Overflow用户
提问于 2015-09-19 00:32:45
回答 1查看 56关注 0票数 0

我花了几个星期的时间尝试使用BitMasking完全掌握Sprite-Kit中的碰撞检测。我在youtube上搜索了一下,什么也没找到。我将发布我现在拥有的代码。当我的两个SKSpriteNodes相互联系时,我现在拥有的代码不能正常工作。谁能告诉我如何修复我的代码,并告诉我步骤和他们的意思。

代码语言:javascript
复制
import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate{

let ballCategory:UInt32 = 0x1 << 0
let boxCategory:UInt32 = 0x1 << 1
let ball = SKSpriteNode(imageNamed: "redball")
let redBall = SKSpriteNode(imageNamed: "redball")


override func didMoveToView(view: SKView) {
    self.physicsWorld.contactDelegate = self
    let box = SKSpriteNode(imageNamed: "box")
    box.position = CGPoint(x: 400, y: 400)
    box.physicsBody?.categoryBitMask = boxCategory
    box.physicsBody?.contactTestBitMask = boxCategory
    box.physicsBody?.collisionBitMask = boxCategory | ballCategory
    addChild(box)

    redBall.position = CGPoint(x: 200, y: 200)
    redBall.physicsBody?.categoryBitMask = ballCategory
    redBall.physicsBody?.contactTestBitMask = ballCategory
    redBall.physicsBody?.collisionBitMask = ballCategory | boxCategory
    addChild(redBall)

    //Backround Properties
    backgroundColor = UIColor(red: 70.0/255.0, green: 67.0/255.0, blue: 67.0/255, alpha: 1)

}




override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let touchLocation = touch.locationInNode(self)
    redBall.position = touchLocation

}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let touchLocation = touch.locationInNode(self)
    redBall.position = touchLocation
}
func didBeginContact(contact: SKPhysicsContact) {
    let contactBodyA = SKSpriteNode()
    let contactBodyB = SKSpriteNode()

    if(contact.bodyA.categoryBitMask == ballCategory) && (contact.bodyB.categoryBitMask == boxCategory){

        println("contact was made")
    }
}
}
EN

回答 1

Stack Overflow用户

发布于 2015-09-19 01:47:35

我已经修改了你的一些代码,现在它工作得很好。下面是你的代码:

代码语言:javascript
复制
import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate{

    let ballCategory:UInt32 = 0x1 << 0
    let boxCategory:UInt32 = 0x1 << 1

    let ball = SKSpriteNode(imageNamed: "redball")
    let redBall = SKSpriteNode(imageNamed: "redball")


    override func didMoveToView(view: SKView) {
        self.physicsWorld.contactDelegate = self
        //make gravity 0
        self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)

        let box = SKSpriteNode(imageNamed: "redball")
        box.position = CGPoint(x: 400, y: 400)
        //add physicsbody for collisin detection
        box.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "redball"), size: box.size)
        //assign a correct cetogary
        box.physicsBody?.categoryBitMask = boxCategory
        box.physicsBody?.collisionBitMask =  ballCategory
        box.physicsBody?.contactTestBitMask =  ballCategory
        addChild(box)

        redBall.position = CGPoint(x: 200, y: 200)
        //add physics body
        redBall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "redball"), size: redBall.size)
        //assign correct cetogary
        redBall.physicsBody?.categoryBitMask = ballCategory
        redBall.physicsBody?.contactTestBitMask = boxCategory
        redBall.physicsBody?.collisionBitMask = boxCategory
        addChild(redBall)

        //Backround Properties
        backgroundColor = UIColor(red: 70.0/255.0, green: 67.0/255.0, blue: 67.0/255, alpha: 1)

    }




    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first! 
        let touchLocation = touch.locationInNode(self)
        redBall.position = touchLocation
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first!
        let touchLocation = touch.locationInNode(self)
        redBall.position = touchLocation
    }

    func didBeginContact(contact: SKPhysicsContact) {
        // 1. Create local variables for two physics bodies
        var firstBody: SKPhysicsBody
        var secondBody: SKPhysicsBody

        // 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody
        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            firstBody = contact.bodyA
            secondBody = contact.bodyB
        } else {
            firstBody = contact.bodyB
            secondBody = contact.bodyA
        }

        // 3. react to the contact between ball and bottom
        if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == boxCategory {
            //TODO: Replace the log statement with display of Game Over Scene
            //add your code here.
            print("Called")
        }
    }
}

使用xCode 7进行了测试。

HERE是关于spriteKit的很好的教程,你可以从中学到更多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32656926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档