无法将'Bird'类型的值转换为'预期参数类型'SKTexture'

时间:2022-05-17 16:35:05
self.name = "Bird"

self.physicsBody = SKPhysicsBody(texture: Bird, size: self.size)

I am having trouble with Physicsbody, I try to use bird shape for the collision instead of circleRadius. How can I fix the problem?

我在使用Physicsbody时遇到了麻烦,我尝试使用鸟形来代替circleRadius。我该如何解决这个问题?

1 个解决方案

#1


1  

If you want to subclass SKSpriteNode to define your own sprite and you want to use the texture for physics collision detection then here's the code

如果你想继承SKSpriteNode来定义你自己的精灵,你想使用纹理进行物理碰撞检测,那么这里是代码

class BirdSprite: SKSpriteNode {

    init() {
        let texture = SKTexture(imageNamed: "birdImage")
        super.init(texture: texture, color: .clearColor(), size: texture.size())
        physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
        name = "Bird"
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

#1


1  

If you want to subclass SKSpriteNode to define your own sprite and you want to use the texture for physics collision detection then here's the code

如果你想继承SKSpriteNode来定义你自己的精灵,你想使用纹理进行物理碰撞检测,那么这里是代码

class BirdSprite: SKSpriteNode {

    init() {
        let texture = SKTexture(imageNamed: "birdImage")
        super.init(texture: texture, color: .clearColor(), size: texture.size())
        physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
        name = "Bird"
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}