Swift 检测 SKNode 是否是自定义 class 的 属性
Swift detecting if a SKNode is a property of custom class
您好,我有一个自定义 class,Ship,它有一个 属性 'Parrent',它是一个 SKNode。当 class 被初始化时,一堆节点被添加到 'Parrent' 中,构成了 class 飞船的外观。 'Ship' 有一种方法 'Explode' 可以向不同方向射击所有节点。这部分效果很好,关于我的问题。
我试图让飞船在我接触到它们时爆炸,但我无法通过 'Ship' 爆炸。现在,当我触摸时,我使用 nodeAtPoint 但这只会让我成为外观节点之一。从那里我需要一种方法将父节点向上移动到 'Parrent' 节点,然后从那里向上移动到整个 Ship 对象。
AKA touch->nodeAtPoint->Appearance node->.parrent->'Parrent'->(卡在这里)->Ship->Ship.explode
我希望我正在努力完成的事情有意义,感谢您的帮助。
飞船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
}
触摸开始于 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
}
}
谢谢!我非常感谢任何帮助!
这终于奏效了。我将飞船 class 更改为从 SKNode 继承并开始编辑触摸:
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)
}
}
}
您好,我有一个自定义 class,Ship,它有一个 属性 'Parrent',它是一个 SKNode。当 class 被初始化时,一堆节点被添加到 'Parrent' 中,构成了 class 飞船的外观。 'Ship' 有一种方法 'Explode' 可以向不同方向射击所有节点。这部分效果很好,关于我的问题。
我试图让飞船在我接触到它们时爆炸,但我无法通过 'Ship' 爆炸。现在,当我触摸时,我使用 nodeAtPoint 但这只会让我成为外观节点之一。从那里我需要一种方法将父节点向上移动到 'Parrent' 节点,然后从那里向上移动到整个 Ship 对象。
AKA touch->nodeAtPoint->Appearance node->.parrent->'Parrent'->(卡在这里)->Ship->Ship.explode
我希望我正在努力完成的事情有意义,感谢您的帮助。
飞船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
}
触摸开始于 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
}
}
谢谢!我非常感谢任何帮助!
这终于奏效了。我将飞船 class 更改为从 SKNode 继承并开始编辑触摸:
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)
}
}
}