SpriteKit + Swift - 联系

时间:2016-04-06 05:53:06

标签: ios swift sprite-kit skphysicsbody

所以我正在玩Swift和SpriteKit。我决定创造一个简单的游戏,让一个角色走到一堆硬币并获得所有宝藏。

我试图检测两个物体何时相互接触,但似乎没有发生任何事情。

我做了什么:

1)我设置了委托:

SKPhysicsContactDelegate

2)创建场景:

override func didMoveToView(view: SKView) {

    backgroundColor = SKColor.whiteColor()
    physicsWorld.contactDelegate = self

    let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    sceneBody.friction = 0

    background.zPosition = -20
    background.anchorPoint = CGPointZero
    background.position = CGPointZero

    backgroundRepeated.zPosition = -20
    backgroundRepeated.anchorPoint = CGPointZero
    backgroundRepeated.position = CGPointMake(background.size.width-1,0)


    addChest()
    addCharacter()

    addChild(background)
    addChild(backgroundRepeated)


}

3)创建了项目:

func addChest() {
    chestOfCoins.anchorPoint = CGPointZero
    chestOfCoins.size = CGSize(width: 200,height: 200)
    chestOfCoins.position = CGPoint(x: self.frame.width-1, y: 300)
    chestOfCoins.zPosition = 20
    chestOfCoins.physicsBody = SKPhysicsBody(rectangleOfSize: chestOfCoins.size)
    chestOfCoins.physicsBody?.affectedByGravity = false
    chestOfCoins.physicsBody?.dynamic = false
    chestOfCoins.physicsBody?.categoryBitMask = physicsCategory.chests
    chestOfCoins.physicsBody?.collisionBitMask = 0
    chestOfCoins.physicsBody?.contactTestBitMask = physicsCategory.character

    addChild(chestOfCoins)
}

func addCharacter() {
    character.anchorPoint = CGPointZero
    character.position = CGPoint(x: 300, y: 300)
    character.zPosition = 20
    character.size = CGSize(width: 200, height: 200)
    character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
    character.physicsBody?.affectedByGravity = false
    character.physicsBody?.dynamic = false
    character.physicsBody?.categoryBitMask = physicsCategory.character
    character.physicsBody?.contactTestBitMask = physicsCategory.chests
    character.physicsBody?.collisionBitMask = 0


    addChild(character)
}

4)创建了didBeginContact:

func didBeginContact(contact: SKPhysicsContact) {
    var contactBody1: SKPhysicsBody
    var contactBody2: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        contactBody1 = contact.bodyA
        contactBody2 = contact.bodyB

    } else {
        contactBody1 = contact.bodyB
        contactBody2 = contact.bodyA
    }

    if((contactBody1.categoryBitMask == 1) && (contactBody2.categoryBitMask == 2)) {
        print("touched")
    } else {
        print("not these two")
    }
}
  

仅供参考,如果((contactBody1.categoryBitMask == 1)&amp;&amp;(contactBody2.categoryBitMask == 2))IF不输出主if块或else。

5)我创建了物理类别:

struct physicsCategory {
  static let none: UInt32 = 0 //0
  static let character: UInt32 = 0b1 //1
  static let chests: UInt32 = 0b10 //2
  static let snake: UInt32 = 0b100 //4
}

Full GameScene.Swift:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

struct physicsCategory {
    static let none: UInt32 = 0 //0
    static let character: UInt32 = 0b1 //1
    static let chests: UInt32 = 0b10 //2
    static let snake: UInt32 = 0b100 //4
    static let all: UInt32 = UInt32.max
}


let background = SKSpriteNode(imageNamed: "Backgrounds")
let backgroundRepeated = SKSpriteNode(imageNamed: "Backgrounds")
let chestOfCoins = SKSpriteNode(imageNamed: "chest")
let character = SKSpriteNode(imageNamed: "character")

let gameActions = NSUserDefaults.standardUserDefaults()

func addChest() {
    chestOfCoins.anchorPoint = CGPointZero
    chestOfCoins.size = CGSize(width: 200,height: 200)
    chestOfCoins.position = CGPoint(x: self.frame.width-1, y: 300)
    chestOfCoins.zPosition = 20
    chestOfCoins.physicsBody = SKPhysicsBody(rectangleOfSize: chestOfCoins.size)
    chestOfCoins.physicsBody?.affectedByGravity = false
    chestOfCoins.physicsBody?.dynamic = false
    chestOfCoins.physicsBody?.categoryBitMask = physicsCategory.chests
    chestOfCoins.physicsBody?.collisionBitMask = 0
    chestOfCoins.physicsBody?.contactTestBitMask = physicsCategory.character

    addChild(chestOfCoins)
}

func addCharacter() {
    character.anchorPoint = CGPointZero
    character.position = CGPoint(x: 300, y: 300)
    character.zPosition = 20
    character.size = CGSize(width: 200, height: 200)
    character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
    character.physicsBody?.affectedByGravity = false
    character.physicsBody?.dynamic = false
    character.physicsBody?.categoryBitMask = physicsCategory.character
    character.physicsBody?.contactTestBitMask = physicsCategory.chests
    character.physicsBody?.collisionBitMask = 0


    addChild(character)
}

override func didMoveToView(view: SKView) {

    backgroundColor = SKColor.whiteColor()
    physicsWorld.contactDelegate = self

    let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    sceneBody.friction = 0

    background.zPosition = -20
    background.anchorPoint = CGPointZero
    background.position = CGPointZero

    backgroundRepeated.zPosition = -20
    backgroundRepeated.anchorPoint = CGPointZero
    backgroundRepeated.position = CGPointMake(background.size.width-1,0)


    addChest()
    addCharacter()

    addChild(background)
    addChild(backgroundRepeated)


}

func didBeginContact(contact: SKPhysicsContact) {
    var contactBody1: SKPhysicsBody
    var contactBody2: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        contactBody1 = contact.bodyA
        contactBody2 = contact.bodyB

    } else {
        contactBody1 = contact.bodyB
        contactBody2 = contact.bodyA
    }

    if((contactBody1.categoryBitMask == 1) && (contactBody2.categoryBitMask == 2)) {
        print("touched")
    } else {
        print("not these two")
    }
}

override func update(currentTime: NSTimeInterval) {

    let startWalking = gameActions.boolForKey("Walking")

    if (startWalking == true) {


        chestOfCoins.position = CGPoint(x: chestOfCoins.position.x - 5, y: chestOfCoins.position.y)

        background.position = CGPoint(x: background.position.x - 5, y: background.position.y)
        backgroundRepeated.position = CGPoint(x: backgroundRepeated.position.x - 5, y: backgroundRepeated.position.y)

        if (background.position.x < -background.size.width) {
            background.position = CGPointMake(backgroundRepeated.position.x + backgroundRepeated.size.width, background.position.y)
        }

        if (backgroundRepeated.position.x < -backgroundRepeated.size.width) {
            backgroundRepeated.position = CGPointMake(background.position.x + background.size.width, backgroundRepeated.position.y)
        }
    }
}

}

问题

任何人都可以告诉我为什么,当屏幕开始滚动并且这两个项目接触时没有任何反应并告诉我如何解决它?

即使我将两者的zPosition设置为相同的

,也似乎落后了

这是一张照片,不要判断图像:(

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:3)

将您的胸部或其他物品属性设置为dynamic = true

两个非动态物理机构不会注册联系人