質問編集履歴

2

mainactivity更新

2020/03/14 15:15

投稿

mijinnnko
mijinnnko

スコア6

test CHANGED
File without changes
test CHANGED
@@ -312,7 +312,9 @@
312
312
 
313
313
  override fun onBackPressed() {}
314
314
 
315
+ }
316
+
315
- }```
317
+ ```
316
318
 
317
319
 
318
320
 

1

mainactivity更新

2020/03/14 15:15

投稿

mijinnnko
mijinnnko

スコア6

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ```MainActivity
12
12
 
13
- package com.example.ninja
13
+ package com.example.myapplication
14
14
 
15
15
 
16
16
 
@@ -34,12 +34,6 @@
34
34
 
35
35
  import android.widget.TextView
36
36
 
37
- import com.example.ninja.R
38
-
39
- import com.example.ninja.ResultActivity
40
-
41
- import com.example.ninja.SoundPlayer
42
-
43
37
  import kotlinx.android.synthetic.main.activity_main.*
44
38
 
45
39
  import java.util.*
@@ -50,8 +44,12 @@
50
44
 
51
45
 
52
46
 
47
+
48
+
53
49
  // サイズ
54
50
 
51
+ private var frameHeight = 0
52
+
55
53
  private var frameside = 0
56
54
 
57
55
  private var hitoSize = 0
@@ -60,15 +58,21 @@
60
58
 
61
59
  private var screenHeight = 0
62
60
 
61
+
62
+
63
63
  // 位置
64
64
 
65
65
  private var hitoX = 0f
66
66
 
67
+ private var hitoY = 0f
68
+
67
- private var kenX : Float = 0f
69
+ private var kenX = 0f
68
-
70
+
69
- private var kenY : Float = 0f
71
+ private var kenY = 0f
70
-
71
-
72
+
73
+
74
+
75
+
72
76
 
73
77
  // スピード
74
78
 
@@ -76,75 +80,87 @@
76
80
 
77
81
  private var kenSpeed = 0
78
82
 
83
+
84
+
85
+
86
+
87
+ // Score
88
+
89
+ private var score = 0
90
+
91
+
92
+
93
+ // Handler & Timer
94
+
95
+ private val handler = Handler()
96
+
97
+ private var timer: Timer? = Timer()
98
+
99
+
100
+
101
+ // Status
102
+
103
+ private var action_flg = false
104
+
105
+ private var start_flg = false
106
+
107
+
108
+
109
+ // Sound
110
+
111
+ private var soundPlayer: SoundPlayer? = null
112
+
113
+ override fun onCreate(savedInstanceState: Bundle?) {
114
+
115
+ super.onCreate(savedInstanceState)
116
+
117
+ setContentView(R.layout.activity_main)
118
+
119
+ soundPlayer = SoundPlayer(this)
120
+
121
+ scoreLabel.text = "scoreLavel"
122
+
123
+ startLabel.text = "startLabel"
124
+
125
+
126
+
127
+
128
+
129
+ // Screen Size
130
+
131
+ val wm = windowManager
132
+
133
+ val display = wm.defaultDisplay
134
+
135
+ val size = Point()
136
+
137
+ display.getSize(size)
138
+
139
+
140
+
141
+ screenWidth = size.x
142
+
143
+ screenHeight = size.y
144
+
145
+
146
+
147
+ hitoSpeed = Math.round(screenWidth / 60f)
148
+
149
+ kenSpeed = Math.round(screenHeight / 60f)
150
+
79
151
 
80
152
 
81
- // Score
153
+
82
-
83
- private var score = 0
84
-
85
- // Handler & Timer
86
-
87
- private val handler = Handler()
88
-
89
- private var timer: Timer? = Timer()
90
-
91
- // Status
92
-
93
- private var action_flg = false
94
-
95
- private var start_flg = false
96
-
97
- // Sound
98
-
99
- private lateinit var soundPlayer: SoundPlayer
100
-
101
-
102
-
103
- override fun onCreate(savedInstanceState: Bundle?) {
104
-
105
- super.onCreate(savedInstanceState)
106
-
107
- setContentView(R.layout.activity_main)
108
-
109
- soundPlayer = SoundPlayer(this)
110
-
111
- scoreLabel.text = "scoreLavel"
112
-
113
- startLabel.text = "startLabel"
114
-
115
-
116
-
117
- // Screen Size
118
-
119
- val wm = windowManager
120
-
121
- val display = wm.defaultDisplay
122
-
123
- val size = Point()
124
-
125
- display.getSize(size)
126
-
127
-
128
-
129
- screenWidth = size.x
130
-
131
- screenHeight = size.y
132
-
133
-
134
-
135
- hitoSpeed = Math.round(screenWidth / 60f)
136
-
137
- kenSpeed = Math.round(screenHeight / 60f)
138
-
139
-
140
154
 
141
155
  ken.x = -100.0f
142
156
 
143
157
  ken.y = -100.0f
144
158
 
145
-
146
-
159
+
160
+
147
- scoreLabel.text = "Score : 0f"
161
+ scoreLabel.setText("Score : 0")
162
+
163
+
148
164
 
149
165
  }
150
166
 
@@ -160,13 +176,11 @@
160
176
 
161
177
  kenY += kenSpeed.toFloat()
162
178
 
163
- if (kenY > screenHeight ) {
179
+ if (kenY > screenHeight) {
164
180
 
165
181
  kenY = -100f
166
182
 
167
- kenX =
168
-
169
- (Math.random() * ( frameHeight-ken!!.width)).toFloat()
183
+ kenX = (Math.random() * (screenHeight - ken!!.width)).toFloat()
170
184
 
171
185
  }
172
186
 
@@ -176,8 +190,6 @@
176
190
 
177
191
 
178
192
 
179
-
180
-
181
193
  // hito
182
194
 
183
195
  if (action_flg) {
@@ -192,7 +204,7 @@
192
204
 
193
205
  if (hitoX < 0) hitoX = 0f
194
206
 
195
- if (hitoX > frameside - hitoSize) hitoX = frameside - hitoSize.toFloat()
207
+ if (hitoX > frameHeight - hitoSize) hitoX = frameHeight - hitoSize.toFloat()
196
208
 
197
209
  hito.x = hitoX
198
210
 
@@ -202,204 +214,212 @@
202
214
 
203
215
 
204
216
 
205
- private fun hitCheck() { // Orange
217
+ fun hitCheck() {
218
+
219
+
220
+
206
-
221
+ // ken
222
+
207
- val kenCenterX = kenX + ken!!.width / 2
223
+ val kenCenterX = kenX + ken.width / 2
208
-
224
+
209
- val kenCenterY = kenY + ken!!.height / 2
225
+ val kenCenterY = kenY + ken.height / 2
210
226
 
211
227
  if (hitStatus(kenCenterX, kenCenterY)) {
212
228
 
213
- kenX = -10.0f
214
-
215
- score += 10
216
-
217
- soundPlayer.playHitSound()
229
+ soundPlayer!!.playHitSound()
230
+
231
+ if (timer != null) {
232
+
233
+ timer!!.cancel()
234
+
235
+ timer = null
236
+
237
+ }
238
+
239
+ // 結果画面へ
240
+
241
+ val intent = Intent(applicationContext, ResultActivity::class.java)
242
+
243
+ intent.putExtra("SCORE", score)
244
+
245
+ startActivity(intent)
246
+
247
+
218
248
 
219
249
  }
220
250
 
251
+
252
+
221
-
253
+ }
254
+
255
+
256
+
257
+ fun hitStatus(centerX: Float, centerY: Float): Boolean {
258
+
259
+ return if (screenHeight <= centerY && centerY <= hitoSize &&
260
+
261
+ hitoX <= centerX && centerX <= hitoX + hitoSize) true else false
222
262
 
223
263
  }
224
264
 
225
265
 
226
266
 
227
- private fun hitStatus(centerX: Float, centerY: Float): Boolean {
267
+ override fun onTouchEvent(event: MotionEvent): Boolean {
268
+
228
-
269
+ if (!start_flg) {
270
+
271
+ start_flg = true
272
+
229
- return if (0 <= centerX && centerX <= hitoSize && hitoX <= centerY && centerY <= hitoX + hitoSize
273
+ val frame = findViewById<FrameLayout>(R.id.frame)
274
+
230
-
275
+ frameHeight = frame.width
276
+
277
+ hitoX = hito!!.x
278
+
279
+ hitoSize = hito!!.height
280
+
281
+ startLabel!!.visibility = View.GONE
282
+
283
+ timer!!.schedule(object : TimerTask() {
284
+
285
+ override fun run() {
286
+
287
+ handler.post { changePos() }
288
+
289
+ }
290
+
291
+ }, 0, 20)
292
+
293
+ } else {
294
+
295
+ if (event.action == MotionEvent.ACTION_DOWN) {
296
+
297
+ action_flg = true
298
+
299
+ } else if (event.action == MotionEvent.ACTION_UP) {
300
+
231
- ) true else false
301
+ action_flg = false
302
+
303
+ }
304
+
305
+ }
306
+
307
+ return true
232
308
 
233
309
  }
234
310
 
235
311
 
236
312
 
237
- override fun onTouchEvent(event: MotionEvent): Boolean {
238
-
239
- if (!start_flg) {
240
-
241
- start_flg = true
242
-
243
- val frame: FrameLayout = findViewById(R.id.frame);
244
-
245
- frameside = frame.width;
246
-
247
-
248
-
249
- hitoX = hito.x;
250
-
251
- hitoSize = hito.height;
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
- startLabel.visibility = View.GONE;
260
-
261
- timer!!.schedule(object : TimerTask() {
262
-
263
- override fun run() {
264
-
265
- handler.post { changePos() }
266
-
267
- }
268
-
269
- }, 0, 20)
270
-
271
- } else {
272
-
273
- if (event.action == MotionEvent.ACTION_DOWN) {
274
-
275
- action_flg = true
276
-
277
- } else if (event.action == MotionEvent.ACTION_UP) {
278
-
279
- action_flg = false
280
-
281
- }
282
-
283
- }
284
-
285
- return true
286
-
287
- }
288
-
289
-
290
-
291
313
  override fun onBackPressed() {}
292
314
 
293
- }
315
+ }```
316
+
317
+
318
+
319
+ activity_main.xml
294
320
 
295
321
  ```
296
322
 
297
-
323
+ <?xml version="1.0" encoding="utf-8"?>
324
+
298
-
325
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
326
+
327
+ xmlns:app="http://schemas.android.com/apk/res-auto"
328
+
329
+ xmlns:tools="http://schemas.android.com/tools"
330
+
331
+ android:layout_width="match_parent"
332
+
333
+ android:layout_height="match_parent"
334
+
335
+ tools:context=".MainActivity"
336
+
337
+ android:orientation="vertical">
338
+
339
+
340
+
341
+
342
+
343
+ <TextView
344
+
345
+ android:id="@+id/scoreLabel"
346
+
347
+ android:layout_width="match_parent"
348
+
349
+ android:layout_height="50dp"
350
+
351
+ android:text="Score : 300"
352
+
353
+ android:textSize="18sp"
354
+
355
+ android:paddingLeft="10dp"
356
+
357
+ android:gravity="center_vertical"/>
358
+
359
+
360
+
361
+ <FrameLayout
362
+
363
+ android:id="@+id/frame"
364
+
365
+ android:layout_width="match_parent"
366
+
367
+ android:layout_height="match_parent">
368
+
369
+
370
+
371
+ <TextView
372
+
373
+ android:id="@+id/startLabel"
374
+
375
+ android:layout_width="wrap_content"
376
+
377
+ android:layout_height="wrap_content"
378
+
379
+ android:text="タップしてスタート"
380
+
381
+ android:textSize="20sp"
382
+
383
+ android:layout_gravity="center"/>
384
+
385
+
386
+
299
- activity_main.xml
387
+ <ImageView
388
+
389
+ android:id="@+id/hito"
390
+
391
+ android:layout_width="100dp"
392
+
393
+ android:layout_height="100dp"
394
+
395
+ android:layout_gravity="center_vertical"
396
+
397
+ android:src="@drawable/hito" />
398
+
399
+
400
+
401
+ <ImageView
402
+
403
+ android:id="@+id/ken"
404
+
405
+ android:layout_width="100dp"
406
+
407
+ android:layout_height="100dp"
408
+
409
+ android:src="@drawable/ken"/>
410
+
411
+
412
+
413
+
414
+
415
+ </FrameLayout>
416
+
417
+
418
+
419
+ </LinearLayout>
300
420
 
301
421
  ```
302
422
 
303
- <?xml version="1.0" encoding="utf-8"?>
304
-
305
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
306
-
307
- xmlns:app="http://schemas.android.com/apk/res-auto"
308
-
309
- xmlns:tools="http://schemas.android.com/tools"
310
-
311
- android:layout_width="match_parent"
312
-
313
- android:layout_height="match_parent"
314
-
315
- tools:context=".MainActivity"
316
-
317
- android:orientation="vertical">
318
-
319
-
320
-
321
-
322
-
323
- <TextView
324
-
325
- android:id="@+id/scoreLabel"
326
-
327
- android:layout_width="match_parent"
328
-
329
- android:layout_height="50dp"
330
-
331
- android:text="Score : 300"
332
-
333
- android:textSize="18sp"
334
-
335
- android:paddingLeft="10dp"
336
-
337
- android:gravity="center_vertical"/>
338
-
339
-
340
-
341
- <FrameLayout
342
-
343
- android:id="@+id/frame"
344
-
345
- android:layout_width="match_parent"
346
-
347
- android:layout_height="match_parent">
348
-
349
-
350
-
351
- <TextView
352
-
353
- android:id="@+id/startLabel"
354
-
355
- android:layout_width="wrap_content"
356
-
357
- android:layout_height="wrap_content"
358
-
359
- android:text="タップしてスタート"
360
-
361
- android:textSize="20sp"
362
-
363
- android:layout_gravity="center"/>
364
-
365
-
366
-
367
- <ImageView
368
-
369
- android:id="@+id/hito"
370
-
371
- android:layout_width="100dp"
372
-
373
- android:layout_height="100dp"
374
-
375
- android:layout_gravity="center_vertical"
376
-
377
- android:src="@drawable/hito" />
378
-
379
-
380
-
381
- <ImageView
382
-
383
- android:id="@+id/ken"
384
-
385
- android:layout_width="100dp"
386
-
387
- android:layout_height="100dp"
388
-
389
- android:src="@drawable/ken"/>
390
-
391
-
392
-
393
-
394
-
395
- </FrameLayout>
396
-
397
-
398
-
399
- </LinearLayout>
400
-
401
- ```
402
-
403
423
 
404
424
 
405
425
  ### 試したこと