节点与子类的联系

时间:2015-06-01 20:25:12

标签: ios swift sprite-kit

我正在创建一个类来将我的Meteors添加到场景中:

import SpriteKit

class Meteor: SKShapeNode {

    var meteorNode : SKShapeNode?
    var size : CGSize!
    //Acoes
    var acaoApagar = SKAction.removeFromParent()
    var acaoAndar : SKAction!

    //Numero Randomico
    var numRandom : Int = 0
    var textoStringMeteor : String!

    //Colisoes
    let CollisionShoot     : UInt32 = 0x1 << 0
    let CollisionHouse : UInt32 = 0x1 << 1
    let CollisionMeteor : UInt32 = 0x1 << 2

    init(size : CGSize, pontoMeteor: CGPoint, textoMeteor: String)
    {
        super.init()
        //super.init(ellipseOfSize: <#CGSize#>)

        self.size = size

        self.textoStringMeteor = textoMeteor
        self.acaoAndar = SKAction.moveTo(pontoMeteor, duration: 10)

        criarMeteoro()
        setarTexto(self.textoStringMeteor)

    }

    func setarTexto(numeroLabel : String) {

        let numNode = SKLabelNode(fontNamed:"Chalkduster")
        numNode.text = numeroLabel;
        numNode.fontSize = 20;
        numNode.position = CGPoint(x: 0, y: -10);
        numNode.name = "textoNode"
        meteorNode?.addChild(numNode)

    }

    func criarMeteoro() {

        var randomX = CGFloat(Int(arc4random()) % Int(size.width))

        //Meteor Node
        meteorNode = SKShapeNode(circleOfRadius: CGFloat(20))
        meteorNode!.physicsBody = SKPhysicsBody(circleOfRadius: 20)
        meteorNode!.physicsBody!.dynamic = true
        meteorNode!.fillColor = UIColor.blueColor()
        meteorNode!.physicsBody!.categoryBitMask = CollisionMeteor
        meteorNode!.physicsBody!.contactTestBitMask = CollisionShoot | CollisionHouse
        meteorNode!.position = CGPoint(x: randomX, y: size.height + 900)
        meteorNode!.name = "meteorNode"
        self.addChild(meteorNode!)

        meteorNode!.runAction(SKAction.sequence([acaoAndar, acaoApagar]))    
    }


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


}

这里我调用Meteor类并传递所有参数:

 var meteor = Meteor(size: CGSize(width: size.width, height: size.height), pontoMeteor: ponto, textoMeteor: "30")

    foreground.addChild(meteor)

最后我联系我,看看节点之间是否有接触:

 func didBeginContact(contact: SKPhysicsContact) {

        var bodyA = contact.bodyA!.node!
        var bodyB = contact.bodyB!.node!

        if bodyA.name == "tiroCanhao" && bodyB.name == "meteorNode"
        {
            bodyB.removeAllActions()
            bodyB.removeFromParent()
            bodyA.removeFromParent()
            self.pointsPlayer++
            self.qtdtiros += 2



        }
        else if bodyA.name == "meteorNode" && bodyB.name == "tiroCanhao"
        {
            bodyA.removeAllActions()
            bodyA.removeFromParent()
            bodyB.removeFromParent()
            self.pointsPlayer++
            self.qtdtiros += 2

        }
}

现在我的问题是:

在联系之后我想在对象的Meteor类上调用setarTexto方法并更改文本但不起作用。我收到这个错误:

  

无法转换类型&#39; SKShapeNode&#39; (0x10bb5ea88)来   &#39; MathDefense.Meteor&#39; (0x10af6b7b0)。

我在做什么(关于联系方式的if语句):

let text = bodyB as! Meteor
text.setarTexto("20")

我甚至尝试过

text.textoStringMeteor = "20"

但这些都没有奏效。我已经做过一些研究,但没有发现任何有我问题的人或试图做类似的事情。

任何解决方案?

感谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,为了解决我的错误,我做了以下内容:

删除SKShapenode,因为该类已经是SKShapenode(这是错误的)。

然后在创建我的节点时,我使用self而不是放入Meteornode。这解决了一切。

示例:

右:self.position = CGPoint(x:randomX,y:size.height + 900)

错误:meteorNode!.position = CGPoint(x:randomX,y:size.height + 900)

如果您正在使用SKSpritenode传递纹理,只需将其放在您的上层。