2016-08-09 6 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; 

} 

あなたがあなたのspeedX変数に加えてspeedY変数を宣言する必要が開始する前に**これは** 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

@selvinに感謝します。しかし、どうすればこのすべてをJavaコードに入れることができますか?説明してください。 – user5234003

答えて

1

、問題のYOUTUBEビデオです。 update()メソッドでそれを使用してspeedXおよびxポジションと同様にyポジションを更新します。また、speedX変数とspeedY変数をの代わりに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まずは、私を助けてくれてありがとうございました。あなたの助けを借りて、弾丸がXY方向に動くことができますが、少し問題があるクラスを作成することができました。今弾丸が画面から離れず、私は触れるところに行くだけです。私は作成した最新のクラスでコードを更新しています。スクリーンに何が起きているのかを示すビデオのリンクを与えています。あなたが助けてくれることを願っています。前もって感謝します。 – user5234003

関連する問題