前提・実現したいこと
プログラムの勉強中です。
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}
回答1件
あなたの回答
tips
プレビュー