使用SpriteKit

时间:2017-02-15 03:17:01

标签: sprite-kit swift3

我使用SpriteKit在Swift 3中遇到了联系人检测问题。联系人检测工作......有时候。它似乎是随机的,它何时发射,何时不发射。 我有一个黄色的“子弹”,它在屏幕上向上移动,命中一个名为targetSprite的红色精灵。所需的行为是在击中目标时移除子弹,但有时它只是从下面穿过。 我发现很多关于接触检测的问题根本没有用,但是我没有发现任何与检测不一致的问题。

我该怎么做才能解决这个问题?

以下是代码:

import SpriteKit
import GameplayKit

enum PhysicsCategory:UInt32 {
    case bullet = 1
    case sprite1 = 2
    case targetSprite = 4
    // each new value should double the previous
}

class GameScene: SKScene, SKPhysicsContactDelegate {

// Create sprites
let sprite1 = SKSpriteNode(color: SKColor.blue, size: CGSize(width:100,height:100))
let targetSprite = SKSpriteNode(color: SKColor.red, size: CGSize(width:100,height:100))
let bullet = SKSpriteNode(color: SKColor.yellow, size: CGSize(width: 20, height: 20))
// show the bullet?
var isShowingBullet = true

// Timers
//var timer:Timer? = nil
var fireBulletTimer:Timer? = nil

// set up bullet removal:
var bulletShouldBeRemoved = false


let bulletMask = PhysicsCategory.bullet.rawValue


override func didMove(to view: SKView) {

    // Physics
    targetSprite.physicsBody = SKPhysicsBody(rectangleOf: targetSprite.centerRect.size)
    targetSprite.physicsBody?.affectedByGravity = false

    bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.centerRect.size)
    bullet.physicsBody?.affectedByGravity = false


    // Contact Detection:
    targetSprite.physicsBody?.categoryBitMask = PhysicsCategory.targetSprite.rawValue

    targetSprite.physicsBody?.contactTestBitMask =
        //PhysicsCategory.sprite1.rawValue |
        PhysicsCategory.bullet.rawValue

    targetSprite.physicsBody?.collisionBitMask = 0 // no collision detection


    // bullet physics
    bullet.physicsBody?.categoryBitMask = PhysicsCategory.bullet.rawValue

    bullet.physicsBody?.contactTestBitMask =
        PhysicsCategory.targetSprite.rawValue

    bullet.physicsBody?.collisionBitMask = 0 // no collision detection


    // execute once:
    fireBulletTimer = Timer.scheduledTimer(timeInterval: 1,
                                           target: self,
                                           selector: #selector(self.fireBullet),
                                           userInfo: nil,
                                           repeats: false)

    // Add sprites to the scene:
    self.addChild(sprite1)
    self.addChild(bullet)
    self.addChild(targetSprite)

    // Positioning
    targetSprite.position = CGPoint(x:0, y:300)
    // Note: bullet and sprite1 are at 0,0 by default

    // Delegate
    self.physicsWorld.contactDelegate = self

}

func didBegin(_ contact: SKPhysicsContact) {

    print("didBegin(contact:))")

    //let firstBody:SKPhysicsBody
   // let otherBody:SKPhysicsBody

    // Use 'bitwise and' to see if both bits are 1:
    if contact.bodyA.categoryBitMask & bulletMask > 0 {

        //firstBody = contact.bodyA
        //otherBody = contact.bodyB
        print("if contact.bodyA....")
        bulletShouldBeRemoved = true
    }
    else {
        //firstBody = contact.bodyB
        //otherBody = contact.bodyA
        print("else - if not contacted?")
    }

    /*
    // Find the type of contact:
    switch otherBody.categoryBitMask {
        case PhysicsCategory.targetSprite.rawValue: print(" targetSprite hit")
        case PhysicsCategory.sprite1.rawValue: print(" sprite1 hit")
        case PhysicsCategory.bullet.rawValue: print(" bullet hit")

        default: print(" Contact with no game logic")
    }
    */


} // end didBegin()


func didEnd(_ contact: SKPhysicsContact) {
    print("didEnd()")

}

func fireBullet() {

    let fireBulletAction = SKAction.move(to: CGPoint(x:0,y:500), duration: 1)
    bullet.run(fireBulletAction)

}

func showBullet() {

    // Toggle to display or not, every 1 second:
    if isShowingBullet == true {
        // remove (hide) it:
        bullet.removeFromParent()
        // set up the toggle for the next call:
        isShowingBullet = false
        // debug:
        print("if")

    }
    else {
        // show it again:
        self.addChild(bullet)
        // set up the toggle for the next call:
        isShowingBullet = true
        // debug:
        print("else")
    }

}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered

    if bulletShouldBeRemoved {
        bullet.removeFromParent()
    }

}

}

对于不一致的缩进感到抱歉,我似乎无法找到一种简单的方法来执行此操作...

修改

我发现使用'frame'而不是'centerRect'会使碰撞区域成为精灵的大小。例如:

    targetSprite.physicsBody = SKPhysicsBody(rectangleOf: targetSprite.centerRect.size)

应该是:

    targetSprite.physicsBody = SKPhysicsBody(rectangleOf: targetSprite.frame.size)

3 个答案:

答案 0 :(得分:4)

第一个建议 - 不要在SpriteKit中使用NSTimer(aka计时器)。它不与游戏循环配对,并且可能在不同情况下导致不同的问题。阅读更多here(LearnCocos2D发布的答案)

所以,这样做:

 let wait = SKAction.wait(forDuration: 1)

 run(wait, completion: {
     [unowned self] in
      self.fireBullet()
 })

我注意到的是,如果我在Simulator中运行您的代码,我会得到您描述的行为。 didBegin(contact:)被随机解雇。尽管如此,对我来说这不是在设备上发生的,设备测试才是最重要的。

现在,当我删除Timer并对SKAction(s)做同样的事情时,一切都有效,意味着每次都会检测到联系。

答案 1 :(得分:2)

您是否尝试过添加

.physicsBody?.isDynamic = true
.physicsBody?.usesPreciseCollisionDetrction =true

答案 2 :(得分:0)

如果您执行以下操作,SpriteKit物理引擎将正确计算碰撞:

1)为bullet的物理主体设置“usesPreciseCollisionDetection”属性为true。这将改变该机构的碰撞检测算法。您可以找到有关此属性的更多信息here,“使用碰撞和联系人”一章。

2)使用applyImpulse或applyForce方法移动你的子弹。如果通过手动更改身体移动身体,碰撞检测将无法正确进行。您可以在here,“让物理机构移动”这一章中找到更多信息。