随机精灵生成?

时间:2015-05-03 18:50:16

标签: android canvas sprite

目前我正在制作一款简单的射击游戏,每次按下精灵时,它都会增加hitcount并获得高分。目前精灵将从左侧进入并且每次都会增加,使其每次进入屏幕下方,除非它已经到达底部,它将从顶部再次开始。我想要做的是更改它,以便y坐标将生成一个随机数,但当我尝试使y = random.nextInt()时,精灵只是没有加载所以我不知道该怎么做,任何帮助我将不胜感激。

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

}  

0 个答案:

没有答案