前提・実現したいこと
android studioでkotlinの勉強中です。
現在、避けるゲームを作っていますが、1つわからないことがあり、kenを上から下に動かし、下まで行ったらまた上に戻り毎回違う位置から出てくるようにランダムな値を生成するといった文を作っていますが、現状、kenが上から下までいったきりで戻ってこなく、kenのY座標が0より小さくなったら画面の上に戻すということをしたいのですが、どういった文にすればいいのかわからず困っています。。
なにとぞ初心者でサイトにも慣れていませんがどなたかお知恵をいただければと思います。よろしくお願いします
MainActivity
1package com.example.ninja 2 3import android.content.Intent 4import android.graphics.Point 5import android.os.Bundle 6import android.os.Handler 7import android.support.v7.app.AppCompatActivity 8import android.view.MotionEvent 9import android.view.View 10import android.widget.FrameLayout 11import android.widget.ImageView 12import android.widget.TextView 13import com.example.ninja.R 14import com.example.ninja.ResultActivity 15import com.example.ninja.SoundPlayer 16import kotlinx.android.synthetic.main.activity_main.* 17import java.util.* 18 19class MainActivity : AppCompatActivity() { 20 21 // サイズ 22 private var frameside = 0 23 private var hitoSize = 0 24 private var screenWidth = 0 25 private var screenHeight = 0 26 // 位置 27 private var hitoX = 0f 28 private var kenX : Float = 0f 29 private var kenY : Float = 0f 30 31 // スピード 32 private var hitoSpeed = 0 33 private var kenSpeed = 0 34 35 // Score 36 private var score = 0 37 // Handler & Timer 38 private val handler = Handler() 39 private var timer: Timer? = Timer() 40 // Status 41 private var action_flg = false 42 private var start_flg = false 43 // Sound 44 private lateinit var soundPlayer: SoundPlayer 45 46 override fun onCreate(savedInstanceState: Bundle?) { 47 super.onCreate(savedInstanceState) 48 setContentView(R.layout.activity_main) 49 soundPlayer = SoundPlayer(this) 50 scoreLabel.text = "scoreLavel" 51 startLabel.text = "startLabel" 52 53 // Screen Size 54 val wm = windowManager 55 val display = wm.defaultDisplay 56 val size = Point() 57 display.getSize(size) 58 59 screenWidth = size.x 60 screenHeight = size.y 61 62 hitoSpeed = Math.round(screenWidth / 60f) 63 kenSpeed = Math.round(screenHeight / 60f) 64 65 ken.x = -100.0f 66 ken.y = -100.0f 67 68 scoreLabel.text = "Score : 0f" 69 } 70 71 fun changePos() { 72 hitCheck() 73 74 // ken 75 kenY += kenSpeed.toFloat() 76 if (kenY < 0) { 77 kenY = (screenHeight + 20).toFloat(); 78 kenX = 79 Math.floor(Math.random() * ( frameside-ken.width)).toFloat() 80 } 81 ken.x = kenX 82 ken.y = kenY 83 84 85 // hito 86 if (action_flg) { 87 hitoX -= hitoSpeed.toFloat() 88 } else { 89 hitoX += hitoSpeed.toFloat() 90 } 91 if (hitoX < 0) hitoX = 0f 92 if (hitoX > frameside - hitoSize) hitoX = frameside - hitoSize.toFloat() 93 hito.x = hitoX 94 scoreLabel!!.text = "Score : $score" 95 } 96 97 private fun hitCheck() { // Orange 98 val kenCenterX = kenX + ken!!.width / 2 99 val kenCenterY = kenY + ken!!.height / 2 100 if (hitStatus(kenCenterX, kenCenterY)) { 101 kenX = -10.0f 102 score += 10 103 soundPlayer.playHitSound() 104 } 105 106 } 107 108 private fun hitStatus(centerX: Float, centerY: Float): Boolean { 109 return if (0 <= centerX && centerX <= hitoSize && hitoX <= centerY && centerY <= hitoX + hitoSize 110 ) true else false 111 } 112 113 override fun onTouchEvent(event: MotionEvent): Boolean { 114 if (!start_flg) { 115 start_flg = true 116 val frame: FrameLayout = findViewById(R.id.frame); 117 frameside = frame.width; 118 119 hitoX = hito.x; 120 hitoSize = hito.height; 121 122 123 124 startLabel.visibility = View.GONE; 125 timer!!.schedule(object : TimerTask() { 126 override fun run() { 127 handler.post { changePos() } 128 } 129 }, 0, 20) 130 } else { 131 if (event.action == MotionEvent.ACTION_DOWN) { 132 action_flg = true 133 } else if (event.action == MotionEvent.ACTION_UP) { 134 action_flg = false 135 } 136 } 137 return true 138 } 139 140 override fun onBackPressed() {} 141}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:id="@+id/scoreLabel" android:layout_width="match_parent" android:layout_height="50dp" android:text="Score : 300" android:textSize="18sp" android:paddingLeft="10dp" android:gravity="center_vertical"/> <FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/startLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="タップしてスタート" android:textSize="20sp" android:layout_gravity="center"/> <ImageView android:id="@+id/hito" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_vertical" android:src="@drawable/hito" /> <ImageView android:id="@+id/ken" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/ken"/> </FrameLayout> </LinearLayout>
試したこと
参考書、他サイト情報
回答1件
あなたの回答
tips
プレビュー