Currently I'm making a simple shooter game where each time a sprite is pressed it will increment the hitcount and allow for a high score. Currently the sprite will enter from the left and will increment each time making it enter lower down the screen each time unless it has reached the bottom where it will start again from the top. What I want to do is change it so that the y coordinate will generate a random number but when I tried to make y = random.nextInt() the sprite just didn't load so I'm not sure what to do, any help would be appreciated at all thanks.
package cct.mad.lab;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
public class Sprite {
//x,y position of sprite - initial position (0,50)
private int x = 0;
private int y = 50;
private int xSpeed = 17;//Horizontal increment of position (speed)
private int ySpeed = 5;// Vertical increment of position (speed)
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();
public Sprite(GameView gameView) {
this.gameView=gameView;
spritebmp = BitmapFactory.decodeResource(gameView.getResources(),
R.drawable.sprite);
this.bmp_width = spritebmp.getWidth();
this.bmp_height= spritebmp.getHeight();
}
//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);
}
/* 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 wrapAround(){
//Code to wrap around
if (x < 0) x = x + gameView.getWidth(); //increment x whilst not off screen
if (x >= gameView.getWidth()){ //if gone of the right sides of screen
x = x - gameView.getWidth(); //Reset x
}
if (y < 0) y = y + gameView.getHeight();//increment y whilst not off screen
if (y >= gameView.getHeight()){//if gone of the bottom of screen
y -= gameView.getHeight();//Reset y
}
}
}