1
\$\begingroup\$

I've been trying to create a game with two finger gameplay (ie. If your finger is in the bottom 20% of the screen it will move the player along X axis towards your finger, and if you press finger above 20% of screen height it will fire bullets towards your touch position). I pretty much have it working but I am getting problems properly using the touchUp method (i believe!). The issue I am having is that it will work just once and then no matter where I have my finger (ie. it's above 20% of screen) the player character still moves along the X-Axis. The same can be said for shooting (on very first go it won't shoot when finger is under the players height but as soon as i have let go and tried next finger press it will shoot even when i am below height)

I've wracked my brains for ages now trying to see the solution and tried many different varieties to my code. The code is pretty short so far so I will paste it all here but I am thinking my problem lies in the main GameScreen classes Overridden touch methods.

If anyone can spot the mistakes in my code I will be eternally grateful:

public class GameScreen implements Screen, InputProcessor {
EnforcerGame game;
SpriteBatch batch;
OrthographicCamera camera;
Viewport v;
Texture backgroundTexture, bulletTexture;

Dude dude;
Vector3 touchPos = new Vector3();
Vector3 touchPosFire = new Vector3();
Vector3 touchPosMove = new Vector3();
float touchPointerMove = -1;
float touchPointerFire = -1;

public GameScreen(EnforcerGame game){
    Gdx.input.setInputProcessor(this);
    this.game = game;
    batch = new SpriteBatch();
    camera = new OrthographicCamera();
    v = new FitViewport(Constants.V_WIDTH, Constants.V_HEIGHT, camera);
    camera.setToOrtho(false, v.getWorldWidth(), v.getWorldHeight());
    backgroundTexture = new Texture("backgrounds/road.jpg");
    bulletTexture = game.assetMan.get("sprites/bullet.jpg", Texture.class);
    addDude();
}
@Override
public void show() {

}

@Override
public void render(float delta) {
    update(delta);
    clearScreen();

    draw();
}

private void update(float delta) {
    dude.update(delta);
    if (touchPosFire.x != 0 && touchPosFire.y != 0){
        dude.fireBullet(bulletTexture, new Vector2(touchPosFire.x, touchPosFire.y));
    }
    if (touchPosMove.x != 0 && touchPosMove.y != 0){
        dude.movementTouch.set(touchPosMove.x, touchPosMove.y);
    }
    else {
        dude.movementTouch.setZero();
    }
}

private void clearScreen() {
    Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}

private void draw() {
    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    batch.draw(backgroundTexture, 0, 0, v.getWorldWidth(), v.getWorldHeight());
    dude.draw(batch);
    for (Bullet bullet : dude.bullets){
        bullet.draw(batch);
    }

    batch.draw(bulletTexture, touchPosFire.x, touchPosFire.y, 0.5f,0.5f);
    batch.draw(bulletTexture, touchPosMove.x, touchPosMove.y, 1,1);
    batch.end();
}

@Override
public void resize(int width, int height) {
    v.update(width, height);
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}

public void addDude(){
    dude = new Dude(game.assetMan.get("sprites/link.png", Texture.class), v.getWorldWidth() / 2, 0, v);
}

@Override
public boolean keyDown(int keycode) {
    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    camera.unproject(touchPos.set(screenX, screenY, 0));
    System.out.println("dude height: " + dude.getHeight());
    System.out.println("tDown: " + touchPos.x + "," + touchPos.y + "," + pointer + "," + button);

    if (touchPos.y < dude.getHeight()){
        touchPosMove.set(touchPos);
        touchPosMove = touchPos;
        touchPointerMove = pointer;
    }
    if (touchPos.y > dude.getHeight()){
        touchPosFire.set(touchPos);
        touchPosFire = touchPos;
        touchPointerFire = pointer;
    }
    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    camera.unproject(touchPos.set(screenX, screenY, 0));
    System.out.println("tDragged: " + touchPos.x + "," + touchPos.y + "," + pointer);

    if (touchPos.y < dude.getHeight()){
        touchPosMove.set(touchPos);
        touchPosMove = touchPos;
        touchPointerMove = pointer;
    }
    if (touchPos.y > dude.getHeight()){
        touchPosFire.set(touchPos);
        touchPosFire = touchPos;
        touchPointerFire = pointer;
    }

    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    System.out.println("tUp pointer: " + pointer);

    if (pointer == touchPointerMove){
        touchPosMove.setZero();
        touchPointerMove = -1;
    }
    else if (pointer == touchPointerFire) {
        touchPosFire.setZero();
        touchPointerFire = -1;
    }

    return false;
}



@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}

@Override
public boolean scrolled(int amount) {
    return false;
}
}

and:

    public Dude(Texture spriteSheet, float x, float y, Viewport viewport){
    super(spriteSheet);
    v = viewport;
    setSize(2f,2f);
    setPosition(x, y);
    frameWidth = spriteSheet.getWidth() / frameCountX;
    frameHeight = spriteSheet.getHeight() / frameCountY;

    // add frames to the LEFT animation
    Array<TextureRegion> frames = new Array<TextureRegion>();
    for (int i = 0; i < frameCountX; i++){
        frames.add(new TextureRegion(spriteSheet, i * frameWidth, 0, frameWidth, frameHeight));
    }
    leftAnim = new Animation<TextureRegion>(frameDuration, frames);
    leftAnim.setPlayMode(Animation.PlayMode.LOOP);
    frames.clear();
    // add frames to the RIGHT animation
    for (int i = 0; i < frameCountX; i++){
        frames.add(new TextureRegion(spriteSheet, i * frameWidth, frameHeight, frameWidth, frameHeight));
    }
    rightAnim = new Animation<TextureRegion>(frameDuration, frames);
    rightAnim.setPlayMode(Animation.PlayMode.LOOP);
    frames.clear();
    // add frames to the UP animation
    for (int i = 0; i < frameCountX; i++){
        frames.add(new TextureRegion(spriteSheet, i * frameWidth, 2 * frameHeight, frameWidth, frameHeight));
    }
    upAnim = new Animation<TextureRegion>(frameDuration, frames);
    upAnim.setPlayMode(Animation.PlayMode.LOOP);
    frames.clear();
    // add frames to the DOWN animation
    for (int i = 0; i < frameCountX; i++){
        frames.add(new TextureRegion(spriteSheet, i * frameWidth, 3 * frameHeight, frameWidth, frameHeight));
    }
    downAnim = new Animation<TextureRegion>(frameDuration, frames);
    downAnim.setPlayMode(Animation.PlayMode.LOOP);
    frames.clear();
    currentAnimState = animState.UP;
    setRegion(upAnim.getKeyFrame(stateTime));
}

public void update(float delta){
    if (movementTouch.x != 0) {
        if (getX() >= movementTouch.x) {
            stateTime += delta;
            translateX(-walkSpeed);
        }
        if (getX() <= movementTouch.x) {
            stateTime += delta;
            translateX(walkSpeed);
        }
    }



    switch (currentAnimState){
        case UP:
            setRegion(upAnim.getKeyFrame(stateTime));
    }


    for (Bullet bullet : bullets){
        bullet.update(delta);
    }

}

public void fireBullet(Texture texture, Vector2 targetVector){
    bullets.add(new Bullet(texture, new Vector2(getX(), getY()), targetVector));
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.