В Android Studio на языке Kotlin, не передвигается картинка танка на телефоне через onKeyDown с помощью KEYCODE_DPAD_ ! Проект компилируется но танк не двигается.
вот код:
package com.yura.tanktutorial
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.view.KeyEvent.*
import android.widget.FrameLayout
import com.yura.tanktutorial.Direction.*
import com.yura.tanktutorial.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view : FrameLayout = binding.root
setContentView(view)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when(keyCode) {
KEYCODE_DPAD_UP -> move(UP)
KEYCODE_DPAD_DOWN -> move(BOTTOM)
KEYCODE_DPAD_LEFT -> move(LEFT)
KEYCODE_DPAD_RIGHT -> move(RIGHT)
}
return super.onKeyDown(keyCode, event)
}
private fun move (direction: Direction){
when(direction) {
UP -> {
binding.myTank.rotation = 0f
(binding.myTank.layoutParams as FrameLayout.LayoutParams).topMargin += -50
}
BOTTOM -> {
binding.myTank.rotation = 180f
(binding.myTank.layoutParams as FrameLayout.LayoutParams).topMargin += 50
}
LEFT -> {
binding.myTank.rotation = 90f
(binding.myTank.layoutParams as FrameLayout.LayoutParams).topMargin += 50
}
RIGHT -> {
binding.myTank.rotation = 270f
(binding.myTank.layoutParams as FrameLayout.LayoutParams).topMargin += -50
}
}
binding.container.removeView(binding.myTank)
binding.container.addView(binding.myTank)
}
}