Заставить массив машин передвигаться налево по сетке после задержки - Android - PullRequest
0 голосов
/ 21 февраля 2020

Я пытаюсь заставить автомобили на сетке двигаться с задержкой, используя массив из i-х и j-х позиций. То, что я делаю здесь, - это циклическое прохождение всех ящиков, поиск ящиков с автомобилями, использующими массивы, перемещение их влево (или назад в начало, если на краю экрана) и обновление позиций. Пока все работает, пока я не добавлю задержку, когда он падает.

        var pcarsloci = arrayOf(2,2,2,8,8,8,11,11)
        var pcarslocj = arrayOf(0,3,5,0,3,5,2,6)
        k = 0 //let k array position to be 0
                    for (i in 0 until 15) { //loop 15 times for each row
                        val row = table.getChildAt(i) as TableRow //get current row
                        for (j in 0 until 7) { //loops through all boxes in row
                            val box = row.getChildAt(j) as ImageView //get current box
                            if (i == pcarsloci[k] && j == pcarslocj[k]) { //if k index of pcars is equal to i or j
                                GlobalScope.launch {
                                    //make box move to the right
                                    val newRow = table.getChildAt(i) as TableRow //new row is same row
                                    val newBox = newRow.getChildAt(j) as ImageView //seet new box to same box
                                    if(pcarslocj[k] == 0){ //if location is at 0
                                        val newBox = newRow.getChildAt(6) as ImageView
                                        val image =
                                            ContextCompat.getDrawable(
                                                newBox.context,
                                                R.drawable.carb
                                            )//new box is 0
                                        newBox.foreground = image
                                        box.foreground = null
                                        newRow.background = row.background
                                        pcarslocj[k] = 6 //set location of k to be 6

                                    }else{
                                        val newBox = newRow.getChildAt(j - 1) as ImageView
                                        val image =
                                            ContextCompat.getDrawable(
                                                newBox.context,
                                                R.drawable.carb
                                            )
                                        newBox.foreground = image
                                        box.foreground = null
                                        newRow.background = row.background
                                        pcarslocj[k]-- //set location of array at k to be 1 less

                                    }

                                    if (k < 7) {
                                        k++
                                    }
                                    delay(1000L)

                                }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...