接触测试位掩码不正常工作

时间:2016-08-08 18:02:57

标签: testing sprite-kit contact bitmask

contactTestBitMask一般情况下有效,但如果图形过于复杂contactTestBitMask吓坏了,你只有一个小的接触区域或者会出现奇怪的错误。我能在那里做什么?

这是关于我所在地区的图片:

enter image description here

class GameScene: SKScene, SKPhysicsContactDelegate {
let WallCategory   : UInt32 = 0x1 << 1
let ShipCategory : UInt32 = 0x1 << 2
var counter = 0
var userCanBuild1Building = false
var KameraSichtVar:SKCameraNode = SKCameraNode()


var touched:Bool = false
var startTouch = CGPointMake(0, 0)
//------------------------------


var draufgeklickt:CGPoint = CGPoint()
var draufgeklicktUpdate:CGPoint = CGPoint()
var userWantScroll = false
var userWantBuild = false
let wohnhausBlue = SKSpriteNode(imageNamed:"wohnhausBlue")
let buildMenu = SKSpriteNode(imageNamed:"Buildmenu")
var hitDetected = false



let actualBuilding = SKSpriteNode()
var userWantUpgrade : Bool = false
//var theBuildingBefore = SKSpriteNode()
//var houseLvlSave : Int = 0
var uphausSave : String = ""
func didBeginContact(contact: SKPhysicsContact) {

    var BuildingObject: SKPhysicsBody
    var ClickObject: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
    {
        BuildingObject = contact.bodyA
        ClickObject = contact.bodyB
    }
    else
    {
        BuildingObject = contact.bodyB
        ClickObject = contact.bodyA
    }


    // upgrade Building
    let name = BuildingObject.node!.name
    let HouseLvL: Int = Int(name!)!
    if HouseLvL > 0 && userCanBuild1Building == true{

        userCanBuild1Building = false
        let touchedBuilding = SKSpriteNode(imageNamed: BuildingObject.node!.name!)
        touchedBuilding.name = BuildingObject.node!.name

        let name = BuildingObject.node!.name
        var HouseLvL: Int = Int(name!)!
        HouseLvL = HouseLvL + 1

        let uphaus = String(HouseLvL)

        let setSpriteTexture = SKAction.setTexture(SKTexture(imageNamed: uphaus))
        let upSize = SKSpriteNode(imageNamed: uphaus)
        let resizeHousewidth = SKAction.resizeToWidth(upSize.size.width, duration: 0)
        let resizeHouseheight = SKAction.resizeToHeight(upSize.size.height, duration: 0)



        touchedBuilding.runAction(resizeHousewidth)
        touchedBuilding.runAction(resizeHouseheight)
        touchedBuilding.runAction(setSpriteTexture)
        touchedBuilding.name = "\(uphaus)"

        touchedBuilding.position = BuildingObject.node!.position
        touchedBuilding.zPosition = BuildingObject.node!.zPosition
        touchedBuilding.userInteractionEnabled = false

        let upgradedbuilding = SKSpriteNode(imageNamed: uphaus)
        touchedBuilding.size = CGSizeMake(upgradedbuilding.size.width, upgradedbuilding.size.height)
        touchedBuilding.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed:"\(uphaus)"), size: upgradedbuilding.size)


        touchedBuilding.physicsBody?.categoryBitMask = WallCategory
        touchedBuilding.physicsBody?.affectedByGravity = false
        touchedBuilding.physicsBody?.dynamic = true
        touchedBuilding.physicsBody?.collisionBitMask = 0

        addChild(touchedBuilding)
        BuildingObject.node?.removeFromParent()
        ClickObject.node?.removeFromParent()






    }


    if HouseLvL == 0
    {
    ClickObject.node?.removeFromParent()
    }




        }`override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    if let KameraSicht:SKCameraNode = self.childNodeWithName("Kamera") as? SKCameraNode{
        KameraSichtVar = KameraSicht
        self.camera = KameraSichtVar


    }


    physicsWorld.contactDelegate = self
    self.physicsWorld.gravity = CGVectorMake(0, 0)

    func addBG(){

        let background = SKSpriteNode(imageNamed: "0")
        background.name = "0"
        background.anchorPoint = CGPointMake(0.5, 0.5)
        background.zPosition = 2
        background.position = CGPointMake(CGRectGetMinX(self.frame)+self.frame.width/4, CGRectGetMaxY(self.frame)-self.frame.height/4)
        background.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "0"), size: background.size)
        //background.physicsBody = SKPhysicsBody(rectangleOfSize: background.size)
        background.physicsBody?.categoryBitMask = WallCategory
        background.physicsBody?.affectedByGravity = false
        background.physicsBody?.dynamic = true
        background.physicsBody?.collisionBitMask = 0


        self.addChild(background)

    }

    addBG()


    let wall = SKSpriteNode(imageNamed:"1" )
    wall.name = "1"
    wall.zPosition = 10
    //wall.xScale = 3
    //wall.yScale = 3
    wall.size = CGSizeMake(100, 100)
    wall.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
    wall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: wall.name!), size: wall.size)
    //wall.physicsBody = SKPhysicsBody(rectangleOfSize: wall.size)
    wall.physicsBody?.affectedByGravity = false
    wall.physicsBody?.dynamic = false
    wall.physicsBody!.categoryBitMask = WallCategory
    wall.physicsBody?.collisionBitMask = 0
    self.addChild(wall)




}

`

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。问题是当你使用这个图形时contactTestBitMask的奇怪行为。(它只在图形的一个小区域内检测到一个联系人)。原因是图形具有分离的形状(如半环和其他一些小东西)。

我连接所有形状,以便它只有一个形状,现在一切都很好。还尝试了一些其他图形和contactTestBitMask的行为:如果你的图形中有单独的形状,contactTestBitMask只取一个形状并使其成为接触区域...但你图形中的其他形状将不会检测到然后联系...