-2

Hi I have a custom class, Ship, that has a property 'Parrent' that is an SKNode. When the class is initialized a bunch of nodes are added to that 'Parrent' making up the appearance of the class ship. 'Ship' has a method 'Explode' that shoots all of the nodes in different directions. This part works great, on to my issue.

I am trying to get the Ships to explode when I touch them, but I am having trouble passing which 'Ship' is to explode. Right now when I touch I use nodeAtPoint but this only gets me one of the appearance nodes. From there I need a way to go up a parent to 'Parrent' node and then from there up to Ship object as a whole.

AKA touch->nodeAtPoint->Appearance node->.parrent->'Parrent'->(get stuck here)->Ship->Ship.explode

I hope what is I am trying to accomplish makes sense, thanks for any help.

Ship class:

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
}

Touches began in 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
    }
}

Thanks! Id greatly appreciate any help!

T Neate
  • 403
  • 2
  • 6
  • 18

1 Answers1

0

This is what finally worked. I changed the ship class to inherit from SKNode and edited touches began as such:

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

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let targetShip = nodeAtPoint(location)
        if targetShip.isMemberOfClass(Ship){
            print("Ship")
            Ship1.shootLazer(targetShip as! Ship)
        }else if targetShip.parent!.isMemberOfClass(Ship){
            print("Ship parrent")
            Ship1.shootLazer(targetShip.parent! as! Ship)
        }
    }
}
T Neate
  • 403
  • 2
  • 6
  • 18