質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
SpriteKit

SpriteKitは、iOSやOS Xで使用できるApple社製の2Dゲーム開発フレームワークです。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1961閲覧

swift 他のファイルから値を参照する

Jose

総合スコア14

SpriteKit

SpriteKitは、iOSやOS Xで使用できるApple社製の2Dゲーム開発フレームワークです。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/06/10 16:33

前提・実現したいこと

プログラムの勉強中です。
Spritekitを使用したアプリケーションを開発中ですが、

GameScene と CustomDialog という二つのファイルがあります。

CustomDialogのtextFieldで入力した文字列を、
Gamesceneのself.myLabel.textの中に入れたいと考えております。

方法ご教示のほどお願いいたします。

GameScene.swift

GameScene.swift

1 2import SpriteKit 3import GameplayKit 4import Foundation 5import UIKit 6 7 8class GameScene: SKScene 9{ 10 11 var circle = SKShapeNode(circleOfRadius: 100) 12 var location = CGPoint(x:0,y:0) 13 var animator: UIDynamicAnimator? 14 var i = 0 15 var myLabel = SKLabelNode(fontNamed:"Chalkduster") 16 var currentMoney = 0 17 var cumulativeNumberOfTouches = 0 18 var myField : SKFieldNode! 19 20 21 override func didMove(to view: SKView) 22 { 23 self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame) 24 // self.backgroundColor = UIColor.white 25 // ボタンを生成. 26 let myButton = UIButton() 27 myButton.frame = CGRect(x:0,y:0,width:200,height:40) 28 myButton.backgroundColor = UIColor.red 29 myButton.layer.masksToBounds = true 30 myButton.setTitle("Add Block", for: []) 31 myButton.setTitleColor(UIColor.white, for: []) 32 myButton.setTitle("Done", for: UIControlState.highlighted) 33 myButton.setTitleColor(UIColor.black, for: UIControlState.highlighted) 34 myButton.layer.cornerRadius = 20.0 35 myButton.layer.position = CGPoint(x: self.view!.frame.width/2, y:200) 36 myButton.addTarget(self, action: #selector(onClickMyButton(sender:)), for: UIControlEvents.touchUpInside) 37 self.view?.addSubview(myButton) 38 } 39 40 41 @objc func onClickMyButton(sender:UIButton) 42 { 43 animator?.removeAllBehaviors() 44 self.i = i+1 45 46 self.circle = SKShapeNode(circleOfRadius: 100) 47 self.circle.physicsBody = SKPhysicsBody(circleOfRadius: 100) 48 self.circle.fillColor = SKColor.white 49 self.circle.position = location 50 self.circle.physicsBody!.affectedByGravity = true 51 self.circle.physicsBody?.restitution = 0.5 52 self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame) 53 self.circle.name = "Circle(String(i))" 54 self.circle.fillTexture = SKTexture(imageNamed: "Bubble.png") 55 self.circle.glowWidth = 5.0 56 self.circle.removeFromParent() 57 self.addChild(circle) 58 59// self.myLabel = SKLabelNode(fontNamed:"Chalkduster") 60// self.myLabel.text = "Hello, World!(String(i))"; 61// self.myLabel.fontSize = 20 62// self.myLabel.position = circle.position 63// self.myLabel.fontColor = UIColor.red 64// self.myLabel.name = "myLabel(String(i))" 65// self.myLabel.removeFromParent() 66// circle.addChild(myLabel) 67 68 // カスタムダイアログを生成. 69 let dialog = CustomDialog(scene: self, frame:CGRect(x:0, y:0, width:self.view!.bounds.maxX - 50, height:300)) 70 self.view!.addSubview(dialog) 71 72 self.myLabel = SKLabelNode(fontNamed:"Chalkduster") 73// ⭐️↓ここに文字列を持って来たいと思います???? 74// self.myLabel.text = CustomDialog. 75 self.myLabel.fontSize = 20 76 self.myLabel.position = circle.position 77 self.myLabel.fontColor = UIColor.red 78 self.myLabel.name = "myLabel(String(i))" 79 self.myLabel.removeFromParent() 80 circle.addChild(myLabel) 81 82 } 83 84 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) 85 { 86 87 for _ : AnyObject in touches 88 { 89 let location = touches.first!.location(in: self) 90 let node : SKNode? = self.atPoint(location) 91 print("Node Name: (String(describing: node?.name))") 92 if node?.name != nil 93 { 94 let location = touches.first!.location(in: self) 95 let node : SKNode? = self.atPoint(location) 96 _ = node?.name 97 if (node?.name?.contains("Circle"))! 98 { 99 circle = node as! SKShapeNode 100 print("Node Name: (String(describing: node?.name))") 101 102 } 103 } 104 } 105 106 107 } 108 109 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) 110 { 111 for _ : AnyObject in touches 112 { 113 let location = touches.first!.location(in: self) 114 let node : SKNode? = self.atPoint(location) 115 if node?.name != nil 116 { 117 let location = touches.first!.location(in: self) 118 let action = SKAction.move(to: CGPoint(x:location.x, y:location.y), duration:0) 119 self.circle.physicsBody!.affectedByGravity = false 120 circle.run(action) 121 } 122 } 123 } 124 125 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) 126 { 127 let location = touches.first!.location(in: self) 128 let node : SKNode? = self.atPoint(location) 129 if node?.name != nil 130 { 131 animator?.removeAllBehaviors() 132 self.circle.physicsBody!.affectedByGravity = true 133 physicsWorld.gravity = CGVector(dx:0,dy:-7.0) 134 } 135 136 137 } 138 139 140 override func update(_ currentTime: TimeInterval) 141 { 142 } 143 144}

CustomDialog.swift

CustomDialog.swift

1import UIKit 2import SpriteKit 3 4class CustomDialog : UIView 5{ 6 var backGroundView : UIView! 7 var scene : SKScene! 8 var textField : UITextField! 9 10 required init(coder aDecoder: NSCoder) 11 { 12 super.init(coder: aDecoder)! 13 } 14 15 init(scene : SKScene,frame : CGRect) 16 { 17 super.init(frame: scene.view!.bounds) 18 // 自分が追加されたシーン. 19 self.scene = scene 20 // シーン内をポーズ. 21 self.scene.view!.isPaused = true 22 // シーン内のタッチを検出させなくする. 23 self.scene.isUserInteractionEnabled = false 24 self.layer.zPosition = 10 25 // シーン全体を被せる背景を追加. 26 self.backGroundView = UIView(frame: scene.view!.bounds) 27 self.backGroundView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3) 28 self.backGroundView.layer.position = scene.view!.center 29 self.addSubview(backGroundView) 30 // ダイアログの背景を追加. 31 let board = UIView(frame: frame) 32 board.backgroundColor = UIColor.yellow 33 board.layer.position = backGroundView.center 34 board.layer.masksToBounds = true 35 board.layer.cornerRadius = 20.0 36 board.layer.borderColor = UIColor.black.cgColor 37 self.addSubview(board) 38// // ラベルを追加. 39// let textView = UILabel(frame: CGRect(x:0, y:0, width:200,height:50)) 40// textView.text = "hogehoge" 41// textView.textAlignment = NSTextAlignment.center 42// textView.layer.position = backGroundView.center 43// textView.backgroundColor = UIColor.clear 44// textView.textColor = UIColor.black 45// self.addSubview(textView) 46 // テキストを追加. 47 textField = UITextField(frame: CGRect(x:0, y:50, width:200,height:50)) 48 textField.placeholder = "Enter" 49 textField.borderStyle = UITextBorderStyle.roundedRect 50 textField.autocorrectionType = UITextAutocorrectionType.yes 51 textField.clearButtonMode = UITextFieldViewMode.whileEditing 52 textField.autocapitalizationType = UITextAutocapitalizationType.allCharacters 53// textField.textAlignment = NSTextAlignment.center 54 textField.layer.position = backGroundView.center 55 textField.keyboardType = UIKeyboardType.twitter 56 self.addSubview(textField) 57 58 // 送信ボタンを追加. 59 let sendButton = UIButton(type: UIButtonType.contactAdd) 60 sendButton.tintColor = UIColor.black 61 sendButton.layer.position = CGPoint(x:board.bounds.maxX - textField.bounds.midX - 70, y:textField.bounds.midY + 180) 62 sendButton.addTarget(self, action: #selector(sendButton(sender:)), for: UIControlEvents.touchUpInside) 63 board.addSubview(sendButton) 64 65 // 閉じるボタンを追加. 66 let myWindowExitButton = UIButton(type: UIButtonType.contactAdd) 67 myWindowExitButton.tintColor = UIColor.black 68 myWindowExitButton.layer.position = CGPoint(x:board.bounds.maxX - myWindowExitButton.bounds.midX - 5, y:myWindowExitButton.bounds.midY + 5) 69 // 追加ボタンを回転させる事で閉じるボタンっぽくみせる. 70 myWindowExitButton.transform = CGAffineTransform(rotationAngle: CGFloat((45.0 * Double.pi) / 180.0)) 71 myWindowExitButton.addTarget(self, action: #selector(onExitButton(sender:)), for: UIControlEvents.touchUpInside) 72 board.addSubview(myWindowExitButton) 73 } 74 75 @objc func sendButton(sender : UILabel) 76 { 77 let mokuhyou = String?(textField.text!)! 78 print("your input:(String(mokuhyou))") 79 } 80 81 @objc func onExitButton(sender : UIButton) 82 { 83 // シーン内のボーズを解除. 84 self.scene.view!.isPaused = false 85 // シーン内のタッチを検出させる. 86 self.scene.isUserInteractionEnabled = true 87 // シーンから自身を削除. 88 self.removeFromSuperview() 89 } 90 91}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2018/06/11 00:06

「ここに文字列を持って来たいと思います」のところでは、まだテキストが入力されていないように思うのですが?
Jose

2018/06/11 15:22

試してみましたが、たしかに mokuhyou = nil となりエラー落ちします。。。どこに移植するのが良いでしょうか?
guest

回答1

0

ベストアンサー

こんな感じでいいのかわかりませんが、
グローバル変数を使うのはどうでしょうか?
このままだとテキストフィールドの送信のタイミングがずれますが・・

CustomDialog.swift

1import UIKit 2import SpriteKit 3 4var mokuhyou : String? = nil 5class CustomDialog : UIView 6{ 7 var backGroundView : UIView! 8 var scene : SKScene! 9 var textField : UITextField! 10 11 required init(coder aDecoder: NSCoder) 12 { 13 super.init(coder: aDecoder)! 14 } 15 16 init(scene : SKScene,frame : CGRect) 17 { 18 super.init(frame: scene.view!.bounds) 19 // 自分が追加されたシーン. 20 self.scene = scene 21 // シーン内をポーズ. 22 self.scene.view!.isPaused = true 23 // シーン内のタッチを検出させなくする. 24 self.scene.isUserInteractionEnabled = false 25 self.layer.zPosition = 10 26 // シーン全体を被せる背景を追加. 27 self.backGroundView = UIView(frame: scene.view!.bounds) 28 self.backGroundView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3) 29 self.backGroundView.layer.position = scene.view!.center 30 self.addSubview(backGroundView) 31 // ダイアログの背景を追加. 32 let board = UIView(frame: frame) 33 board.backgroundColor = UIColor.yellow 34 board.layer.position = backGroundView.center 35 board.layer.masksToBounds = true 36 board.layer.cornerRadius = 20.0 37 board.layer.borderColor = UIColor.black.cgColor 38 self.addSubview(board) 39 // // ラベルを追加. 40 // let textView = UILabel(frame: CGRect(x:0, y:0, width:200,height:50)) 41 // textView.text = "hogehoge" 42 // textView.textAlignment = NSTextAlignment.center 43 // textView.layer.position = backGroundView.center 44 // textView.backgroundColor = UIColor.clear 45 // textView.textColor = UIColor.black 46 // self.addSubview(textView) 47 // テキストを追加. 48 textField = UITextField(frame: CGRect(x:0, y:50, width:200,height:50)) 49 textField.placeholder = "Enter" 50 textField.borderStyle = UITextBorderStyle.roundedRect 51 textField.autocorrectionType = UITextAutocorrectionType.yes 52 textField.clearButtonMode = UITextFieldViewMode.whileEditing 53 textField.autocapitalizationType = UITextAutocapitalizationType.allCharacters 54 // textField.textAlignment = NSTextAlignment.center 55 textField.layer.position = backGroundView.center 56 textField.keyboardType = UIKeyboardType.twitter 57 self.addSubview(textField) 58 59 // 送信ボタンを追加. 60 let sendButton = UIButton(type: UIButtonType.contactAdd) 61 sendButton.tintColor = UIColor.black 62 sendButton.layer.position = CGPoint(x:board.bounds.maxX - textField.bounds.midX - 70, y:textField.bounds.midY + 180) 63 sendButton.addTarget(self, action: #selector(sendButton(sender:)), for: UIControlEvents.touchUpInside) 64 board.addSubview(sendButton) 65 66 // 閉じるボタンを追加. 67 let myWindowExitButton = UIButton(type: UIButtonType.contactAdd) 68 myWindowExitButton.tintColor = UIColor.black 69 myWindowExitButton.layer.position = CGPoint(x:board.bounds.maxX - myWindowExitButton.bounds.midX - 5, y:myWindowExitButton.bounds.midY + 5) 70 // 追加ボタンを回転させる事で閉じるボタンっぽくみせる. 71 myWindowExitButton.transform = CGAffineTransform(rotationAngle: CGFloat((45.0 * Double.pi) / 180.0)) 72 myWindowExitButton.addTarget(self, action: #selector(onExitButton(sender:)), for: UIControlEvents.touchUpInside) 73 board.addSubview(myWindowExitButton) 74 } 75 76 @objc func sendButton(sender : UILabel) 77 { 78 mokuhyou = String?(textField.text!)! 79 print("your input:(String(describing: mokuhyou))") 80 81 } 82 83 @objc func onExitButton(sender : UIButton) 84 { 85 // シーン内のボーズを解除. 86 self.scene.view!.isPaused = false 87 // シーン内のタッチを検出させる. 88 self.scene.isUserInteractionEnabled = true 89 // シーンから自身を削除. 90 self.removeFromSuperview() 91 } 92 93}

GameScene.swift

1import SpriteKit 2import GameplayKit 3import Foundation 4import UIKit 5 6 7class GameScene: SKScene 8{ 9 10 var circle = SKShapeNode(circleOfRadius: 100) 11 var location = CGPoint(x:110,y:110) 12 var animator: UIDynamicAnimator? 13 var i = 0 14 var myLabel = SKLabelNode(fontNamed:"Chalkduster") 15 var currentMoney = 0 16 var cumulativeNumberOfTouches = 0 17 var myField : SKFieldNode! 18 19 20 override func didMove(to view: SKView) 21 { 22 self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame) 23 // self.backgroundColor = UIColor.white 24 // ボタンを生成. 25 let myButton = UIButton() 26 myButton.frame = CGRect(x:0,y:0,width:200,height:40) 27 myButton.backgroundColor = UIColor.red 28 myButton.layer.masksToBounds = true 29 myButton.setTitle("Add Block", for: []) 30 myButton.setTitleColor(UIColor.white, for: []) 31 myButton.setTitle("Done", for: UIControlState.highlighted) 32 myButton.setTitleColor(UIColor.black, for: UIControlState.highlighted) 33 myButton.layer.cornerRadius = 20.0 34 myButton.layer.position = CGPoint(x: self.view!.frame.width/2, y:200) 35 myButton.addTarget(self, action: #selector(onClickMyButton(sender:)), for: UIControlEvents.touchUpInside) 36 self.view?.addSubview(myButton) 37 } 38 39 40 @objc func onClickMyButton(sender:UIButton) 41 { 42 animator?.removeAllBehaviors() 43 self.i = i+1 44 45 self.circle = SKShapeNode(circleOfRadius: 100) 46 self.circle.physicsBody = SKPhysicsBody(circleOfRadius: 100) 47 self.circle.fillColor = SKColor.white 48 self.circle.position = location 49 self.circle.physicsBody!.affectedByGravity = true 50 self.circle.physicsBody?.restitution = 0.5 51 self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame) 52 self.circle.name = "Circle(String(i))" 53 self.circle.fillTexture = SKTexture(imageNamed: "Bubble") 54 self.circle.glowWidth = 5.0 55 self.circle.removeFromParent() 56 self.addChild(circle) 57 58 // self.myLabel = SKLabelNode(fontNamed:"Chalkduster") 59 // self.myLabel.text = "Hello, World!(String(i))"; 60 // self.myLabel.fontSize = 20 61 // self.myLabel.position = circle.position 62 // self.myLabel.fontColor = UIColor.red 63 // self.myLabel.name = "myLabel(String(i))" 64 // self.myLabel.removeFromParent() 65 // circle.addChild(myLabel) 66 67 // カスタムダイアログを生成. 68 let dialog = CustomDialog(scene: self, frame:CGRect(x:0, y:0, width:self.view!.bounds.maxX - 50, height:300)) 69 self.view!.addSubview(dialog) 70 71 self.myLabel = SKLabelNode(fontNamed:"Chalkduster") 72 // ⭐️↓ここに文字列を持って来たいと思います???? 73 self.myLabel.text = mokuhyou 74 self.myLabel.fontSize = 20 75 self.myLabel.position = circle.position 76 self.myLabel.fontColor = UIColor.red 77 self.myLabel.name = "myLabel(String(i))" 78 self.myLabel.removeFromParent() 79 circle.addChild(myLabel) 80 81 } 82 83 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) 84 { 85 86 for _ : AnyObject in touches 87 { 88 let location = touches.first!.location(in: self) 89 let node : SKNode? = self.atPoint(location) 90 print("Node Name: (String(describing: node?.name))") 91 if node?.name != nil 92 { 93 let location = touches.first!.location(in: self) 94 let node : SKNode? = self.atPoint(location) 95 _ = node?.name 96 if (node?.name?.contains("Circle"))! 97 { 98 circle = node as! SKShapeNode 99 print("Node Name: (String(describing: node?.name))") 100 101 } 102 } 103 } 104 105 106 } 107 108 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) 109 { 110 for _ : AnyObject in touches 111 { 112 let location = touches.first!.location(in: self) 113 let node : SKNode? = self.atPoint(location) 114 if node?.name != nil 115 { 116 let location = touches.first!.location(in: self) 117 let action = SKAction.move(to: CGPoint(x:location.x, y:location.y), duration:0) 118 self.circle.physicsBody!.affectedByGravity = false 119 circle.run(action) 120 } 121 } 122 } 123 124 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) 125 { 126 let location = touches.first!.location(in: self) 127 let node : SKNode? = self.atPoint(location) 128 if node?.name != nil 129 { 130 animator?.removeAllBehaviors() 131 self.circle.physicsBody!.affectedByGravity = true 132 physicsWorld.gravity = CGVector(dx:0,dy:-7.0) 133 } 134 135 136 } 137 138 139 override func update(_ currentTime: TimeInterval) 140 { 141 } 142 143} 144

投稿2018/06/10 22:59

tanakana

総合スコア150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Jose

2018/06/11 14:48

Cannot convert value of type 'NSCoder.Type' to expected argument type 'NSCoder' とエラーがでてしまいます
tanakana

2018/06/12 14:26

私の環境では、エラーは出ていません。
Jose

2018/06/12 15:40

修正しましたが、ご指摘の通りテキストが入るタイミングがずれました。 テキストを入れることはできたので、一旦クローズします。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問