gpt4 book ai didi

swift - didBeginContact 无法正常工作

转载 作者:可可西里 更新时间:2023-11-01 01:05:35 24 4
gpt4 key购买 nike

我为每个想要相互交互的不同对象创建了一个 struct PhysicsCatagory

struct PhysicsCatagory {
static let Blade : UInt32 = 1
static let Laser : UInt32 = 2

在我的类 GameScene 之上

 class GameScene: SKScene, SKPhysicsContactDelegate {

然后我在 didMoveToView 方法中用它的 physicsBody 初始化了一个 SKSpriteNode Blade

override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
Blade.position = CGPointMake(self.size.width / 2, (self.size.height / 14))
Blade.anchorPoint = CGPointMake(0.5, -0.13)
Blade.physicsBody = SKPhysicsBody(rectangleOfSize: Blade.size)
Blade.physicsBody?.categoryBitMask = PhysicsCatagory.Blade
Blade.physicsBody?.contactTestBitMask = PhysicsCatagory.Laser
Blade.physicsBody?.dynamic = false

self.addChild(Blade)
}

以及方法 shootLaser 中的 SKSpriteNode 激光及其物理体

func shootLaser(){
var Laser = SKSpriteNode(imageNamed: "Laser.png")
Laser.position = Enemy.position
Laser.zPosition = -5
Laser.physicsBody = SKPhysicsBody(rectangleOfSize: Laser.size)
Laser.physicsBody?.categoryBitMask = PhysicsCatagory.Laser
Laser.physicsBody?.contactTestBitMask = PhysicsCatagory.Blade
Laser.physicsBody?.dynamic = false

let action = SKAction.moveBy(laserVector, duration: 0.7)
let actionDone = SKAction.removeFromParent()
Laser.runAction(SKAction.sequence([action,actionDone]))
self.addChild(Laser)
}

但是当他们在模拟中发生碰撞时,并没有调用didBeginContact方法,也没有打印“Hello”

func didBeginContact(contact: SKPhysicsContact) {
NSLog("Hello")
}

为什么在碰撞时不调用 didBeginContact 方法?提前致谢 (:

最佳答案

Sprite Kit 不会检查非动态物理体之间的接触,因为它们预计不会移动。如果您不希望您的 Sprite 由于重力而从屏幕上掉下来,请将物理体的 affectedByGravity 属性设置为 false 并设置 dynamic = true.

关于swift - didBeginContact 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31388144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com