Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么可以';将ContactBody强制转换为SpriteKit中的自定义SKNode_Ios_Swift_Collision_Contact - Fatal编程技术网

Ios 为什么可以';将ContactBody强制转换为SpriteKit中的自定义SKNode

Ios 为什么可以';将ContactBody强制转换为SpriteKit中的自定义SKNode,ios,swift,collision,contact,Ios,Swift,Collision,Contact,我在尝试将ContactBody强制转换为自定义节点时遇到问题。实现如下所示: case PhysicsCategory.player | PhysicsCategory.spring: if contact.bodyA.categoryBitMask == PhysicsCategory.spring{ if let theSpring = contact.bodyA.node as? Spring {

我在尝试将ContactBody强制转换为自定义节点时遇到问题。实现如下所示:

    case PhysicsCategory.player | PhysicsCategory.spring:
        if contact.bodyA.categoryBitMask == PhysicsCategory.spring{

            if let theSpring = contact.bodyA.node as? Spring
            {
                theSpring.printMsg()
            }else{
               print("can't cast bodyA to spring")
            }
        }else{
            if let theSpring = contact.bodyB.node as? Spring
            {
                theSpring.printMsg()
            }else{
                print("can't cast bodyB to spring")
            }
        }
import SpriteKit
class Spring: SKNode {

var impulse: Int
var sprite: SKSpriteNode
var textureAtlas: SKTextureAtlas
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
init (launchSpeed: Int){
    impulse = launchSpeed
    textureAtlas = SKTextureAtlas(named: "NonTiledItems")
    sprite = SKSpriteNode(texture: textureAtlas.textureNamed("spring01"))
    sprite.physicsBody =  SKPhysicsBody(rectangleOfSize: sprite.size)
    sprite.physicsBody!.categoryBitMask = PhysicsCategory.spring // could we make this button?
    sprite.physicsBody!.allowsRotation = false
    sprite.physicsBody!.dynamic = false
    sprite.physicsBody!.affectedByGravity = false
    sprite.physicsBody!.friction = 0.6
    sprite.physicsBody!.collisionBitMask = PhysicsCategory.player
    super.init()
    addChild(sprite)
}

func printMsg(){
    print("contact Made")
}
}
convenience init (launchSpeed: Int)    
{
    self.SKSpriteNode(texture: SKTextureAtlas(named: "NonTiledItems").textureNamed("spring01"))
    impulse = launchSpeed

    self.physicsBody =  SKPhysicsBody(rectangleOfSize: self.size)
    self.physicsBody!.categoryBitMask = PhysicsCategory.spring // could we make this button?
    self.physicsBody!.allowsRotation = false
    self.physicsBody!.dynamic = false
    self.physicsBody!.affectedByGravity = false
    self.physicsBody!.friction = 0.6
    self.physicsBody!.collisionBitMask = PhysicsCategory.player


}
当播放器和弹簧接触时,我得到消息“不能将bodyA投射到弹簧”。弹簧类如下所示:

    case PhysicsCategory.player | PhysicsCategory.spring:
        if contact.bodyA.categoryBitMask == PhysicsCategory.spring{

            if let theSpring = contact.bodyA.node as? Spring
            {
                theSpring.printMsg()
            }else{
               print("can't cast bodyA to spring")
            }
        }else{
            if let theSpring = contact.bodyB.node as? Spring
            {
                theSpring.printMsg()
            }else{
                print("can't cast bodyB to spring")
            }
        }
import SpriteKit
class Spring: SKNode {

var impulse: Int
var sprite: SKSpriteNode
var textureAtlas: SKTextureAtlas
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
init (launchSpeed: Int){
    impulse = launchSpeed
    textureAtlas = SKTextureAtlas(named: "NonTiledItems")
    sprite = SKSpriteNode(texture: textureAtlas.textureNamed("spring01"))
    sprite.physicsBody =  SKPhysicsBody(rectangleOfSize: sprite.size)
    sprite.physicsBody!.categoryBitMask = PhysicsCategory.spring // could we make this button?
    sprite.physicsBody!.allowsRotation = false
    sprite.physicsBody!.dynamic = false
    sprite.physicsBody!.affectedByGravity = false
    sprite.physicsBody!.friction = 0.6
    sprite.physicsBody!.collisionBitMask = PhysicsCategory.player
    super.init()
    addChild(sprite)
}

func printMsg(){
    print("contact Made")
}
}
convenience init (launchSpeed: Int)    
{
    self.SKSpriteNode(texture: SKTextureAtlas(named: "NonTiledItems").textureNamed("spring01"))
    impulse = launchSpeed

    self.physicsBody =  SKPhysicsBody(rectangleOfSize: self.size)
    self.physicsBody!.categoryBitMask = PhysicsCategory.spring // could we make this button?
    self.physicsBody!.allowsRotation = false
    self.physicsBody!.dynamic = false
    self.physicsBody!.affectedByGravity = false
    self.physicsBody!.friction = 0.6
    self.physicsBody!.collisionBitMask = PhysicsCategory.player


}

我不明白为什么ContactBody不会强制转换到Spring类,因为Spring继承自SKNode。如果有人有任何建议,我们将不胜感激。

您的物理体连接到一个SKSpriteNode,而不是弹簧。您需要删除skspritenode引用,您需要Spring成为skspritenode的子级,并且您需要编写一个如下所示的便利初始化:

    case PhysicsCategory.player | PhysicsCategory.spring:
        if contact.bodyA.categoryBitMask == PhysicsCategory.spring{

            if let theSpring = contact.bodyA.node as? Spring
            {
                theSpring.printMsg()
            }else{
               print("can't cast bodyA to spring")
            }
        }else{
            if let theSpring = contact.bodyB.node as? Spring
            {
                theSpring.printMsg()
            }else{
                print("can't cast bodyB to spring")
            }
        }
import SpriteKit
class Spring: SKNode {

var impulse: Int
var sprite: SKSpriteNode
var textureAtlas: SKTextureAtlas
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
init (launchSpeed: Int){
    impulse = launchSpeed
    textureAtlas = SKTextureAtlas(named: "NonTiledItems")
    sprite = SKSpriteNode(texture: textureAtlas.textureNamed("spring01"))
    sprite.physicsBody =  SKPhysicsBody(rectangleOfSize: sprite.size)
    sprite.physicsBody!.categoryBitMask = PhysicsCategory.spring // could we make this button?
    sprite.physicsBody!.allowsRotation = false
    sprite.physicsBody!.dynamic = false
    sprite.physicsBody!.affectedByGravity = false
    sprite.physicsBody!.friction = 0.6
    sprite.physicsBody!.collisionBitMask = PhysicsCategory.player
    super.init()
    addChild(sprite)
}

func printMsg(){
    print("contact Made")
}
}
convenience init (launchSpeed: Int)    
{
    self.SKSpriteNode(texture: SKTextureAtlas(named: "NonTiledItems").textureNamed("spring01"))
    impulse = launchSpeed

    self.physicsBody =  SKPhysicsBody(rectangleOfSize: self.size)
    self.physicsBody!.categoryBitMask = PhysicsCategory.spring // could we make this button?
    self.physicsBody!.allowsRotation = false
    self.physicsBody!.dynamic = false
    self.physicsBody!.affectedByGravity = false
    self.physicsBody!.friction = 0.6
    self.physicsBody!.collisionBitMask = PhysicsCategory.player


}

非常感谢,我已经附加了SKSpriteNode,所以我可以向对象添加图像,但是物理体不需要是sprite的一部分,只需要对象本身。谢谢你澄清。不客气,我有点困惑,我正在添加图像,但我确实注意到我在之后分配了纹理图集,所以我正在编辑代码以更正它