我目前正在为Swift 4中的iOS编写应用程序,但是在尝试检测两个“ SkSpritenodes”碰到对方时会遇到问题–检测联系人。
乍一看,我已经完成了所有设置位掩码等操作,但是似乎我没有以正确的方式完成操作。
这是我的代码,也许您可以帮助我。
我认为这也是许多其他人可能遇到的一个问题,也许是因为掩码错误?
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let bananenkategorie: UInt32 = 0x1 << 1
let ananaskategorie: UInt32 = 0x1 << 2
let kirschekategorie: UInt32 = 0x1 << 3
let traubekategorie: UInt32 = 0x1 << 4
let bombe1kategorie: UInt32 = 0x1 << 5
let bomb2kategorie: UInt32 = 0x1 << 6
let korbkategorie: UInt32 = 0x1 << 0
var korb = SKSpriteNode()
var timer: Timer!
func neuesobst(art: String, menge: Int, geschwindigkeit: TimeInterval){
if art == "Banane"{
let banane = SKSpriteNode(imageNamed: "Banane.png")
banane.size = CGSize(width: 120, height: 120)
banane.color = UIColor.yellow
var randomx = Int(arc4random_uniform(UInt32(self.frame.size.width)))
if randomx < Int(banane.size.width){randomx = randomx + Int(banane.size.width)}
if randomx > Int(self.frame.size.width - banane.size.width){randomx = randomx - Int(banane.size.width)}
banane.position = CGPoint(x: CGFloat(randomx), y: self.frame.size.height+banane.size.height)
banane.physicsBody? = SKPhysicsBody(circleOfRadius: 60)
banane.physicsBody?.categoryBitMask = bananenkategorie
banane.physicsBody?.collisionBitMask = 0
banane.physicsBody?.contactTestBitMask = korbkategorie
banane.physicsBody?.affectedByGravity = true
banane.physicsBody?.isDynamic = true
self.addChild(banane)
banane.run(SKAction.moveTo(y: -10, duration: geschwindigkeit))
// adding one sprite, I ad all other sprites the same way.
override func didMove(to view: SKView) {
korb = SKSpriteNode(imageNamed: "Korb.png")
korb.size = CGSize(width: 300, height: 200)
korb.physicsBody?.categoryBitMask = korbkategorie
korb.physicsBody?.collisionBitMask = 0
korb.physicsBody?.contactTestBitMask = bananenkategorie|ananaskategorie|traubekategorie|kirschekategorie|bomb2kategorie|bombe1kategorie
korb.physicsBody? = SKPhysicsBody(rectangleOf: CGSize(width: 300, height: 200))
korb.position = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.1)
korb.physicsBody?.isDynamic = true
korb.physicsBody?.affectedByGravity = true
self.addChild(korb)
self.physicsWorld.gravity = CGVector(dx: 100, dy: 100)
self.physicsWorld.contactDelegate = self
timer = Timer.scheduledTimer(timeInterval: 0.9, target: self, selector: #selector(spawn), userInfo: nil, repeats: true)
} // here is: the did move to view function. So I am checking the collision of the "Korb" node and the "Banane" node.
func didBegin(_ contact: SKPhysicsContact) {
korb.alpha = 0 // just to test if anything works
} // but it doesn't it doesn't even go into this function anytime even when you clearly see a collision between two sprites on screen
答案 0 :(得分:0)
查看您的代码:
override func didMove(to view: SKView) {
korb = SKSpriteNode(imageNamed: "Korb.png")
korb.size = CGSize(width: 300, height: 200)
korb.physicsBody?.categoryBitMask = korbkategorie
korb.physicsBody?.collisionBitMask = 0
korb.physicsBody?.contactTestBitMask = bananenkategorie|ananaskategorie|traubekategorie|kirschekategorie|bomb2kategorie|bombe1kategorie
korb.physicsBody? = SKPhysicsBody(rectangleOf: CGSize(width: 300, height: 200))
korb.position = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.1)
korb.physicsBody?.isDynamic = true
korb.physicsBody?.affectedByGravity = true
self.addChild(korb)
self.physicsWorld.gravity = CGVector(dx: 100, dy: 100)
self.physicsWorld.contactDelegate = self
timer = Timer.scheduledTimer(timeInterval: 0.9, target: self, selector: #selector(spawn), userInfo: nil, repeats: true)
} // here is: the did move to view function. So I am checking the collision of the "Korb" node and the "Banane" node.
特别是:
korb.physicsBody?.categoryBitMask = korbkategorie
korb.physicsBody?.collisionBitMask = 0
korb.physicsBody?.contactTestBitMask = bananenkategorie|ananaskategorie|traubekategorie|kirschekategorie|bomb2kategorie|bombe1kategorie
korb.physicsBody? = SKPhysicsBody(rectangleOf: CGSize(width: 300, height: 200))
您拥有:
无
无
无
身体
这不是您想要的。
在处理可选内容时,请使用!
而不是?
并使程序崩溃。
这将帮助您更轻松地调试代码。
只有当您期望nil是有效值时,才应该放置?
。