简体   繁体   English

从Java转换Kotlin-IndexOutOfBoundsException

[英]Kotlin - IndexOutOfBoundsException when converting it from Java

Currently I'm trying to learn making games in Kotlin and I was converting the java code to Kotlin and everything is working except the below problems. 目前,我正在尝试在Kotlin中学习制作游戏,并且我正在将Java代码转换为Kotlin,并且除以下问题外,其他所有功能都可以正常工作。

//Missile.kt

class Missile(context: Context) {

    internal var x: Int = 0
    internal var y: Int = 0
    internal var mVelocity: Int = 0
    internal var missile: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.missile)

    val missileWidth: Int
        get() = missile.width
    val missileHeight: Int
        get() = missile.height

    init {
        x = GameView.dWidth / 2 - missileWidth / 2
        y = GameView.dHeight - GameView.tankHeight - missileHeight / 2
        mVelocity = 50
    }
}


//In Kotlin

for (i in missiles.indices) {

    if (missiles[i].y >- missiles[i].missileHeight) {

        missiles[i].y -= missiles[i].mVelocity

        canvas.drawBitmap(missiles[i].missile, missiles[i].x.toFloat(), missiles[i].y.toFloat(), null)

        if (missiles[i].x >= planes[0].planeX && missiles[i].x + missiles[i].missileWidth <= planes[0].planeX + planes[0].width && missiles[i].y >= planes[0].planeY &&

                missiles[i].y <= planes[0].planeY + planes[0].height) {

            val explosion = Explosion(context)

            explosion.explosionX = planes[0].planeX + planes[0].width / 2 - explosion.explosionWidth / 2

            explosion.explosionY = planes[0].planeY + planes[0].height / 2 - explosion.explosionHeight / 2

            explosions.add(explosion)

            planes[0].resetPosition()

            count++

            missiles.removeAt(i)

            if (point != 0) {

                sp.play(point, 1f, 1f, 0, 0, 1f)

            }



//In Java

for (int i = 0; i < missiles.size(); i++) {

    if (missiles.get(i).y > -missiles.get(i).getMissileHeight()) {

        missiles.get(i).y -= missiles.get(i).mVelocity;

        canvas.drawBitmap(missiles.get(i).missile, missiles.get(i).x, missiles.get(i).y, null);

        if (missiles.get(i).x >= planes.get(0).planeX && (missiles.get(i).x + missiles.get(i).getMissileWidth())

                <= (planes.get(0).planeX + planes.get(0).getWidth()) && missiles.get(i).y >= planes.get(0).planeY &&

                missiles.get(i).y <= (planes.get(0).planeY + planes.get(0).getHeight())) {

            Explosion explosion = new Explosion(context);

            explosion.explosionX = planes.get(0).planeX + planes.get(0).getWidth()/2 - explosion.getExplosionWidth()/2;

            explosion.explosionY = planes.get(0).planeY + planes.get(0).getHeight()/2 - explosion.getExplosionHeight()/2;

            explosions.add(explosion);

            planes.get(0).resetPosition();

            count++;

            missiles.remove(i);

            if(point != 0){

                sp.play(point, 1, 1, 0, 0, 1);

            }

The part where causing is below. 引起原因的部分如下。

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

java.lang.IndexOutOfBoundsException: Index: 2, Size: 2



if (missiles[i].y >- missiles[i].missileHeight) {

So, the situation is if I press the cannon two times, it will cause the java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 因此,情况是如果我按两次加农炮,将导致java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

and if I press the cannon three times, it will cause the java.lang.IndexOutOfBoundsException: Index: 2, Size: 2. 如果我按三门大炮,将导致java.lang.IndexOutOfBoundsException: Index: 2, Size: 2.

If I only press the cannon one time, there will be no crash. 如果我只按一次大炮,就不会崩溃。

I would love to get possible suggestion about how to fix the problems. 我很想获得有关如何解决问题的建议。

I believe it's because you are removing elements causing your index to be incorrect. 我认为这是因为要删除导致索引不正确的元素。 If you are removing elements from a list, you should use Iterator to iterate the list. 如果要从列表中删除元素,则应使用Iterator来迭代列表。

Iterator itr = missiles.iterator();
while(itr.hasNext()){
    Object missile = itr.next(); 
    itr.remove();
}

the remove will also remove it from your missiles list. 删除也会将其从您的导弹列表中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM