【发布时间】:2015-09-18 18:56:32
【问题描述】:
您好,我有一个自定义类 Ship,它有一个属性“Parrent”,它是一个 SKNode。当类被初始化时,一堆节点被添加到构成类船外观的“Parrent”中。 'Ship' 有一种方法 'Explode' 可以向不同方向射击所有节点。这部分效果很好,可以解决我的问题。
我试图让飞船在我触摸它们时爆炸,但我无法通过哪条“飞船”将爆炸。现在,当我触摸时,我使用 nodeAtPoint 但这只会让我获得外观节点之一。从那里我需要一种方法将父节点上升到“Parrent”节点,然后从那里上升到整个 Ship 对象。
AKA touch->nodeAtPoint->外观节点->.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
}
}
谢谢!非常感谢任何帮助!
【问题讨论】:
-
那么我会做类似'如果 x 是 Ship.parrent {'吗?
-
好的,请给我一个更具体的例子
-
“父母”,而不是“父母”。
-
感谢您对拼写发表评论而不回答问题,非常有帮助。并且 fyi 故意拼错以区别 SKNode 'parent' 的属性