2013-03-05 99 views
1

我不能用我的演员拍摄。当我按住拍摄按钮时,我的演员前面有子弹,但它不会移动。我不知道如何成为它动如陨石......android libgdx用演员拍摄

public class Game implements ApplicationListener { 
    Texture meteoriteImage; 
    Texture shipImage; 
    Texture bulletImage; 
    SpriteBatch batch; 
    OrthographicCamera camera; 
    Rectangle ship; 
    Rectangle bullet; 
    Array<Rectangle> meteorites; 
    long lastMeteoriteTime; 


    public void create() { 
     meteoriteImage = new Texture(Gdx.files.internal("meteorite.png")); 
     shipImage = new Texture(Gdx.files.internal("ship.png")); 
     bulletImage = new Texture(Gdx.files.internal("bullet.png")); 

     camera = new OrthographicCamera(); 
     camera.setToOrtho(false, 800, 480); 
     batch = new SpriteBatch(); 

     ship = new Rectangle(); 
     ship.x = 800 - 48; 
     ship.y = 480/2 - 48/2; 
     ship.width = 48; 
     ship.height = 48; 

     meteorites = new Array<Rectangle>(); 
     spawnMeteorite(); 
    } 

    private void spawnMeteorite() { 
     Rectangle meteorite = new Rectangle(); 
     meteorite.y = MathUtils.random(0, 480-48); 
     meteorite.x = 0; 
     meteorite.width = 48; 
     meteorite.height = 48; 
     meteorites.add(meteorite); 
     lastMeteoriteTime = TimeUtils.nanoTime(); 
    } 


    public void render() { 
     Gdx.gl.glClearColor(0, 0, 0.2f, 1); 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
     camera.update(); 
     batch.setProjectionMatrix(camera.combined); 

     batch.begin(); 
     batch.draw(shipImage, ship.x, ship.y); 
     for(Rectangle meteorite: meteorites) { 
     batch.draw(meteoriteImage, meteorite.x, meteorite.y); 
     } 
     batch.end(); 

     if(Gdx.input.isKeyPressed(Keys.DPAD_UP)) ship.y += 200 * Gdx.graphics.getDeltaTime(); 
     if(Gdx.input.isKeyPressed(Keys.DPAD_DOWN)) ship.y -= 200 * Gdx.graphics.getDeltaTime(); 
     if(Gdx.input.isKeyPressed(Keys.BUTTON_Y)){ 
       bullet = new Rectangle(); 
       bullet.x = ship.x - 48; 
       bullet.y = ship.y; 
       bullet.width = 48; 
       bullet.height = 48; 
       batch.begin(); 
       batch.draw(bulletImage, bullet.x, bullet.y); 
       batch.end(); 

      while(bullet.x > 0){ 
       bullet.x -= 200 * Gdx.graphics.getDeltaTime(); 
      } 
     } 

     if(ship.x < 0) ship.x = 0; 
     if(ship.x > 800 - 48) ship.x = 800 - 48; 
     if(ship.y < 0) ship.y = 0; 
     if(ship.y > 480 - 48) ship.y = 480 - 48; 

     if(TimeUtils.nanoTime() - lastMeteoriteTime > 1000000000) spawnMeteorite(); 

     Iterator<Rectangle> iter = meteorites.iterator(); 
     while(iter.hasNext()) { 
     Rectangle meteorite = iter.next(); 
     meteorite.x += 200 * Gdx.graphics.getDeltaTime(); 
     if(meteorite.x + 48 < 0) iter.remove(); 
     if(meteorite.overlaps(ship)) { 

      iter.remove(); 
     } 
     } 
    } 


    public void dispose() { 
     shipImage.dispose(); 
     meteoriteImage.dispose(); 
     bulletImage.dispose(); 
     batch.dispose(); 
    } 


    public void resize(int width, int height) { 
    } 


    public void pause() { 
    } 


    public void resume() { 
    } 
} 

我在本教程中的工作:https://code.google.com/p/libgdx/wiki/SimpleApp

+0

如果按下BUTTON_Y,则只更新bullet.x。 – 2013-03-05 19:33:05

回答

1

嗯,这 - 是不是你想要的。

while(bullet.x > 0){ 
    bullet.x -= 200 * Gdx.graphics.getDeltaTime(); 
} 

你真的想让子弹的位置变成动画吗?因此,您需要将子弹添加为世界的对象,然后在每次调用“渲染”时将其更改为x位置。否则,你不会看到动画在所有...

0

看来你错过了在子弹循环来检查哪些陨石,试试这个:

while(iter2.hasNext()) { 
    Rectangle bullet = iter2.next(); 
    bullet.x -= 200 * Gdx.graphics.getDeltaTime(); 
    if(bullet.x + 48 < 0) iter2.remove(); 
    iter = meteorites.iterator(); 
    while(iter.hasNext()) { 
    if(bullet.overlaps(iter.next())) { 
     iter2.remove(); 
     iter.remove(); 
    } 
    } 
} 

提醒不要简单地把原来的陨石因为你在两个循环中都有翻译代码,所以循环到子弹循环中。