2016-08-09 70 views
-4

我想創建一個類,它可以觸摸我觸摸的點[手機屏幕上的XY點]。我搜索了很多,但我無法找到任何好的資源來讀取。 我創造了一個只能在X方向拍攝的課程。我也上傳了圖片。 任何幫助,將不勝感激。在XY方向拍攝

在此先感謝。

public class Gun { 

    private float x, y; 
    private int speedX; 
    private boolean Visible; 
    private Rect rect; 

    public Gun (int startX, int startY) 
    { 
     x = startX; 
     y = startY; 
     speedX = 220; 
     Visible = true; 
     rect = new Rect(); 
    } 

    public void update(float delta) 
    { 
     x += speedX*delta; 
     if (x > 800) 
     { 
      Visible = false; 
     } 
     updateRect(); 
    } 

    private void updateRect() 
    { 
     rect.set((int) x, (int) y, (int) x + 20, (int) y + 10); 
    } 

    public void onCollideWith(Enemy e) 
    { 
     Visible = false; 
    } 

這是最近的代碼,我已更新但有一點問題。

public class Gum { 
    private float x,y; // x & y position of the gun 
    private float bulletSpeed,speedX,speedY;  
    //speedX and speedY are define to update x & y position of bullets 
    private boolean Visible; 
    private Rect rect; 
    private float handlerX, handlerY; //to get the X and Y value of the touch 
    public Gum(int startX, int startY) 
     { 
      x = startX; 
      y = startY; 
      bulletSpeed = 220; // 
      Visible = true; 
      rect = new Rect(); 
     } 
    public void update(float delta) 
     { 
      x+= speedX*delta; 
      y += speedY*delta; 
      if(x>800 || y >450) 
       { 
        Visible = false; 
       } 
     updateBullets(); 
     updateRect(); 
     } 
    private void updateBullets() 
     { 
      handlerX = InputHandler.scaledX ; 
      handlerY = InputHandler.scaledY ; 
      //location of the touch - location of the gun 
      float deltaX = handlerX - x; 
      /* THIS DEFINE IN ANOTHER CLASS (InputHandler) 
      * scaledX = (int) ((event.getX()/v.getWidth())* *GameMainActivity.GAME_WIDTH);  
      scaledY = (int) ((event.getY()/v.getHeight()) * GameMainActivity.GAME_HEIGHT); 

      */ 

       float deltaY = handlerY - y; 
       float length =(float) (Math.sqrt(Math.pow(deltaX, 2)) + Math.pow(deltaY, 2)); 

       float normalDeltaX = deltaX/ length; 
       float normalDeltaY = deltaY/length; 

       speedX = (bulletSpeed * normalDeltaX); 
       speedY = bulletSpeed * normalDeltaY; 

} 

**這是問題的YOUTUBE視頻** https://youtu.be/C6AdnU_2Qz4

This is the image of the class which I have created, This class can shoot only in single direction

+1

https://en.wikipedia.org/wiki/Basis_(linear_algebra)https://www.mathsisfun.com/algebra/vectors.html – Selvin

+0

thanks @selvin。但我怎麼能把所有這一切在Java代碼?請解釋。 – user5234003

回答

1

開始之前,你需要在添加speedY變量聲明你的speedX變量。在update()方法中使用它來更新y的位置,就像您使用speedXx的位置一樣。此外,您應該使speedX和speedY變量float而不是int

這裏是你做了什麼讓子彈朝播放器(在類的構造函數執行此操作)會:

//First you need to find the tap position. 
//In libgdx you can do this using Gdx.input.getX() and Gdx.input.getY() 
float tapX = Gdx.input.getX(); 
float tapY = Gdx.input.getY(); 

//Calculate the x and y distance from the gun to the tap. 
//This gives us a vector between the player and the tap position. 
float dx = tapX - x; 
float dy = tapY - y; 

//Now we need to normalize the vector so that it is of length 1. 
//This is so that we can control the speed of the bullet. 
//First use the pythagorean theorem to calculate the length of the vector 
//(this is the same as the distance between the player and the tap position). 
float length = (float)Math.sqrt(dx*dx + dy*dy); 

//failsafe to avoid division by zero 
if (length == 0) 
{ 
    length = 1; 
    dx = 1; 
    dy = 0; 
} 

//Divide the vector components by the length to make it length one. 
dx /= length; 
dy /= length; 

//Now we can calculate the x and y speed of the bullet! 
final float bulletSpeed = 220; //change this number to speed up or slow down the bullet 
speedX = bulletSpeed * dx; 
speedY = bulletSpeed * dy; 

如果您對此有任何疑問,請隨時問。

+0

如果他準確地點擊槍,該怎麼辦? 長度將爲零,他將被零運行時錯誤劃分。 –

+0

沒錯,忘了。補充一下 –

+0

@AlfredAndersson的支票首先謝謝你幫助我Sir.With你的幫助,我可以創建一個班,讓子彈可以在XY方向移動,但有一點問題。現在子彈不會離開屏幕,他們只是在我觸摸的地方。我用最近創建的課程更新了代碼,並給出了視頻鏈接,它會告訴你屏幕上發生了什麼。希望你能幫上忙。提前致謝。 – user5234003