0

我想发射一个像导弹一样的射弹(一个物体),具有恒定的速度(x,y),但我希望射弹根据用户的触摸飞行。

我怎样才能实现这样的目标?

PS我试图做这样的事情,但这不是我需要的。无论用户触摸如何,它都会飞到一个恒定点。它还受重力影响:

package com.david.helpers;

import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.utils.Array;
import com.david.objects.Acorn;

public class InputHandler implements InputProcessor{

    private Body ground;
    private Vector2 target;
    private Vector3 temp;
    private OrthographicCamera camera;
    private Array<Acorn> acorns;
    private static final float Pixels_To_Meters = 32.0f;
    public boolean isTouchDown = false;
    public InputHandler(Body ground,  Array<Acorn> acorns, OrthographicCamera camera) {
        this.camera = camera;
        this.ground = ground;
        this.acorns = acorns;
        target = new Vector2();
        temp = new Vector3();
    }
    @Override
    public boolean keyDown(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        camera.unproject(temp.set(screenX, screenY, 0));
        target.x = temp.x;
        target.y = temp.y;
        isTouchDown = true;
        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        acorns.peek().getBody().applyLinearImpulse(20-target.x/Pixels_To_Meters, 20-target.y/Pixels_To_Meters*0.1f, target.x, target.y, true);
        isTouchDown = false;
        return true;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        // TODO Auto-generated method stub
        camera.unproject(temp.set(screenX, screenY, 0));
        target.x = temp.x;
        target.y = temp.y;
        return true;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        // TODO Auto-generated method stub
        return false;
    }
    public Vector2 getTarget() {
        return this.target;
    }
}

无论用户触摸如何,身体都会移动到一个恒定点。

4

0 回答 0