2016-08-09 16 views
-4

Ich möchte eine Klasse erstellen, die an dem Punkt aufnimmt, an dem ich [XY-Punkt auf meinem Telefonbildschirm] berühre. Ich habe viel gesucht, aber ich kann keine gute Quelle finden, von der ich lesen kann. Ich habe eine Klasse erstellt, die nur in X-Richtung schießen kann. Ich habe das Bild auch hochgeladen. Jede Hilfe wäre willkommen.Aufnahme in XY-Richtung

Vielen Dank im Voraus.

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; 
    } 

Dies ist der letzte Code, den ich aktualisiert haben, aber ein kleines Problem.

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; 

} 

** DIES IST DAS YOUTUBE VIDEO DES PROBLEMS ** 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

danke @selvin. aber wie kann ich all das in Java-Code stecken? Bitte erkläre. – user5234003

Antwort

1

Bevor Sie beginnen Sie benötigen eine speedY Variable zusätzlich zu Ihren speedX Variable deklarieren. Verwenden Sie es in der update() - Methode, um die Position y genauso zu aktualisieren wie die Position speedX und x. Außerdem sollten Sie die Variablen speedX und speedY float anstelle von int eingeben.

Hier ist, was Sie tun, um die Kugeln zu erhalten gegenüber dem Spieler gehen (tun dies im Konstruktor Ihrer Klasse):

//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; 

Wenn Sie Fragen zu diesem Thema haben, zu fragen sich frei fühlen.

+0

Was ist, wenn er genau auf die Waffe klopft? Länge ist Null und er wird durch Null Laufzeitfehler geteilt. –

+0

Stimmt, vergaß das. Hinzugefügte eine Überprüfung für diese –

+0

@AlfredAndersson Zunächst einmal danke für Ihre Hilfe Sir.Mit Ihrer Hilfe kann ich eine Klasse erstellen, wo Kugeln in XY-Richtung bewegen können, aber mit einem kleinen Problem. Jetzt verlassen die Kugeln den Bildschirm nicht, sie gehen nur dahin, wo ich mich berühre. Ich aktualisiere den Code mit der letzten Klasse, die ich erstellt habe, und gebe den Link des Videos, der Ihnen zeigt, was im Bildschirm geschieht. Sie werden helfen. Danke im Voraus. – user5234003