0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

androidでkotlinの入門 リバーシー2

Posted at

前回はリバーシー(≒オセロ)のゲーム盤の表示
要はグラフィックの表示の仕方

今回は、画面へのタッチを検出して座標からどこが押されたかを判断

ゲームの処理は reversiGame で行うようにして、
ゲーム盤クラス(BOARD_DISP)を使って表示していました

reversiGame.kt
class reversiGame : AppCompatActivity() {
    internal lateinit var boardBan :BOARD_DISP // ゲーム盤

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_reversi_game)

        // ボードを追加(表示)
        boardBan = BOARD_DISP(this, gameStaus, boardSelect)
        ban.addView(boardBan)

タッチは onTouchEvent() で検出できるので、reversiGame で検出しようとしたのですが
意図したように、ボード上のマス座標が取れませんでいた

そこで、最初 BOARD_DISPでonTouchEvent()を使い、マス座標を求め

BOARD_DISD.kt
    // ボード上でのタッチ検出
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.getAction()) {
            MotionEvent.ACTION_DOWN -> {
                // タッチされた、マスの座標を求め
                val x = ((event.getX() - boardLeft) / masuSize).toInt()
                masuSelect.x = if (x < 9) {
                    val xmin = boardLeft + (masuSize * x) + fukantai
                    val xmax = boardLeft + (masuSize * (x + 1)) - fukantai
                    if (event.getX() > xmin && event.getX() < xmax) { x  } else { -1 }
                } else { -1 }
                val y = (event.getY() / masuSize).toInt()
                masuSelect.y = if (y < 9) {
                    val ymin = boardTop + (masuSize * y) + fukantai
                    val ymax = boardTop + (masuSize * (y + 1)) - fukantai
                    if (event.getY() > ymin && event.getY() < ymax) { y } else { -1 }
                } else { -1 }

                // 盤上をタッチされたかを判定
                masuSelect.InSw = if (masuSelect.x != -1 && masuSelect.y != -1) {
                    // 有効な座標
                    SELECT_MASU.IN_STATUS.MASU_SELECT
                } else {
                    SELECT_MASU.IN_STATUS.NON
                }
                // 上位(reversiGame)でタッチを検出してもらうため
                // 有効時 false で返している
                if (SELECT_MASU.IN_STATUS.MASU_SELECT == masuSelect.InSw) return false
            }
        }
        return true
    }

その後、reversiGame の onTouchEvent()で押された時の処理を行うという
面倒なことしてます

reversiGame.kt
    // タッチされた時
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.getAction()) {
            MotionEvent.ACTION_DOWN -> {
                if (boardSelect.InSw == SELECT_MASU.IN_STATUS.MASU_SELECT) {
                    // BOARD_DISDで盤上かの判断はしている
                    gameActionCheck(boardSelect)
                    return true
                }
            }
        }
        return false
    }

他にやり方があるとは思いますが、一応動いてはいます。

コードはここに置いてあります

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?