Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 随机精灵生成?_Android_Canvas_Sprite - Fatal编程技术网

Android 随机精灵生成?

Android 随机精灵生成?,android,canvas,sprite,Android,Canvas,Sprite,目前,我正在制作一个简单的射击游戏,每次按下一个精灵,它都会增加命中率并获得高分。当前,精灵将从左侧进入,并将每次递增,使其每次进入屏幕下方,除非它已到达底部,将再次从顶部开始。我想做的是更改它,以便y坐标将生成一个随机数,但当我尝试使y=random.nextInt()时,精灵没有加载,因此我不确定该怎么办,如果您有任何帮助,我们将不胜感激。谢谢 package cct.mad.lab; import java.util.Random; import android.graphics.B

目前,我正在制作一个简单的射击游戏,每次按下一个精灵,它都会增加命中率并获得高分。当前,精灵将从左侧进入,并将每次递增,使其每次进入屏幕下方,除非它已到达底部,将再次从顶部开始。我想做的是更改它,以便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
    }
}

}  
包cct.mad.lab;
导入java.util.Random;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
公共级雪碧{
//x、 精灵的y位置-初始位置(0,50)
私有整数x=0;
私人int y=50;
private int xSpeed=17;//位置的水平增量(速度)
private int ySpeed=5;//位置的垂直增量(速度)
私人游戏视图游戏视图;
私有位图spritebmp;
//精灵图像的宽度和高度
私有整数bmp_宽度;
私家车高度;
//需要新的随机坐标。
私有随机=新随机();
公共精灵(GameView GameView){
this.gameView=gameView;
spritebmp=BitmapFactory.decodeResource(gameView.getResources(),
R.可拉拔雪碧);
this.bmp_width=spritebmp.getWidth();
this.bmp_height=spritebmp.getHeight();
}
//更新精灵的位置
公共无效更新(){
x=x+x速度;
y=y+y速度;
环绕();//调整精灵的运动。
}
公共空白绘制(画布){
//绘制精灵图像
drawBitmap(spritebmp,x,y,null);
}
/*检查是否触摸了精灵*/
公共布尔值wasItTouched(float-ex,float-ey){
布尔值=假;
如果((x=gameView.getHeight()){//if离开屏幕底部
y-=gameView.getHeight();//重置y
}
}
}  

@DerGolem但如果这是真的,如果我等待足够长的时间,一个精灵会出现在屏幕上吗?当我加载一个随机精灵时,一个精灵根本不会出现。@DerGolem我的概括并没有阻止这个问题吗?如果没有,我有没有办法让它只生成屏幕高度的数字?@DerGolem这部分没有覆盖它?如果(y>=gameView.getHeight()){//如果屏幕底部消失了y-=gameView.getHeight();//重置y@DerGolem,我知道怎么做,有没有一个快速的方法来找出android屏幕的最大水平位置?如果有帮助的话,我正在5.4英寸的屏幕上测试它screen@DerGolem这对我很有帮助,谢谢。