Java 清空后重新填充数组列表

Java 清空后重新填充数组列表,java,android,arraylist,Java,Android,Arraylist,我现在做的是一个游戏,用户按下一个精灵,当按下它时,它会增加他们的分数,然后使它消失。我遇到的问题是,当精灵被完全从阵列列表中移除时,我不知道如何在按下精灵后使其重新出现,我是将其读取到阵列列表中,还是以其他方式执行 这是精灵代码: package cct.mad.lab; import java.util.Random; import android.content.Context; import android.graphics.Bitmap; import android.graphic

我现在做的是一个游戏,用户按下一个精灵,当按下它时,它会增加他们的分数,然后使它消失。我遇到的问题是,当精灵被完全从阵列列表中移除时,我不知道如何在按下精灵后使其重新出现,我是将其读取到阵列列表中,还是以其他方式执行

这是精灵代码:

package cct.mad.lab;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;  
import android.os.Vibrator;
public class Sprite {

    //x,y position of sprite - initial position (0,50)
    private GameView gameView;
    private Bitmap spritebmp;
    //Width and Height of the Sprite image
    private int bmp_width;
    private int bmp_height;
    // Needed for new random coordinates.
    private Random random = new Random();
    private int x = random.nextInt(200)-1; 
    private int y = random.nextInt(200)-1;
    int xSpeed = (random.nextInt(30)-15);
    int ySpeed = (random.nextInt(30)-15);

    public Sprite(GameView gameView) {
        this.gameView=gameView;
        spritebmp = BitmapFactory.decodeResource(gameView.getResources(),
          R.drawable.spritehead);
        this.bmp_width = spritebmp.getWidth();
        this.bmp_height= spritebmp.getHeight();
        //random y coordinate for sprite spawn
        x = gameView.getWidth();
        x = random.nextInt(x);
        y = gameView.getHeight();
        y = random.nextInt(y);
    }

    //update the position of the sprite
    public void update() {
        x = x + xSpeed;
        y = y + ySpeed;
        wrapAround(); //Adjust motion of sprite.
    }

    public void draw(Canvas canvas) {
        //Draw sprite image
        canvas.drawBitmap(spritebmp, x , y, null);
    }

    //y -= gameView.getHeight();//Reset y
    public void wrapAround(){
        //Code to wrap around   
        //increment x whilst not off screen
        if (x >= (gameView.getWidth() - 40)){ //if gone off the right sides of screen
            xSpeed = (xSpeed * -1);
        }
        if (x <= -10)
        {
            xSpeed = (xSpeed * -1);
        }

        if (y >= (gameView.getHeight() - 40)){//if gone off the bottom of screen
            ySpeed = (ySpeed * -1);
        }

        if (y <= 0)//if gone off the top of the screen
        {
            ySpeed = (ySpeed * -1);
        }
        xSpeed = (xSpeed * -1);
    }


    /* Checks if the Sprite was touched. */
    public boolean wasItTouched(float ex, float ey) {
        boolean touched = false; 
        if ((x <= ex) && (ex < x + bmp_width) &&
                (y <= ey) && (ey < y + bmp_height)) {
            touched = true;
        }
        return touched;
    }//End of wasItTouched 
}  
这是我实际显示项目和数组列表的代码:

public void surfaceCreated(SurfaceHolder holder) {
    // We can now safely setup the game start the game loop.
    ResetGame();//Set up a new game up - could be called by a 'play again option'
    gameLoopThread = new GameLoopThread(this.getHolder(), this);
    gameLoopThread.running = true;
    gameLoopThread.start();
    mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, getWidth(), getHeight(), true);

    for (int sp =0; spritesArrayList.size() < spNumber; sp++) {
        spritesArrayList.add(sprite = new Sprite(this));
    }
}

我不确定当我必须从对象中添加/删除项时,它为什么不起作用,通常我使用HashMap

在您的例子中,类似于HashMap的东西,因此您可以通过Hash键添加/删除标识它们的项

e、 g


我知道这不是一个真正的答案,但作为一个建议,我们将不胜感激

在哪里定义/初始化spritesArrayList?它只是这样:私有ArrayList spritesArrayList;int spNumber=5@mikekeepsonshineplease发布初始化和操作spritesArrayList的代码。这不应该是注释吗?
//HashMap<String,Sprite> hashSprite
//add
hashSprite.put("sprite1",new Sprite(this));
//remove 
hashSprite.remove("sprite1")