gpt4 book ai didi

swift - 将 Sprite 附加到 SKPhysicsBody

转载 作者:行者123 更新时间:2023-11-28 06:10:28 24 4
gpt4 key购买 nike

我正在制作一个 Sprite 动画,当它接触到另一个 Sprite 时,会调用一个物理接触函数。然后,在这个函数中,我试图让 Sprite 接触到另一个。它的主体是 bodyA 但它是一个 SKPhysicsBody,它不能转换为 SKSpriteNode。你有什么想法?接触时正确调用该函数,我只是尝试获取 body 接触的 Sprite 。最终的想法是通过我正在寻找的方式将 Action 附加到 sprite,但我想当您拥有 sprite 时,这很容易。

    let shootCategory: UInt32 = 0x1 << 0
let enemyCategory: UInt32 = 0x1 << 1


// Declaration of the SKPhysicsBody of the sprite wich will touch the other one
sprite.physicsBody = SKPhysicsBody(circleOfRadius: (20))
sprite.physicsBody?.usesPreciseCollisionDetection = true
sprite.physicsBody?.categoryBitMask = shootCategory
sprite.physicsBody?.collisionBitMask = shootCategory | enemyCategory
sprite.physicsBody?.contactTestBitMask = shootCategory | enemyCategory
sprite.physicsBody?.affectedByGravity = false

// Declaration of the SKPhysicsBody of the sprite wich will be touched
sprite.run(SKAction.group([moveAction, fadeInAction]))
sprite.physicsBody = SKPhysicsBody(circleOfRadius: (20))
sprite.physicsBody?.usesPreciseCollisionDetection = true
sprite.physicsBody?.categoryBitMask = enemyCategory
sprite.physicsBody?.collisionBitMask = shootCategory | enemyCategory
sprite.physicsBody?.contactTestBitMask = shootCategory | enemyCategory
sprite.physicsBody?.affectedByGravity = false


func didBegin(_ contact: SKPhysicsContact) {

if (contact.bodyA.categoryBitMask == enemyCategory) && (contact.bodyB.categoryBitMask == shootCategory){

// I tried this to get the sprite wich the bodyB is attached to but it doesn't even build
let sprite: SKSpriteNode = sprite.contact.bodyB
contact.bodyA.removeAction(forKey: "moveToAction")
}
}

最佳答案

我终于找到了非常简单的解决方案:

    let Node = contact.bodyB.node as! SKSpriteNode

关于swift - 将 Sprite 附加到 SKPhysicsBody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46867940/

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