teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

2018/12/21 09:37

投稿

syakemi
syakemi

スコア16

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  質問を修正します
2
- if文の中にarc4randomを入れることはできますか? またarc4randomを入れたの中に更にifを入れられますか?
2
+ if文の中にarc4randomを入れることはできますか? またarc4randomを入れたif文の中に更にifを入れられますか?
3
3
 
4
4
 
5
5
  変数aが10以上の時ランダムに文を出力、その文が出力された時、更にランダムに文を出力

2

2018/12/21 09:37

投稿

syakemi
syakemi

スコア16

title CHANGED
@@ -1,1 +1,1 @@
1
- arc4randomを利用して
1
+ arc4random
body CHANGED
@@ -1,122 +1,6 @@
1
+ 質問を修正します
1
- ### arc4randomを使って3つ画像ImageVIewを利用して表示したい
2
+ if文の中にarc4randomを入れることはできますか? またarc4randomを入れた中に更にif入れられますか?
2
3
 
3
- とある避けゲーを制作しているのですが敵役三種をランダムに生成してバラバラの位置に生成する。という所で止まっています。
4
-
5
- ### 発生している問題・エラーメッセージ
6
-
7
- ```
4
+
8
- エラーメッセージ
9
- ```
10
-
11
- ### 該当のソースコード
12
-
13
- import UIKit
14
-
15
- class ViewController: UIViewController {
5
+ 変数aが10以上の時ランダムに文を出力、その文が出力された時、更にランダムに文を出力
16
-
17
- // 画像インスタンス
18
- let imageBag = UIImageView()
19
- var myimage1 = UIImageView()
20
- let myimage2 = UIImageView()
21
- let myimage3 = UIImageView()
22
- var timer : Timer!
6
+ (出力はすべてprintで出します)
23
- var cnt : Float = 0.0
24
- var a : Int!
25
- override func viewDidLoad() {
26
- super.viewDidLoad()
27
-
28
- // Screen Size の取得
29
- let screenWidth:CGFloat = view.frame.size.width
30
- let screenHeight:CGFloat = view.frame.size.height
31
-
32
- // ハンドバッグの画像を設定
33
- imageBag.image = UIImage(named: "りんご.pmg")
34
- myimage1.image = UIImage(named: "餌.png")
35
- myimage2.image = UIImage(named: "餌.png")
36
- myimage3.image = UIImage(named: "餌.png")
37
-
38
- // 画像のフレームを設定
39
- imageBag.frame = CGRect(x:0, y:0, width:128, height:128)
40
- myimage1.frame = CGRect(x:0, y:0, width:128, height:128)
41
- myimage2.frame = CGRect(x:0, y:0, width:128, height:128)
42
- myimage3.frame = CGRect(x:0, y:0, width:128, height:128)
43
-
44
- // 画像をスクリーン中央に設定
45
- imageBag.center = CGPoint(x:screenWidth/2, y:screenHeight/2)
46
- myimage1.center = CGPoint(x: screenHeight + 10, y: screenHeight + 10)
47
- myimage2.center = CGPoint(x: screenHeight + 10, y: screenHeight + 10)
48
- myimage3.center = CGPoint(x: screenHeight + 10, y: screenHeight + 10)
49
-
50
- // タッチ操作を enable
51
- imageBag.isUserInteractionEnabled = true
52
- self.view.addSubview(imageBag)
53
-
54
- }
55
-
56
-
57
-
58
- // 画面にタッチで呼ばれる
59
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
60
- let random = arc4random() % 10
61
- if random < 3 {
62
- }
63
- }
64
-
65
- // ドラッグ時に呼ばれる
66
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
67
-
68
- // タッチイベントを取得
69
-
70
- let touchEvent = touches.first!
71
-
72
- // ドラッグ前の座標, Swift 1.2 から
73
- let preDx = touchEvent.previousLocation(in: self.view).x
74
- let preDy = touchEvent.previousLocation(in: self.view).y
75
-
76
- // ドラッグ後の座標
77
- let newDx = touchEvent.location(in: self.view).x
78
- let newDy = touchEvent.location(in: self.view).y
79
-
80
- // ドラッグしたx座標の移動距離
81
- let dx = newDx - preDx
82
- print("x:(dx)")
83
-
84
- // ドラッグしたy座標の移動距離
85
- let dy = newDy - preDy
86
- print("y:(dy)")
87
-
88
- // 画像のフレーム
89
- var viewFrame: CGRect = imageBag.frame
90
-
91
- // 移動分を反映させる
92
- viewFrame.origin.x += dx
93
- viewFrame.origin.y += dy
94
-
95
- imageBag.frame = viewFrame
96
-
97
- self.view.addSubview(imageBag)
98
-
99
- }
100
- func onUpdate(timer : Timer){
101
-
102
- cnt += 0.1
103
-
104
-
105
- }
106
-
107
- // 小数点以下2桁のみ表示
108
-
109
- override func didReceiveMemoryWarning() {
110
- super.didReceiveMemoryWarning()
111
- // Dispose of any resources that can be recreated.
112
- }
113
- }
114
-
115
-
116
- ### 試したこと
117
-
118
-
119
-
120
- ### 補足情報(FW/ツールのバージョンなど)
121
-
122
- ここにより詳細な情報を記載してください。

1

2018/12/21 09:36

投稿

syakemi
syakemi

スコア16

title CHANGED
File without changes
body CHANGED
File without changes