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

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

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

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

Xcode

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

Swift

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

Q&A

0回答

246閲覧

swift プログラムの実行順について SKSceneとUIView

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/12 16:18

前提・実現したいこと

ポップアップで入力したテキストを、生成されるノードの中に入れたい

発生している問題・エラーメッセージ

現在の動き
ポップアップ表示→テキスト記入→送信ボタン(+)→ノード生成→ノードラベル生成→テキスト反映

希望の動き
ポップアップ表示→テキスト記入→送信ボタン(+)→テキスト反映→ノード生成→ノードラベル生成

説明がうまくできず、申し訳ありません。

※現在、自分の知識でできる方法として、「テキストセットボタン」と「ノード生成ボタン」
を作成する方法を思いつきましたが、使い勝手を優先して、送信ボタン(+)だけを押したら
テキストが書き換わって、ノードが表示されるようにしたいと思います。

プログラム改修方法、ご教示のほどお願いいたします。

該当のソースコード

GameScene.swift(SKScene)

swift

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

CustomDialog.swift(UIView)

swift

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

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問