Java 我的2d android游戏有问题吗
嘿,伙计们,我有点傻,所以不要对我太苛刻。 我正在为Android开发一个2D游戏,我有点麻烦。 它几乎完成了,但我希望在我的游戏中收集硬币,我希望他们在我的障碍物随机产卵。我已经完成了硬币的主代码,它们在我的障碍物上盘旋,你可以收集它们,但我不知道如何让它们随机繁殖:我希望你能帮我这里是我的代码: 我的硬币课:Java 我的2d android游戏有问题吗,java,android,random,2d-games,android-studio-2.1,Java,Android,Random,2d Games,Android Studio 2.1,嘿,伙计们,我有点傻,所以不要对我太苛刻。 我正在为Android开发一个2D游戏,我有点麻烦。 它几乎完成了,但我希望在我的游戏中收集硬币,我希望他们在我的障碍物随机产卵。我已经完成了硬币的主代码,它们在我的障碍物上盘旋,你可以收集它们,但我不知道如何让它们随机繁殖:我希望你能帮我这里是我的代码: 我的硬币课: enter code herepublic class Coin extends GameObject{ private Bitmap spritesheet; private do
enter code herepublic class Coin extends GameObject{
private Bitmap spritesheet;
private double dya;
private boolean playing;
private long startTime;
private Animation animation = new Animation();
private int num = 0;
private Obsticals obsticals;
public Coin(Bitmap res, int w, int h, int numFrames) {
x = GamePanel.WIDTH + 20;
y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 - 200;
dy = 0;
dx = +GamePanel.MOVESPEED;
height = h;
width = w;
Bitmap[] image = new Bitmap[numFrames];
spritesheet = res;
for (int i = 0; i < image.length; i++)
{
image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height);
}
animation.setFrames(image);
animation.setDelay(100);
}
public void update()
{
if (y <= 50) {
resetO();
}
x += dx;
long elapsed = (System.nanoTime() - startTime) / 1000000;
if (elapsed > 10000) {
dx = dx - 1;
startTime = System.nanoTime();
System.out.println(dx);
num = num + 30;
}
if (dx <= -25) {
dx = -24;
}
if (num >= 200) {
num = 200;
}
}
public void draw(Canvas canvas)
{
canvas.drawBitmap(animation.getImage(),x,y,null);
}
public void resetO(){
x = GamePanel.WIDTH + 20;
y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 - 200;
dy = 0;
dx = +GamePanel.MOVESPEED;
num = 0;
}
public void coinCollected(){
resetO();
}
}
我希望你能帮助我 这里有一个函数,用于生成一个随机位置的球。希望您能适应您的需要:
public void spawn() {
/**
* Spawns the ball at a random position
*/
Random random = new Random();
radius = random.nextFloat() * 100f + 50f;
float velocity_x = random.nextFloat() * 70.0f + 40.0f;
float position_y = random.nextFloat() * (screen_height - 120 - radius);
position = new Vector(screen_width, position_y);
paused_velocity = velocity = new Vector(-velocity_x, 0.0f);
}
来源:Virajmahesh | GitHub:落球这里有一个函数,可以将球生成到随机位置。希望您能适应您的需要:
public void spawn() {
/**
* Spawns the ball at a random position
*/
Random random = new Random();
radius = random.nextFloat() * 100f + 50f;
float velocity_x = random.nextFloat() * 70.0f + 40.0f;
float position_y = random.nextFloat() * (screen_height - 120 - radius);
position = new Vector(screen_width, position_y);
paused_velocity = velocity = new Vector(-velocity_x, 0.0f);
}
资料来源:Virajmahesh | GitHub:落球