清空后重新填充数组列表

时间:2015-05-15 08:22:12

标签: java android arraylist

我目前所拥有的是一个用户按下精灵的游戏,当按下它时会增加他们的分数,然后会使精灵消失。我遇到的问题是,我无法弄清楚如何将精灵重新出现,因为它们被完全从数组列表中删除,我会将这些精灵读到阵列列表中,还是我以不同的方式做到这一点?

这是精灵代码:

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));
    }
}

我不确定为什么它不会工作

1 个答案:

答案 0 :(得分:0)

当我必须添加/删除对象中的项目时,通常我使用HashMap

在您的情况下,类似于HashMap<String,Sprite>,因此您可以通过哈希键添加/删除标识它们的项目。

e.g。

//HashMap<String,Sprite> hashSprite
//add
hashSprite.put("sprite1",new Sprite(this));
//remove 
hashSprite.remove("sprite1")  

我知道这不是一个真正的答案,但建议可以表示赞赏!