基本的に、skpritenodeクラスを拡張するオブジェクトに含まれている2つのメソッドを呼び出す必要があるゲームがあります。touches beginメソッドを使用してノードのタッチを検出し、そのノードからオブジェクトへの参照をプルしてから、touchesBeganメソッド内の一時変数に参照を割り当てて、そのメソッドを呼び出せるようにします。これが不明確な場合は申し訳ありませんが、私はまだ質問を作成するための「正しい」方法に慣れようとしています。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if(atPoint((touch?.location(in: self))!) == homeButton) && scene?.isPaused == false{
goToHomeG()
}
else {
if let location = touch?.location(in: self){
let theNodes = nodes(at: location)
for node in theNodes{
if node.name == "yesText" {
let transition = SKTransition.flipHorizontal(withDuration: 0.5)
let menuSceneOB = MenuScene(fileNamed: "MenuScene")
menuSceneOB?.scaleMode = .aspectFill
self.view?.presentScene(menuSceneOB!, transition: transition)
node.removeFromParent()
}
else if node.name == "noText" {
for child in self.children {
if child.name == "goToHomeText"{
child.removeFromParent()
}
if child.name == "noText"{
child.removeFromParent()
}
if child.name == "yesText"{
child.removeFromParent()
}
if child.name == "box"{
child.removeFromParent()
}
}
scene?.isPaused = false
}
else if node.name == "enemy"{
//here is where I want to detect if the object is an enemy and then use the assignment to the location to modify the object with its own internal methods.
var modify : EnemyChar?
modify = node
modify.update
}
}
}
}
}
私が正しく理解していれば、あなたはただキャストする必要がありますnode
:
else if node.name == "enemy"{
//here is where I want to detect if the object is an enemy and then use the assignment to the location to modify the object with its own internal methods.
var enemy = node as? EnemyChar
enemy.update()
}
ちなみに、このコード:
for child in self.children {
if child.name == "goToHomeText"{
child.removeFromParent()
}
if child.name == "noText"{
child.removeFromParent()
}
if child.name == "yesText"{
child.removeFromParent()
}
if child.name == "box"{
child.removeFromParent()
}
}
次のように簡略化できます。
for child in self.children {
if ["goToHomeText", "noText", "yesText", "box"].contains(child.name) {
child.removeFromParent()
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加