从ArrayList(Android Studio)获取随机精灵

时间:2016-04-16 23:04:53

标签: java android android-studio arraylist random

当随机数生成器达到某个数字时,出现精灵出现问题。基本上,我试图让它每隔3秒就能显示出相同或不同的另一张图像。我有一个类,其中一切都发生在名为ItemSprites的图像上,然后是一个VGameView,它被称为。

这是我的ItemSprites类:

public class ItemSprites {

private VGameView vGameView;
private Bitmap parrot, moneyChest, bomb;

private Random randy = new Random();
ArrayList<Integer> pirateItems = new ArrayList<Integer>();

private Random randyPosition = new Random();

private int x = 0;
private int y = 0;

private int xSpeed;
private int ySpeed;

private int height;
private int width;

public ItemSprites(VGameView vGameView) {
    this.vGameView = vGameView;

    parrot = BitmapFactory.decodeResource(null, R.drawable.parrot);
    moneyChest = BitmapFactory.decodeResource(null, R.drawable.moneychest);
    bomb = BitmapFactory.decodeResource(null, R.drawable.bomb);
}

public void onDraw(Canvas canvas) {
    update();
    for (int i = 1; i < 100; i++) {
        int randyNumber = randy.nextInt(3)+1;

        if (randyNumber == 1) {
            canvas.drawBitmap(parrot, 200, 500, null);
        }

        if (randyNumber == 2) {
            canvas.drawBitmap(moneyChest, 200, 500, null);
        }

        if (randyNumber == 3) {
            canvas.drawBitmap(bomb, 200, 500, null);
        }
    }
}

这是我的VGameView类:

public class VGameView extends View {
private ItemSprites itemSprites;

public VGameView(Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas) {
    itemSprites.onDraw(canvas);
}

0 个答案:

没有答案