我想创建一个类,在我触摸[手机屏幕上的XY点]的点上拍摄。我搜索了很多,但我找不到任何好的资源来阅读。 我创建了一个只能在X方向拍摄的类。我还上传了图片。 任何帮助将不胜感激。
提前致谢。
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
答案 0 :(得分:1)
在开始之前,除了speedY
变量之外,还需要声明speedX
变量。在update()方法中使用它来更新y
位置,就像使用speedX
和x
位置一样。此外,您应该使用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;
如果您对此有任何疑问,请随时提出。