samedi 16 avril 2016

Getting random sprite to appear from an ArrayList (Android Studio)

Having problems with getting sprites to appear when a random number generator hits a certain number. Essentially, I am trying to get it so every 3 seconds it will then display another image either the same or different. I have my class where everything happens for the images called ItemSprites and then a VGameView where it is all called.

This is my ItemSprites class:

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

This my VGameView class:

public class VGameView extends View {
private ItemSprites itemSprites;

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

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




Aucun commentaire:

Enregistrer un commentaire