質問編集履歴

1

ご指摘ありがとうございました。コードを```で囲みました。

2019/05/18 00:28

投稿

tetutaroz
tetutaroz

スコア12

test CHANGED
File without changes
test CHANGED
@@ -50,11 +50,203 @@
50
50
 
51
51
 
52
52
 
53
+ ```
54
+
53
55
  Swift5
54
56
 
57
+ import Foundation
58
+
59
+ import SpriteKit
60
+
61
+
62
+
63
+ class GameScene: SKScene, SKPhysicsContactDelegate {
64
+
65
+ // どんぶり
66
+
67
+ var bowl: SKSpriteNode?
68
+
69
+ // タイマー
70
+
71
+ var timer: Timer?
72
+
73
+ // 落下判定用しシェイプ
74
+
75
+ var lowestShape: SKShapeNode?
76
+
77
+ // スコア用プロパティ
78
+
55
- ソースコード
79
+ var score = 0
80
+
56
-
81
+ var scoreLabel: SKLabelNode?
82
+
83
+ // 名古屋名物ごとのスコア
84
+
85
+ var scoreList = [100, 200, 300, 500, 800, 1000, 1500]
86
+
87
+
88
+
89
+ override func didMove(to view: SKView) {
90
+
91
+
92
+
93
+ self.physicsWorld.gravity = CGVector(dx: 0.0, dy: -2.0)
94
+
95
+
96
+
97
+ self.physicsWorld.contactDelegate = self
98
+
99
+
100
+
101
+ // 背景スプライトの追加
102
+
103
+ let background = SKSpriteNode(imageNamed: "background")
104
+
105
+ background.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.5)
106
+
107
+ background.size = self.size
108
+
109
+ self.addChild(background)
110
+
111
+
112
+
113
+ // 落下検知用シェイプノードを追加
114
+
115
+ let lowestShape = SKShapeNode(rectOf: CGSize(width: self.size.width*3, height: 10))
116
+
117
+ // 画面外に配置
118
+
119
+ lowestShape.position = CGPoint(x: self.size.width*0.5, y: -10)
120
+
121
+
122
+
123
+ // シェイプに合わせてPhysicsBodyを生成
124
+
125
+ // シェイプの大きさで物理シミュレーションを行う
126
+
127
+ let physicsBody = SKPhysicsBody(rectangleOf: lowestShape.frame.size)
128
+
129
+ // 落下しないよう固定
130
+
131
+ physicsBody.isDynamic = false
132
+
133
+ // 衝突を検知する対象のビットマスクを指定
134
+
135
+ // 名古屋名物との衝突を検知する
136
+
137
+ physicsBody.contactTestBitMask = 0x1 << 1
138
+
139
+ lowestShape.physicsBody = physicsBody
140
+
141
+ self.addChild(lowestShape)
142
+
143
+ self.lowestShape = lowestShape
144
+
145
+
146
+
147
+ // どんぶりを追加
148
+
149
+ let bowlTexture = SKTexture(imageNamed: "bowl")
150
+
151
+ let bowl = SKSpriteNode(texture: bowlTexture)
152
+
153
+ bowl.position = CGPoint(x: self.size.width*0.5, y: 100)
154
+
155
+ bowl.size = CGSize(width: bowlTexture.size().width*0.5, height: bowlTexture.size().height*0.5)
156
+
157
+ // テクスチャの不透過部分の形状で物理シミュレーションを行う
158
+
159
+ bowl.physicsBody = SKPhysicsBody(texture: bowlTexture, size: bowl.size)
160
+
161
+ // 落下しないよう固定
162
+
163
+ bowl.physicsBody?.isDynamic = false
164
+
165
+ self.bowl = bowl
166
+
167
+ self.addChild(bowl)
168
+
169
+
170
+
171
+ // スコア用ラベル
172
+
173
+ var scoreLabel = SKLabelNode(fontNamed: "Helvetica")
174
+
175
+ scoreLabel.position = CGPoint(x: self.size.width*0.92, y: self.size.height*0.78)
176
+
177
+ scoreLabel.text = "¥0"
178
+
179
+ scoreLabel.fontSize = 32
180
+
181
+ scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.right // 右寄せ
182
+
183
+ scoreLabel.fontColor = UIColor.green
184
+
185
+ self.addChild(scoreLabel)
186
+
187
+ self.scoreLabel = scoreLabel
188
+
189
+
190
+
191
+ self.fallNagoyaSpecialty()
192
+
193
+
194
+
195
+ // タイマーを生成
196
+
197
+ self.timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(GameScene.fallNagoyaSpecialty), userInfo: nil, repeats: true)
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+ // 名古屋名物を落下させる
206
+
207
+ @objc func fallNagoyaSpecialty() {
208
+
209
+ // 0〜6のランダムな整数を発生させる
210
+
211
+ let index = Int(arc4random_uniform(7))
212
+
213
+ // 選択された番号のテクスチャを読み込む
214
+
215
+ let texture = SKTexture(imageNamed: "(index)")
216
+
217
+ // テクスチャからスプライトを生成する
218
+
219
+ let Nagoyasprite = SKSpriteNode(texture: texture)
220
+
221
+ Nagoyasprite.position = CGPoint(x: self.size.width*0.5, y: self.size.height)
222
+
223
+ Nagoyasprite.size = CGSize(width: texture.size().width*0.5, height: texture.size().height*0.5)
224
+
225
+
226
+
227
+ // テクスチャからPhysicsBodyを追加
228
+
229
+ Nagoyasprite.physicsBody = SKPhysicsBody(texture: texture, size: Nagoyasprite.size)
230
+
231
+
232
+
233
+ Nagoyasprite.physicsBody?.contactTestBitMask = 0x1 << 1 // 衝突を検知する対象のビットマスクを指定
234
+
235
+ self.addChild(Nagoyasprite)
236
+
237
+ // 落下した物に応じてスコアを加算する
238
+
239
+ self.score += self.scoreList[index]
240
+
241
+ // 金額のラベルを更新
242
+
243
+ self.scoreLabel?.text = "¥(self.score)"
244
+
245
+
246
+
247
+ }
248
+
57
- // 衝突が発生したときに呼ばれるメソッド
249
+ // 衝突が発生したときに呼ばれるメソッド
58
250
 
59
251
  func didBegin(_ contact: SKPhysicsContact) {
60
252
 
@@ -66,11 +258,11 @@
66
258
 
67
259
  // ゲームオーバースプライトを表示
68
260
 
69
- let sprite = SKSpriteNode(imageNamed: "gameover")
261
+ let GameOversprite = SKSpriteNode(imageNamed: "gameover")
70
-
262
+
71
- sprite.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.5)
263
+ GameOversprite.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.5)
72
-
264
+
73
- self.addChild(sprite)
265
+ self.addChild(GameOversprite)
74
266
 
75
267
 
76
268
 
@@ -82,18 +274,20 @@
82
274
 
83
275
  self.timer?.invalidate()
84
276
 
85
- //self.isPaused = false
86
-
87
- //sprite.removeAllChildren()
88
-
89
277
  }
90
278
 
91
279
  }
92
280
 
281
+
282
+
93
283
  // タッチ開始時に呼ばれるメソッド
94
284
 
95
285
  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
96
286
 
287
+
288
+
289
+
290
+
97
291
  for touch: AnyObject in touches {
98
292
 
99
293
  // シーン上のタッチされた位置を取得する
@@ -112,6 +306,28 @@
112
306
 
113
307
  }
114
308
 
309
+
310
+
311
+ // ドラック時
312
+
313
+ override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
314
+
315
+ for touch: AnyObject in touches {
316
+
317
+ let location = touch.location(in: self)
318
+
319
+ let action = SKAction.move(to: location, duration: 0.2)
320
+
321
+ self.bowl?.run(action)
322
+
323
+ }
324
+
325
+ }
326
+
327
+ }
328
+
329
+ ```
330
+
115
331
 
116
332
 
117
333
  ### 試したこと