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

質問編集履歴

2

mainactivity更新

2020/03/14 15:15

投稿

mijinnnko
mijinnnko

スコア6

title CHANGED
File without changes
body CHANGED
@@ -155,7 +155,8 @@
155
155
  }
156
156
 
157
157
  override fun onBackPressed() {}
158
+ }
158
- }```
159
+ ```
159
160
 
160
161
  activity_main.xml
161
162
  ```

1

mainactivity更新

2020/03/14 15:15

投稿

mijinnnko
mijinnnko

スコア6

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,7 @@
4
4
  現在、避けるゲームを作っていますが、最初左上から落下し、それからランダムな位置で出てくるようになってるのですが、それを最初からランダムな位置からスタートするようにしたいのですが、どうしても左上からになってしまい困ってます。お知恵をお願いします。
5
5
 
6
6
  ```MainActivity
7
- package com.example.ninja
7
+ package com.example.myapplication
8
8
 
9
9
  import android.content.Intent
10
10
  import android.graphics.Point
@@ -16,39 +16,44 @@
16
16
  import android.widget.FrameLayout
17
17
  import android.widget.ImageView
18
18
  import android.widget.TextView
19
- import com.example.ninja.R
20
- import com.example.ninja.ResultActivity
21
- import com.example.ninja.SoundPlayer
22
19
  import kotlinx.android.synthetic.main.activity_main.*
23
20
  import java.util.*
24
21
 
25
22
  class MainActivity : AppCompatActivity() {
26
23
 
24
+
27
25
  // サイズ
26
+ private var frameHeight = 0
28
27
  private var frameside = 0
29
28
  private var hitoSize = 0
30
29
  private var screenWidth = 0
31
30
  private var screenHeight = 0
31
+
32
32
  // 位置
33
33
  private var hitoX = 0f
34
+ private var hitoY = 0f
34
- private var kenX : Float = 0f
35
+ private var kenX = 0f
35
- private var kenY : Float = 0f
36
+ private var kenY = 0f
36
-
37
+
38
+
37
39
  // スピード
38
40
  private var hitoSpeed = 0
39
41
  private var kenSpeed = 0
40
-
42
+
43
+
41
44
  // Score
42
45
  private var score = 0
46
+
43
47
  // Handler & Timer
44
48
  private val handler = Handler()
45
49
  private var timer: Timer? = Timer()
50
+
46
51
  // Status
47
52
  private var action_flg = false
48
53
  private var start_flg = false
54
+
49
55
  // Sound
50
- private lateinit var soundPlayer: SoundPlayer
56
+ private var soundPlayer: SoundPlayer? = null
51
-
52
57
  override fun onCreate(savedInstanceState: Bundle?) {
53
58
  super.onCreate(savedInstanceState)
54
59
  setContentView(R.layout.activity_main)
@@ -56,6 +61,7 @@
56
61
  scoreLabel.text = "scoreLavel"
57
62
  startLabel.text = "startLabel"
58
63
 
64
+
59
65
  // Screen Size
60
66
  val wm = windowManager
61
67
  val display = wm.defaultDisplay
@@ -67,11 +73,13 @@
67
73
 
68
74
  hitoSpeed = Math.round(screenWidth / 60f)
69
75
  kenSpeed = Math.round(screenHeight / 60f)
70
-
76
+
77
+
71
78
  ken.x = -100.0f
72
79
  ken.y = -100.0f
73
-
80
+
74
- scoreLabel.text = "Score : 0f"
81
+ scoreLabel.setText("Score : 0")
82
+
75
83
  }
76
84
 
77
85
  fun changePos() {
@@ -79,15 +87,13 @@
79
87
 
80
88
  // ken
81
89
  kenY += kenSpeed.toFloat()
82
- if (kenY > screenHeight ) {
90
+ if (kenY > screenHeight) {
83
91
  kenY = -100f
84
- kenX =
85
- (Math.random() * ( frameHeight-ken!!.width)).toFloat()
92
+ kenX = (Math.random() * (screenHeight - ken!!.width)).toFloat()
86
93
  }
87
94
  ken!!.x = kenX
88
95
  ken!!.y = kenY
89
96
 
90
-
91
97
  // hito
92
98
  if (action_flg) {
93
99
  hitoX -= hitoSpeed.toFloat()
@@ -95,39 +101,44 @@
95
101
  hitoX += hitoSpeed.toFloat()
96
102
  }
97
103
  if (hitoX < 0) hitoX = 0f
98
- if (hitoX > frameside - hitoSize) hitoX = frameside - hitoSize.toFloat()
104
+ if (hitoX > frameHeight - hitoSize) hitoX = frameHeight - hitoSize.toFloat()
99
105
  hito.x = hitoX
100
106
  scoreLabel!!.text = "Score : $score"
101
107
  }
102
108
 
103
- private fun hitCheck() { // Orange
109
+ fun hitCheck() {
110
+
111
+ // ken
104
- val kenCenterX = kenX + ken!!.width / 2
112
+ val kenCenterX = kenX + ken.width / 2
105
- val kenCenterY = kenY + ken!!.height / 2
113
+ val kenCenterY = kenY + ken.height / 2
106
114
  if (hitStatus(kenCenterX, kenCenterY)) {
107
- kenX = -10.0f
108
- score += 10
109
- soundPlayer.playHitSound()
115
+ soundPlayer!!.playHitSound()
116
+ if (timer != null) {
117
+ timer!!.cancel()
118
+ timer = null
119
+ }
120
+ // 結果画面へ
121
+ val intent = Intent(applicationContext, ResultActivity::class.java)
122
+ intent.putExtra("SCORE", score)
123
+ startActivity(intent)
124
+
110
125
  }
111
-
112
- }
113
126
 
127
+ }
128
+
114
- private fun hitStatus(centerX: Float, centerY: Float): Boolean {
129
+ fun hitStatus(centerX: Float, centerY: Float): Boolean {
115
- return if (0 <= centerX && centerX <= hitoSize && hitoX <= centerY && centerY <= hitoX + hitoSize
130
+ return if (screenHeight <= centerY && centerY <= hitoSize &&
116
- ) true else false
131
+ hitoX <= centerX && centerX <= hitoX + hitoSize) true else false
117
132
  }
118
133
 
119
134
  override fun onTouchEvent(event: MotionEvent): Boolean {
120
135
  if (!start_flg) {
121
136
  start_flg = true
122
- val frame: FrameLayout = findViewById(R.id.frame);
137
+ val frame = findViewById<FrameLayout>(R.id.frame)
123
- frameside = frame.width;
138
+ frameHeight = frame.width
124
-
125
- hitoX = hito.x;
139
+ hitoX = hito!!.x
126
- hitoSize = hito.height;
140
+ hitoSize = hito!!.height
127
-
128
-
129
-
130
- startLabel.visibility = View.GONE;
141
+ startLabel!!.visibility = View.GONE
131
142
  timer!!.schedule(object : TimerTask() {
132
143
  override fun run() {
133
144
  handler.post { changePos() }
@@ -144,8 +155,7 @@
144
155
  }
145
156
 
146
157
  override fun onBackPressed() {}
147
- }
148
- ```
158
+ }```
149
159
 
150
160
  activity_main.xml
151
161
  ```