1

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

}  
Matt Craig
  • 11
  • 1
  • @DerGolem but surely if that was true if I waited long enough a sprite would appear on screen? With how it is when I load a random sprite a sprite simply never appears. – Matt Craig May 03 '15 at 18:58
  • @DerGolem Does my wraparound not prevent this issue? If not is there a way that I could get it to only generate numbers the height of the screen? – Matt Craig May 03 '15 at 19:02
  • @DerGolem Does this part not cover it? if (y >= gameView.getHeight()){//if gone of the bottom of screen y -= gameView.getHeight();//Reset y – Matt Craig May 03 '15 at 19:05
  • @DerGolem, I know how to do that, is there a quick way to find out the max horizontal position of the android screen? If it helps I'm testing it on a 5.4 inch screen – Matt Craig May 03 '15 at 19:09
  • http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels – Phantômaxx May 03 '15 at 19:17

0 Answers0