Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
didBeginContact未在Swift 2+中工作;斯皮特基特_Swift_Sprite Kit_Swift2_Skspritenode - Fatal编程技术网

didBeginContact未在Swift 2+中工作;斯皮特基特

didBeginContact未在Swift 2+中工作;斯皮特基特,swift,sprite-kit,swift2,skspritenode,Swift,Sprite Kit,Swift2,Skspritenode,我正在制作一个游戏,我正在使用spritekit和Swift 这是我的代码: import SpriteKit struct collision { static let arrow:UInt32 = 0x1 << 1 static let runner:UInt32 = 0x1 << 2 static let target:UInt32 = 0x1 << 3 static let targetCenter:UInt32 =

我正在制作一个游戏,我正在使用spritekit和Swift

这是我的代码:

import SpriteKit

struct collision {
    static let arrow:UInt32 = 0x1 << 1
    static let runner:UInt32 = 0x1 << 2
    static let target:UInt32 = 0x1 << 3
    static let targetCenter:UInt32 = 0x1 << 4
}

class GameScene: SKScene, SKPhysicsContactDelegate {

    var person = SKSpriteNode()
    var box = SKSpriteNode()

    var screenSize:CGSize!
    var gameScreenSize:CGSize!

    var gameStarted:Bool = false

    var moveAndRemove = SKAction()

    var boxVelocity:NSTimeInterval = 5.5

    override func didMoveToView(view: SKView) {

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

        screenSize = self.frame.size
        gameScreenSize = view.frame.size

        createPerson()
    }

    func createPerson() -> Void {
        person.texture = SKTexture(imageNamed:"person")
        person.setScale(1.0)
        person.size = CGSize(width: 80, height: 80)
        person.position = CGPoint(x: screenSize.width / 2, y: 150)

        person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size)
        person.physicsBody?.affectedByGravity = false
        person.physicsBody?.dynamic = false

        self.addChild(person)
    }

    func createTarget() -> Void {
        box = SKSpriteNode()

        box.size = CGSize(width: 70, height: 100)
        box.setScale(1.0)

        box.position = CGPoint(x: (screenSize.width / 3) * 2, y: screenSize.height + box.size.height)
        box.texture = SKTexture(imageNamed: "box")

        box.physicsBody? = SKPhysicsBody(rectangleOfSize: box.size)
        box.physicsBody?.categoryBitMask = collision.target
        box.physicsBody?.collisionBitMask = collision.arrow
        box.physicsBody?.contactTestBitMask = collision.targetCenter
        box.physicsBody?.affectedByGravity = false
        box.physicsBody?.dynamic = true
        box.physicsBody?.usesPreciseCollisionDetection = true

        self.addChild(box)

        let distance = CGFloat(self.frame.height - box.frame.height)
        let moveTargets = SKAction.moveToY(-distance, duration: boxVelocity)
        let removeTargets = SKAction.removeFromParent()

        moveAndRemove = SKAction.sequence([moveTargets,removeTargets])
        box.runAction(moveAndRemove)
    }

    func createBall() ->Void {
        let ball = SKSpriteNode()
        ball.size = CGSize(width: 20, height: 22)
        ball.zPosition = 5

        let moveToXY = CGPoint(x: self.size.width, y: self.size.height)

        ball.texture = SKTexture(imageNamed: "ball")
        ball.position = CGPointMake(person.position.x + ball.size.width, person.position.y + ball.size.height)

        ball.physicsBody? = SKPhysicsBody(rectangleOfSize: ball.size)
        ball.physicsBody?.categoryBitMask = collision.arrow
        ball.physicsBody?.collisionBitMask = collision.target
        ball.physicsBody?.affectedByGravity = false
        ball.physicsBody?.dynamic = true
        ball.physicsBody?.usesPreciseCollisionDetection = true

        let action = SKAction.moveTo(moveToXY, duration: 1.5)
        let delay = SKAction.waitForDuration(1.5)
        ball.runAction(SKAction.sequence([action,delay]))

        self.addChild(ball)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if gameStarted == false {
            gameStarted = true

            let spawn = SKAction.runBlock { () in
                self.createTarget()
            }
            let delay = SKAction.waitForDuration(1.5)
            let spawnDelay = SKAction.sequence([spawn, delay])
            let spanDelayForever = SKAction.repeatActionForever(spawnDelay)
            self.runAction(spanDelayForever)
        } else {
            createBall()
            boxVelocity -= 0.1
        }
    }

    func didBeginContact(contact: SKPhysicsContact) {
        print("Detect")
    }

    func didEndContact(contact: SKPhysicsContact) {
        print("end detect")
    }

    override func update(currentTime: CFTimeInterval) {

    }
}
导入SpriteKit
结构碰撞{

静态let arrow:UInt32=0x1尝试以下修改:

 box.physicsBody = SKPhysicsBody(rectangleOfSize: box.size)
 box.physicsBody?.categoryBitMask = collision.target
 box.physicsBody?.collisionBitMask = collision.arrow
 box.physicsBody?.contactTestBitMask = collision.targetCenter

请注意,在初始化
physicsBody
和新的
contactTestBitMask

ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.size)
ball.physicsBody?.categoryBitMask = collision.arrow
ball.physicsBody?.collisionBitMask = collision.target
ball.physicsBody?.contactTestBitMask = collision.target