问题描述
我正在开发一个游戏libgdx和我被困在一个点上。所以,我的SpriteBatch绘制所有的都具有相同的纹理数组中的矩形,但我想,每一个都有自己的质感。 MY code看起来像这样
I am developing a game with libgdx and i got stuck at a point. So My SpriteBatch draws for all the Rectangles that are in an array with the same texture but I want that every single one has its own texture. MY Code looks like this
public class GameScreen implements Screen{
final MrJetpack game;
OrthographicCamera camera;
SpriteBatch batch;
ShapeRenderer rend;
private Array<Rectangle> raindrops;
Texture enemy1,enemy2,enemy3,enemy4,endScreen;
TextureRegion[] enemys = new TextureRegion[4];
private int random;
public GameScreen(final MrJetpack game){
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
enemy1 = new Texture(Gdx.files.internal("boxk.png"));
enemy2 = new Texture(Gdx.files.internal("boxg.png"));
enemy3 = new Texture(Gdx.files.internal("kugel.png"));
enemy4 = new Texture(Gdx.files.internal("kugelk.png"));
enemys[0] = new TextureRegion(enemy1);
enemys[1] = new TextureRegion(enemy2);
enemys[2] = new TextureRegion(enemy3);
enemys[3] = new TextureRegion(enemy4);
raindrops = new Array<Rectangle>();
rend = new ShapeRenderer();
batch = new SpriteBatch();
}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
//Gdx.gl.glClearColor(0, (float)148/255,(float) 255/255, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
rend.begin(ShapeType.Filled);
rend.rect(0, 0, 800, 10);
rend.rect(0, 160, 800, 10);
rend.rect(0, 320, 800, 10);
rend.setColor(Color.ORANGE);
rend.end();
batch.begin();
for(Rectangle raindrop: raindrops) {
batch.draw(enemys[random], raindrop.x - 10, raindrop.y);
}
batch.end();
if(TimeUtils.nanoTime() - lastDropTime > spawnTime){
spawnRaindrop();
}
Iterator<Rectangle> iter = raindrops.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.x -= 20 * Gdx.graphics.getDeltaTime();
if(raindrop.x < 0) {
spawnRaindrop();
iter.remove();
}
}
}
private void spawnRaindrop() {
Rectangle raindrop = new Rectangle();
raindrop.x = 800;
stages = MathUtils.random(1, 3);
random = MathUtils.random(0, 3);
raindrop.width = 30;
raindrop.height = 53;
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
那么,实际发生的是,在屏幕上每次一个新的Rectangle产卵其他的人谁已经显示变化的纹理让每一个矩形得到了相同的纹理。任何解决方案或例子?
So what actually happening is that everytime a new Rectangle spawns in the screen the other ones who were already displayed change the Texture so every Rectangle got the same Texture . Any solutions or examples?
编辑: http://imgur.com/46ywYyy 这是我的问题的人谁明白我的问题虚假:)就像你所看到的纹理正在改变所有其它的矩形,但我想每个人都有自己的静态纹理
http://imgur.com/46ywYyyThis is my problem for people who understood my question false :)Like you can see the texture is changing for all the other rectangles but i want everyone to have their static texture
推荐答案
他们告诉你,一个办法做到这一点,但我看你不太清楚,我会把更多或更少,你可能会做。
They told you, one way to do it, but I see you do not clear, I'll put more or less as you might do.
我会尽力使形式更类似于你已经在你的code。
I'll try to make the form more similar to what you already have in your code.
注意这code可能有语法错误,等等,becouse我从编辑器计算器做的事情。
notethis code may have syntax error, among others, becouse I'm doing from the editor StackOverflow.
1 - 创建一个从精灵派生的类,例如是这样的:
1- create a class that is derived from sprite, for example something like:
public class SpriteRaindrop extends Sprite{
Rectangle raindrop = new Rectangle();
public SpriteRaindrop(Texture t, int srcWidth,
int srcHeigth,
float posX,
float posY){
super(t, srcWidth, srcHeigth);
raindrop.x = posX;
raindrop.y = posY;
raindrop.width = srcWidth;
raindrop.height = srcHeigth;
setPosition(posX, posY);
}
public void updateR(){
raindrod.x = getX();
raindrod.y = getY();
}
}
这你code与任何修改
This your code with with any changes
public class GameScreen implements Screen{
final MrJetpack game;
OrthographicCamera camera;
SpriteBatch batch;
ShapeRenderer rend;
private Array<SpriteRaindrop> raindrops;
Texture enemy1,enemy2,enemy3,enemy4,endScreen;
TextureRegion[] enemys = new TextureRegion[4];
private int random;
public GameScreen(final MrJetpack game){
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
enemy1 = new Texture(Gdx.files.internal("boxk.png"));
enemy2 = new Texture(Gdx.files.internal("boxg.png"));
enemy3 = new Texture(Gdx.files.internal("kugel.png"));
enemy4 = new Texture(Gdx.files.internal("kugelk.png"));
enemys[0] = new TextureRegion(enemy1);
enemys[1] = new TextureRegion(enemy2);
enemys[2] = new TextureRegion(enemy3);
enemys[3] = new TextureRegion(enemy4);
raindrops = new Array<SpriteRaindrop>();
rend = new ShapeRenderer();
batch = new SpriteBatch();
}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
//Gdx.gl.glClearColor(0, (float)148/255,(float) 255/255, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
rend.begin(ShapeType.Filled);
rend.rect(0, 0, 800, 10);
rend.rect(0, 160, 800, 10);
rend.rect(0, 320, 800, 10);
rend.setColor(Color.ORANGE);
rend.end();
batch.begin();
for(SpriteRaindrop raindrop: raindrops) {
//raindrop.translatex(-10f);
//what is raindrop.y value
raindrop.updateR();
raindrop.draw(batch);
}
batch.end();
if(TimeUtils.nanoTime() - lastDropTime > spawnTime){
spawnRaindrop();
}
Iterator<SpriteRaindrop> iter = raindrops.iterator();
while(iter.hasNext()) {
SpriteRaindrop raindrop = iter.next();
raindrop.translateX(-20f * Gdx.graphics.getDeltaTime());
if(raindrop.getX() < 0) {
spawnRaindrop();
iter.remove();
}
}
}
private void spawnRaindrop() {
stages = MathUtils.random(1, 3);
random = MathUtils.random(0, 3);
lastDropTime = TimeUtils.nanoTime();
SpriteRaindrop raindrop = new SpriteRaindrop(enemys[random],
30, 53,
800, 0);
raindrops.add(raindrop);
}
这只是一个想法,但我认为它可以工作,希望大家帮忙
this is just an idea, but I think it can work, I hope you help
这篇关于Libgdx呈现为一个矩形阵列不同的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!