Libgdx 在随机位置生成随机的不同对象
我正在做一个游戏,不同的物体从屏幕的上半部分落到下半部分。我对如何随机选择要生成的TextureRegion和不更改已生成的TextureRegion存在问题。当我运行游戏时,让我们假设电子区域首先生成,然后当下一个生成时,让我们假设它是一个反质子区域,第一个电子区域更改为我不想要的反质子区域 这是我的游戏课:Libgdx 在随机位置生成随机的不同对象,libgdx,Libgdx,我正在做一个游戏,不同的物体从屏幕的上半部分落到下半部分。我对如何随机选择要生成的TextureRegion和不更改已生成的TextureRegion存在问题。当我运行游戏时,让我们假设电子区域首先生成,然后当下一个生成时,让我们假设它是一个反质子区域,第一个电子区域更改为我不想要的反质子区域 这是我的游戏课: public class GameScreenTest implements Screen { ... @Override public void render(float delta
public class GameScreenTest implements Screen {
...
@Override
public void render(float delta) {
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.disableBlending();
game.batch.draw(background, 0, 0);
game.batch.enableBlending();
for(Particles particle: particles) {
particlesControl.draw(particle.x, particle.y);
}
game.batch.end();
if(TimeUtils.millis() - lastDropTime > 500) {
particlesControl.spawn();
particlesControl.update();
}
Iterator<Particles> iter = particles.iterator();
while(iter.hasNext()) {
Particles particle = iter.next();
particle.y -= 200 * Gdx.graphics.getDeltaTime();
if(particle.y + particle.height < 0) {
iter.remove();
}
}
}
...
private class Particles {
private int width;
private int height;
private int x;
private int y;
private Particles() {
}
private void spawn() {
Particles particle = new Particles();
particle.x = MathUtils.random(0, 480 - width);
particle.y = 800;
particle.width = width;
particle.height = height;
particles.add(particle);
lastDropTime = TimeUtils.millis();
}
private void update() {
choice = MathUtils.random(1, 4);
switch(choice) {
case 1:
chosen = new TextureRegion(protonRegion);
width = 75;
height = 75;
break;
case 2:
chosen = new TextureRegion(electronRegion);
width = 75 / 2;
height = 75 / 2;
break;
case 3:
chosen = new TextureRegion(antiprotonRegion);
width = 75;
height = 75;
break;
case 4:
chosen = new TextureRegion(antielectronRegion);
width = 75 / 2;
height = 75 / 2;
break;
}
}
private void draw(int x, int y) {
game.batch.draw(chosen, x, y, width, height);
}
}
公共类GameScreenTest实现屏幕{
...
@凌驾
公共无效渲染(浮动增量){
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.disableBlending();
游戏。批量。抽签(背景,0,0);
game.batch.enableBluding();
用于(粒子:粒子){
particlesControl.draw(particle.x,particle.y);
}
game.batch.end();
如果(TimeUtils.millis()-lastDropTime>500){
particlesControl.spawn();
particlesControl.update();
}
迭代器iter=particles.Iterator();
while(iter.hasNext()){
粒子=iter.next();
particle.y-=200*Gdx.graphics.getDeltaTime();
if(particle.y+particle.height<0){
iter.remove();
}
}
}
...
私有类粒子{
私有整数宽度;
私人内部高度;
私人INTX;
私营企业;
私人粒子(){
}
私有void spawn(){
粒子=新粒子();
particle.x=MathUtils.random(0480-宽度);
颗粒y=800;
粒子宽度=宽度;
粒子高度=高度;
粒子。添加(粒子);
lastDropTime=TimeUtils.millis();
}
私有void更新(){
选择=数学随机(1,4);
开关(选择){
案例1:
所选=新纹理区域(原区域);
宽度=75;
高度=75;
打破
案例2:
所选=新纹理区域(电子区域);
宽度=75/2;
高度=75/2;
打破
案例3:
所选=新纹理区域(反质子区域);
宽度=75;
高度=75;
打破
案例4:
所选=新纹理区域(反电子区域);
宽度=75/2;
高度=75/2;
打破
}
}
专用空白绘制(整数x,整数y){
游戏。批量。抽签(选择,x,y,宽度,高度);
}
}
我想知道为什么每次进行随机选择时,所有生成的对象都会发生变化,当然,还有如何解决此问题。谢谢。
public class GameScreenTest implements Screen {
final AntimatterBlast game;
private Texture gameObjects;
private TextureRegion electronRegion;
private TextureRegion antielectronRegion;
private TextureRegion protonRegion;
private TextureRegion antiprotonRegion;
//===========================================//
//remove private TextureRegion chosen;
//===========================================//
private TextureRegion background;
private Music gameMusic;
private OrthographicCamera camera;
private Array<Particles> particles;
private Particles particlesControl;
private long lastDropTime;
private int choice;
public GameScreenTest(final AntimatterBlast game) {
this.game = game;
gameObjects = new Texture(Gdx.files.internal("GameObjects.png"));
electronRegion = new TextureRegion(gameObjects, 105, 103, 50, 50);
antielectronRegion = new TextureRegion(gameObjects, 105, 155, 46, 46);
protonRegion = new TextureRegion(gameObjects, 6, 6, 100, 100);
antiprotonRegion = new TextureRegion(gameObjects, 6, 108, 90, 90);
background = new TextureRegion(gameObjects, 0, 204, 480, 800);
gameMusic = Gdx.audio.newMusic(Gdx.files.internal("DST-ElektroHauz.mp3"));
gameMusic.setLooping(true);
camera = new OrthographicCamera();
camera.setToOrtho(false, 480, 800);
particles = new Array<Particles>();
particlesControl = new Particles();
//===========================================//
//choice = MathUtils.random(1, 4); //remove
//chosen = new TextureRegion(protonRegion); //remove
//===========================================//
}
@Override
public void render(float delta) {
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.disableBlending();
game.batch.draw(background, 0, 0);
game.batch.enableBlending();
for(Particles particle: particles) {
//===========================================//
particlesControl.draw(particle.chosen, particle.x, particle.y);
//change
//particlesControl.draw(particle.x, particle.y);
//===========================================//
}
game.batch.end();
if(TimeUtils.millis() - lastDropTime > 500) {
//===========================================//
particlesControl.spawn();
//===========================================//
//===========================================//
//remove particlesControl.update();
//===========================================//
}
Iterator<Particles> iter = particles.iterator();
while(iter.hasNext()) {
Particles particle = iter.next();
particle.y -= 200 * Gdx.graphics.getDeltaTime();
if(particle.y + particle.height < 0) {
iter.remove();
}
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
gameMusic.play();
particlesControl.spawn();
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
gameObjects.dispose();
gameMusic.dispose();
}
private class Particles {
//===========================================//
private TextureRegion chosen; //add variable
//===========================================//
private int width;
private int height;
private int x;
private int y;
private Particles() {
}
private void spawn() {
Particles particle = new Particles();
particle.x = MathUtils.random(0, 480 - width);
particle.y = 800;
particle.width = width;
particle.height = height;
//===========================================//
particle.selectTexture();
//===========================================//
lastDropTime = TimeUtils.millis();
//===========================================//
selectTexture(); //add call
//===========================================//
particles.add(particle);
}
//===========================================//
private void selectTexture() { //change name, but is not nesesari
//===========================================//
choice = MathUtils.random(1, 4);
switch(choice) {
case 1:
//===========================================//
//change
//chosen = new TextureRegion(antielectronRegion);
//if you are not going to change or modific TextureRegion
//independet other the textureRegion,
//I think you could use it well.It is just an idea
chosen = protonRegion;
//===========================================//
width = 75;
height = 75;
break;
case 2:
//===========================================//
//change
//chosen = new TextureRegion(antielectronRegion);
//if you are not going to change or modific TextureRegion
//independet other the textureRegion,
//I think you could use it well.It is just an idea
chosen = electronRegion;
//===========================================//
width = 75 / 2;
height = 75 / 2;
break;
case 3:
//===========================================//
//change
//chosen = new TextureRegion(antielectronRegion);
//if you are not going to change or modific TextureRegion
//independet other the textureRegion,
//I think you could use it well.It is just an idea
chosen = antiprotonRegion;
//===========================================//
width = 75;
height = 75;
break;
case 4:
//===========================================//
//change
//chosen = new TextureRegion(antielectronRegion);
//if you are not going to change or modific TextureRegion
//independet other the textureRegion,
//I think you could use it well.It is just an idea
chosen = antielectronRegion;
//===========================================//
width = 75 / 2;
height = 75 / 2;
break;
}
}
//===========================================//
private void draw(TextureRegion chosen, int x, int y) {
game.batch.draw(chosen, x, y, width, height);
//===========================================//
}
}
}
公共类GameScreenTest实现屏幕{
最后一场反物质末日游戏;
私有纹理游戏对象;
私人纺织区电子区;
私有纹理区域反电子区域;
私有结构区域原区域;
私有结构区域反质子区;
//===========================================//
//移除所选区域的私有纹理;
//===========================================//
私人区域背景;
私人音乐游戏音乐;
私人正交摄影机;
私有阵列粒子;
私人控制;
私人长时间;
私人选择;
公共游戏屏幕测试(最终反物质末日游戏){
这个游戏=游戏;
gameObjects=新纹理(Gdx.files.internal(“gameObjects.png”);
电子区域=新的纹理区域(游戏对象,105、103、50、50);
反电子区域=新的纹理区域(游戏对象,105,155,46,46);
protonRegion=新的纹理区域(gameObjects,6,6100100);
反质子区域=新的纹理区域(游戏对象,6,108,90,90);
背景=新的纹理区域(游戏对象,0,204,480,800);
gameMusic=Gdx.audio.newMusic(Gdx.files.internal(“DST ElektroHauz.mp3”);
gameMusic.setLooping(true);
摄影机=新的正交摄影机();
摄像头。设置为ToOrtho(假,480800);
粒子=新数组();
particlesControl=新粒子();
//===========================================//
//choice=MathUtils.random(1,4);//删除
//所选=新纹理区域(原区域);//删除
//===========================================//
}
@凌驾
公共无效渲染(浮动增量){
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.disableBlending();
游戏。批量。抽签(背景,0,0);
game.batch.enableBluding();
用于(粒子:粒子){
//===========================================//
particlesControl.draw(particle.selected、particle.x、particle.y);
//改变
//particlesControl.draw(particle.x,particle.y);
//===========================================//
}
game.batch.end();
如果(TimeUtils.millis()-lastDropTime>500){
//===========================================//
particlesControl.spawn();
//===========================================//
//===========================================//
//删除particlesControl.update();
//===========================================//
}
迭代器iter=particles.Iterator();
while(iter.hasNext()){
粒子=iter.next();
particle.y-=200*Gdx.graphics.getDeltaTime();
if(particle.y+particle.height<0){
iter.remove();
}
}
}
@凌驾
公共空心调整大小(整型宽度、整型高度){
}
@凌驾
公开展览({
游戏音乐;
particlesControl.spawn();
}
@凌驾
公共空间隐藏(){
}
@凌驾
公共空间暂停(){
}
@凌驾
公众简历(){
}
@