Cannot cast SKNode to subclass of SKNode

大兔子大兔子 提交于 2019-12-11 12:35:42

问题


I am returning an SKNode from one function and I need to cast it to a custom SKNode. I get the error Cannot assign value ofSKNodeto typeGroundNode. If I force cast, it compiles, but fails at runtime. What am I missing?

// Custom node class
class GroundNode: SKNode {
        weak var entity: GKEntity!
    }

// Return node function
func returnNode() -> SKNode {
    ...
    return node
}

// Where I am getting the error
func setupNode() {
    var ground: GroundNode
    ground = returnNode() // error here.
    //// ground = returnNode() as! GroundNode fails at runtime.
}

EDIT: I am getting an SKNode from an sks file. My returnNode() just get the child with name, and returns it to my setupNode() function. I need to add the entity property, so I want to cast my returned SKNode to a GroundNode type.

I have seen this stackoverflow post.

This works with SKSpiteNode, but apparently not with SKNode, which does not make much sense to me.

If I cast my SKNode from my sks file to a GroundNode, it crashes at runtime.


回答1:


Based on your code:

// Custom node class
class GroundNode: SKNode {
    weak var entity: GKEntity! = GKEntity() // or some custom initialization...
}

// Where I am getting the error
func setupNode() {
   var ground: GroundNode
   ground = returnNode() as? GroundNode
}

This happened because returnNode output is a generic SKNode and you must explicit your casting to the subclassed GroundNode.

EDIT: Ok, with your update I think I've understand your issue, you've forgot to set the custom class for your GroundNode:




回答2:


first

returnNode() returns an instance of SKNode class. It might also return an instance of derived class (e.g. GroundNode), but call site doesn't know that from the function declaration. You are trying to assign this instance to the variable of SKNode's subclass - GroundNode which is not appropriate in OOP.

See Dynamic Binding OOP concept:

In object- oriented programming languages, a variable of a superclass type can contain a subclass instance.

(possibly, someone else could explain this in more details, but this is how things look to me)

second

Type Casting. When you suspect that some variable of a class type, might hold an instance of derived class, you could do a type casting in order to proof that.

returnNode() returns an instance of SKNode OR any derived class, but the call site wants to process only instances of derived class (GroundNode), hence one of type casting technique should be used.

import GameplayKit
import SpriteKit

// Custom node class
class GroundNode: SKNode {
    var entity: GKEntity = GKEntity()
}

// Return node function
func returnNode() -> SKNode {
    // let's assume that you are correctly getting an instance
    // of GroundNode class here, and return it from the function
    // as of SKNode type
    let returnNode: SKNode = GroundNode()
    return returnNode
}

func setupNode() {
    var groundNode: GroundNode?

    // Type Casting

    // Approach #1
    let someNode = returnNode()
    if someNode is GroundNode {
        groundNode = someNode as! GroundNode
    }

    // Approach #2
    groundNode = returnNode() as? GroundNode

    print("groundNode = \(groundNode)")
}



回答3:


I think it should be

func returnNode() -> GroundNode {



来源:https://stackoverflow.com/questions/37460222/cannot-cast-sknode-to-subclass-of-sknode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!