Swift检测SKNode是否属于自定义类

时间:2015-09-18 18:56:33

标签: swift sknode

您好我有一个自定义类Ship,它有一个属性'Parrent'是一个SKNode。当类被初始化时,一堆节点被添加到'Parrent'中,构成了类船的外观。 '船'有一种方法'爆炸',可以射击不同方向的所有节点。关于我的问题,这部分很有用。

当我碰到它时,我试图让船舶爆炸,但是我遇到了“船只”爆炸的麻烦。现在,当我触摸时,我使用nodeAtPoint,但这只是我的一个外观节点。从那里我需要一种方法将父母上升到'Parrent'节点,然后从那里一直到Ship对象作为一个整体。

AKA touch-> nodeAtPoint-> Appearance node-> .parrent->'Parrent' - >(卡在这里) - > Ship-> Ship.explode

我希望我想要完成的是有道理的,谢谢你的帮助。

船级:

class Ship: NSObject {
var Position: CGPoint!
var Scene: SKNode!
var Parrent = SKNode();
let Parts = [
    SKSpriteNode(imageNamed: "Ship/Ship1.png"),
    SKSpriteNode(imageNamed: "Ship/Ship2.png"),
    SKSpriteNode(imageNamed: "Ship/Ship3.png"),
    SKSpriteNode(imageNamed: "Ship/Ship4.png"),
    SKSpriteNode(imageNamed: "Ship/Ship5.png"),
    SKSpriteNode(imageNamed: "Ship/Ship6.png"),
];

init(position : CGPoint, parrent : SKNode, scaleFactor: CGFloat) {
    self.Position = position;
    self.Scene =  parrent;

    self.Parrent.position = self.Position
    var x = 0;
    for part in Parts {
        x++;
        part.xScale = scaleFactor
        part.yScale = abs(scaleFactor)
        part.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Ship/Ship\(x).png"), size: part.size)
        part.physicsBody?.collisionBitMask = PhysicsCategory.Ship
        part.physicsBody?.categoryBitMask = PhysicsCategory.Something
        part.position = CGPoint(x: 0, y: 0)
        self.Parrent.addChild(part)
    }
    self.Scene.addChild(self.Parrent)
}

//other functions including explode
}

触动始于SKScene

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let targetShip = nodeAtPoint(location)
        let targetParrent = targetShip.parent
        //decide if it is a 'Ship' here
    }
}

谢谢!我非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

这是最终奏效的。我将船级更改为继承自SKNode,并且编辑后的触摸开始如下:

.on();