如何修复 SKPhysicsContactDelegate 未被调用
How to fix SKPhysicsContactDelegate not being called
我的 sprite 应该相互接触并打印到控制台,但是,一个在另一个后面,它们实际上并没有接触。不用说,没有任何内容被打印到控制台。
我试过在函数中使用许多不同的 "types" of if 语句,但是,其中 none 有效。例如,我试过使用:
if bodyA.categoryBitMask == 1 && bodyB.categoryBitMask == 2 || bodyA.categoryBitMask == 2 && bodyB.categoryBitMask == 1
以及:
如果联系。bodyA.categoryBitMask <联系。bodyB.categoryBitMask
非常感谢您的帮助! :)
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var e = SKSpriteNode()
var square = SKSpriteNode()
var timer:Timer!
let squareCategory:UInt32 = 0x1 << 1
let eCategory:UInt32 = 0x1 << 2
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
square = SKSpriteNode(imageNamed: "square")
square.size = CGSize(width: 100, height: 100)
square.physicsBody = SKPhysicsBody(rectangleOf: square.size)
square.position = CGPoint(x: 0, y: -590)
square.physicsBody = SKPhysicsBody(rectangleOf: e.size)
square.name = "square"
square.physicsBody?.categoryBitMask = squareCategory
square.physicsBody?.contactTestBitMask = eCategory
square.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(square)
let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.swipedRight(sender:)))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector (GameScene.swipedLeft(sender:)))
swipeLeft.direction = .left
view.addGestureRecognizer(swipeLeft)
timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.adde), userInfo: nil, repeats: true)
}
@objc func swipedRight(sender: UISwipeGestureRecognizer) {
//middle
if square.position == CGPoint(x: 0, y: -590) {
square.position = CGPoint(x: 200, y: -590)
}
//left
if square.position == CGPoint(x: -200, y: -590) {
square.position = CGPoint(x: 0, y: -590)
}
}
@objc func swipedLeft(sender: UISwipeGestureRecognizer) {
//middle
if square.position == CGPoint(x: 0, y: -590) {
square.position = CGPoint(x: -200, y: -590)
}
//right
if square.position == CGPoint(x: 200, y: -590) {
square.position = CGPoint(x: 0, y: -590)
}
}
@objc func adde() {
e = SKSpriteNode(imageNamed: "e")
let ePosition = GKRandomDistribution(lowestValue: -414, highestValue: 414)
let position = CGFloat(ePosition.nextInt())
e.size = CGSize(width: 75, height: 75)
e.position = CGPoint(x: position, y: 640)
e.physicsBody = SKPhysicsBody(rectangleOf: e.size)
e.name = "e"
e.physicsBody?.categoryBitMask = eCategory
e.physicsBody?.contactTestBitMask = squareCategory
e.physicsBody?.isDynamic = true
self.addChild(e)
let animationDuration:TimeInterval = 6
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: CGPoint(x: position, y: -705), duration: animationDuration))
actionArray.append(SKAction.removeFromParent())
e.run(SKAction.sequence(actionArray))
}
func didBegin(_ contact: SKPhysicsContact) {
let bodyAName = contact.bodyA.node?.name
let bodyBName = contact.bodyB.node?.name
if bodyAName == "square" && bodyBName == "e" || bodyAName == "e" && bodyBName == "square"{
if bodyAName == "square" {
contact.bodyA.node?.removeFromParent()
} else if bodyBName == "e" {
contact.bodyB.node?.removeFromParent()
}
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
我应该收到消息,但是 "e" 落后于 "square",我没有收到任何消息。
您的代码有错误。
更改以下内容:
square = SKSpriteNode(imageNamed: "square")
square.size = CGSize(width: 100, height: 100)
square.physicsBody = SKPhysicsBody(rectangleOf: square.size)
square.position = CGPoint(x: 0, y: -590)
square.physicsBody = SKPhysicsBody(rectangleOf: e.size)
square.name = "square"
至
square = SKSpriteNode(imageNamed: "square")
square.color = UIColor.red
square.size = CGSize(width: 100, height: 100)
square.physicsBody = SKPhysicsBody(rectangleOf: square.size)
square.position = CGPoint(x: 0, y: horizotaly)
//square.physicsBody = SKPhysicsBody(rectangleOf: e.size)
square.physicsBody?.affectedByGravity = false
square.name = "square"
您的问题已解决。
我的 sprite 应该相互接触并打印到控制台,但是,一个在另一个后面,它们实际上并没有接触。不用说,没有任何内容被打印到控制台。
我试过在函数中使用许多不同的 "types" of if 语句,但是,其中 none 有效。例如,我试过使用:
if bodyA.categoryBitMask == 1 && bodyB.categoryBitMask == 2 || bodyA.categoryBitMask == 2 && bodyB.categoryBitMask == 1
以及:
如果联系。bodyA.categoryBitMask <联系。bodyB.categoryBitMask
非常感谢您的帮助! :)
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var e = SKSpriteNode()
var square = SKSpriteNode()
var timer:Timer!
let squareCategory:UInt32 = 0x1 << 1
let eCategory:UInt32 = 0x1 << 2
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
square = SKSpriteNode(imageNamed: "square")
square.size = CGSize(width: 100, height: 100)
square.physicsBody = SKPhysicsBody(rectangleOf: square.size)
square.position = CGPoint(x: 0, y: -590)
square.physicsBody = SKPhysicsBody(rectangleOf: e.size)
square.name = "square"
square.physicsBody?.categoryBitMask = squareCategory
square.physicsBody?.contactTestBitMask = eCategory
square.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(square)
let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.swipedRight(sender:)))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector (GameScene.swipedLeft(sender:)))
swipeLeft.direction = .left
view.addGestureRecognizer(swipeLeft)
timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.adde), userInfo: nil, repeats: true)
}
@objc func swipedRight(sender: UISwipeGestureRecognizer) {
//middle
if square.position == CGPoint(x: 0, y: -590) {
square.position = CGPoint(x: 200, y: -590)
}
//left
if square.position == CGPoint(x: -200, y: -590) {
square.position = CGPoint(x: 0, y: -590)
}
}
@objc func swipedLeft(sender: UISwipeGestureRecognizer) {
//middle
if square.position == CGPoint(x: 0, y: -590) {
square.position = CGPoint(x: -200, y: -590)
}
//right
if square.position == CGPoint(x: 200, y: -590) {
square.position = CGPoint(x: 0, y: -590)
}
}
@objc func adde() {
e = SKSpriteNode(imageNamed: "e")
let ePosition = GKRandomDistribution(lowestValue: -414, highestValue: 414)
let position = CGFloat(ePosition.nextInt())
e.size = CGSize(width: 75, height: 75)
e.position = CGPoint(x: position, y: 640)
e.physicsBody = SKPhysicsBody(rectangleOf: e.size)
e.name = "e"
e.physicsBody?.categoryBitMask = eCategory
e.physicsBody?.contactTestBitMask = squareCategory
e.physicsBody?.isDynamic = true
self.addChild(e)
let animationDuration:TimeInterval = 6
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: CGPoint(x: position, y: -705), duration: animationDuration))
actionArray.append(SKAction.removeFromParent())
e.run(SKAction.sequence(actionArray))
}
func didBegin(_ contact: SKPhysicsContact) {
let bodyAName = contact.bodyA.node?.name
let bodyBName = contact.bodyB.node?.name
if bodyAName == "square" && bodyBName == "e" || bodyAName == "e" && bodyBName == "square"{
if bodyAName == "square" {
contact.bodyA.node?.removeFromParent()
} else if bodyBName == "e" {
contact.bodyB.node?.removeFromParent()
}
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
我应该收到消息,但是 "e" 落后于 "square",我没有收到任何消息。
您的代码有错误。
更改以下内容:
square = SKSpriteNode(imageNamed: "square")
square.size = CGSize(width: 100, height: 100)
square.physicsBody = SKPhysicsBody(rectangleOf: square.size)
square.position = CGPoint(x: 0, y: -590)
square.physicsBody = SKPhysicsBody(rectangleOf: e.size)
square.name = "square"
至
square = SKSpriteNode(imageNamed: "square")
square.color = UIColor.red
square.size = CGSize(width: 100, height: 100)
square.physicsBody = SKPhysicsBody(rectangleOf: square.size)
square.position = CGPoint(x: 0, y: horizotaly)
//square.physicsBody = SKPhysicsBody(rectangleOf: e.size)
square.physicsBody?.affectedByGravity = false
square.name = "square"
您的问题已解决。