2015-06-07 38 views
0

我試圖讓子彈在每次觸摸屏幕時射出太空船精靈,但子彈精靈正好從屏幕的左角移開。請幫助子彈不在我的太空船精靈旁邊拍攝(LIbgdx)

子彈類

public class Bullet { 
    public Vector2 bulletLocation = new Vector2(0,0); 
    public Vector2 bulletVelocity = new Vector2(0,0); 

    public Bullet(Vector2 sentLocation,Vector2 sentVelocity){ 
    bulletLocation = new Vector2(sentLocation.x,sentLocation.y); 
    bulletVelocity = new Vector2(sentVelocity.x,sentVelocity.y); 
    } 

    public void Update(){ 
    bulletLocation.x+=bulletVelocity.x; 
    bulletLocation.y+= bulletVelocity.y; 
    } 
} 

主類

public class MyGdxGame extends ApplicationAdapter implements ApplicationListener,InputProcessor { 
    SpriteBatch batch; 
    private Sprite spriteLeftButton; 
    private Sprite spriteRightButton; 
    private Sprite spaceShip; 
    private Texture buttonTexture; 
    private OrthographicCamera camera; 
    private Rectangle leftButton, rightButton, spaceshipr; 
    private TextureRegion region; 
    private Sprite spriteBullet; 
    private Array<Rectangle> raindrops; 
    private long lastDropTime; 
    private Rectangle raindrop; 
    private Rectangle camSize; 
    Vector2 shipLocation = new Vector2(0, 0); 
    Vector2 cursorLocation = new Vector2(0, 0); 
    float screenWidth = 0; 
    float screenHeight = 0; 

    Bullet testBullet = new Bullet(shipLocation, new Vector2(10, 0)); 

    ArrayList<Bullet> bulletManager = new ArrayList<Bullet>(); 

    @Override 
    public void create() { 

    //gets screen dimensions 
    screenWidth = Gdx.graphics.getWidth(); 
    screenHeight = Gdx.graphics.getHeight(); 

    raindrops = new Array<Rectangle>(); 
    raindrop = new Rectangle(); 
    raindrops.add(raindrop); 
    lastDropTime = TimeUtils.nanoTime(); 

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

    batch = new SpriteBatch(); 
    buttonTexture = new Texture(Gdx.files.internal("pics/idf_frigate_tut_06.jpg")); 
    spaceShip = new Sprite(buttonTexture); 
    spaceShip.setPosition(300, 300); 


    spaceshipr = new Rectangle(); 
    spaceshipr.x = 300/2 - 64/2; 
    spaceshipr.y = 20; 
    spaceshipr.width = 64; 
    spaceshipr.height = 64; 

    buttonTexture = new Texture(Gdx.files.internal("pics/bullet.jpg")); 
    spriteBullet = new Sprite(buttonTexture); 

    //try buttonTexture 
    Gdx.input.setInputProcessor(this); 
    } 

    @Override 
    public void render() { 

    Gdx.gl.glClearColor(1, 1, 1, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 


    camera.update(); 
    batch.setProjectionMatrix(camera.combined); 

    batch.begin(); 
    batch.draw(spaceShip, spaceshipr.x, spaceshipr.y); 
    if(Gdx.input.isTouched()){ 
     batch.draw(spriteBullet, spaceshipr.x,spaceshipr.y); 

     int counter = 0; 
     while (counter < bulletManager.size()) { 

     Bullet currentBullet = bulletManager.get(counter); 
     currentBullet.Update(); 

     batch.draw(spriteBullet, currentBullet.bulletLocation.x,currentBullet.bulletLocation.y); 
     counter++; 
     } 
    } 
    batch.end(); 
    } 

    @Override 
    public boolean keyDown(int keycode) { 
    return false; 
    } 

    @Override 
    public boolean keyUp(int keycode) { 
    return false; 
    } 

    @Override 
    public boolean keyTyped(char character) { 
    return false; 
    } 

    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
    if (Gdx.input.isTouched()) { 
     Vector3 touchPos = new Vector3(); 
     touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); 
     camera.unproject(touchPos); 
     spaceshipr.x = touchPos.x - 64; 
     spaceshipr.y = touchPos.y - 64; 

     raindrop.x = touchPos.x + 50; 
     raindrop.y = touchPos.y + 50; 

     Bullet myBullet = new Bullet(shipLocation, new Vector2(0,10)); 
     bulletManager.add(myBullet); 
    } 

    return true; 
    } 

    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
    return false; 
    } 

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer) { 
    if(Gdx.input.isTouched()) { 
     Vector3 touchPos = new Vector3(); 
     touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); 
     camera.unproject(touchPos); 
     spaceshipr.x = touchPos.x - 64; 
     spaceshipr.y = touchPos.y - 64; 
    } 
    } 
} 
+0

你的問題解決了嗎?如果是,請將答案標記爲已接受。 – vojta

回答

1

的問題是你把每一個新的子彈shipLocation,這在遊戲過程中不會被更新。使用船的當前座標並改爲創建一個新的2d向量。