【Kotlin】坦克大战5:让子弹飞

907 阅读2分钟

让子弹飞

子弹飞是一种能力,新建AutoMoveable接口

/**
 * 自动移动的能力
 */
interface AutoMoveable : View {
	//方向
    val currentDirection:Direction
    //速度
    val speed:Int
    fun autoMove()
}

让子弹实现这个接口

class Bullet(override val currentDirection: Direction, create: (width: Int, height: Int) -> Pair<Int, Int>) : AutoMoveable {
    override var x: Int = 0
    override var y: Int = 0

    override val speed: Int = 8
    ......
    override fun autoMove() {
        //根据自己的方向,来改变x和y
        when(currentDirection){
            Direction.up->y-= speed //y轴会逐渐减小
            Direction.down -> y+=speed
            Direction.left -> x-=speed
            Direction.right -> x+=speed
        }
    }

}

写业务逻辑

class GameWindow :
    ......
    override fun onRefresh() {
        ......
        //检测自动移动能力的物体,让ta们自己动起来
        views.filter { it is AutoMoveable }.forEach{
            (it as AutoMoveable).autoMove()
        }
    }
}

运行看一下

11.gif

界面中元素的回收

GameWindow中的onDisplay方法不断绘制界面中的元素,我们可以在这里打印元素个数

22.png

页面中有61个元素,当我们发射一颗子弹时,变成了62个,每次发射子弹,元素个数都会增加。发射子弹越多,页面的负担也会越来越大。其实当子弹飞出界面时,应该销毁它

新建Destroyable

/**
 * 销毁的能力
 */
interface Destroyable : View {
    //判断是否销毁了
    fun isDestroyed():Boolean
}

GameWindow业务逻辑中,找到具备销毁能力的元素,移除

    override fun onRefresh() {
        ......
        //检测销毁
        views.filter { it is Destroyable }.forEach {
            //判断具备销毁能力的物体是否符合销毁条件
            if((it as Destroyable).isDestroyed()){
                views.remove(it)
            }
        }
    }

让子弹实现这个接口

class Bullet(override val currentDirection: Direction, create: (width: Int, height: Int) -> Pair<Int, Int>) : AutoMoveable,Destroyable {
    ......
    override fun isDestroyed(): Boolean {
         //子弹脱离屏幕后需要被销毁
        if(x<-width) return true
        if(x>Config.gameWidth) return true
        if(y<-height) return true
        if(y>Config.gameHeight) return true
        return false
    }
}

当子弹向左发射时,x小于0-子弹的宽度时需要被销毁

33.png

当子弹向右发射时,x>屏幕宽度,可以被销毁 当子弹向上发射时,y<0-子弹高度,可以被销毁 当子弹向下发射时,y>屏幕高度,可以被销毁

运行程序,开始是61个元素,打一颗子弹,增加一个元素。当子弹飞离屏幕时,元素减少

你可能会注意到,运行时,会报一些错误

在这里插入图片描述

这是当前线程的异常。用集合管理元素,这些元素有的是在耗时操作里做的,耗时操作时,是跨线程的,也就是在子线程中做,如果在跨线程操作时,对集合做增删操作,就会报这个异常,解决这个问题,我们需要一个线程安全的集合,修改GameWindow中以下代码即可

    //管理元素的集合
    //private val views = arrayListOf<View>()
    //线程安全的集合
    private val views = CopyOnWriteArrayList<View>()