2017-03-17 2 views
0

これは私のジレンマです。 GamePadクラス(私はアンドロイドデバイス用のゲームコントローラ用にこのクラスを作った)を使って、稲妻攻撃のアニメーション化に問題があります。Libgdx - touchDown/touchUpとGdx.input.isKeyPressed/Gdx.input.isKeyJustPressed

Gdx.input.isKeyPressed(キー名)を使用すると、コードがフレームごとに重なり合って落雷攻撃を起こすことが分かりました。ですから、私はGdx.input.isKeyJustPressed()を使用し、1つの攻撃を作成し、それがアニメーション化され、完璧です。

GamePadクラスを作成するときに、タッチイベントを処理するリスナーを使用してイメージを作成しました。各ボタンについて、私はtouchDownとtouchUpメソッドを持っています。私のキャラクターを左から右へ走らせるとき、これらのタッチイベントは正常に動作します。しかし、Gdx.input.isKeyPressed()と同様に、落雷攻撃は各フレームごとに作成され、攻撃は互いに重ね合わされます。私は各フレームの攻撃の作成を修正するための回避策を試みましたが、イメージは静的であり、アニメーション化されません。これは60 fpsより優れていますが、私の問題は解決しません。

Gdx.input.isKeyJustPressed()に似たtouchDownイベントがありますか? GamePadクラスをConfigクラスと同じように修正するにはどうすればよいですか?

GamePad.java

public class GamePad implements Disposable{ 

private Viewport viewport; 
public Stage stage; 
boolean leftPressed, rightPressed, pausePressed, aPressed, bReleased, bPressed, bPreviouslyPressed; 
private Config config = Config.getInstance(); 
private Table table; 

public GamePad(){ 
    viewport = new FitViewport(EIUGame.V_WIDTH, EIUGame.V_HEIGHT, new OrthographicCamera()); 
    stage = new Stage(viewport); 

    table = new Table(); 
    table.setFillParent(true); 
    table.bottom(); 

    bPreviouslyPressed = false; 

    // "Left" Button 
    Image leftImg = new Image(new Texture("controller/leftButton.png")); 
    leftImg.setSize(35, 35); 
    leftImg.addListener(new InputListener(){ 

     @Override 
     public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ 
      leftPressed = true; 
      return true; 
     } 


     @Override 
     public void touchUp(InputEvent event, float x, float y, int pointer, int button){ 
      leftPressed = false; 
     } 
    }); 

    // "Right" Button 
    Image rightImg = new Image(new Texture("controller/rightButton.png")); 
    rightImg.setSize(35, 35); 
    rightImg.addListener(new InputListener(){ 

     @Override 
     public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ 
      rightPressed = true; 
      return true; 
     } 

     @Override 
     public void touchUp(InputEvent event, float x, float y, int pointer, int button){ 
      rightPressed = false; 
     } 
    }); 

    // "Pause" Button 
    Image pauseImg = new Image(new Texture("controller/pauseButton.png")); 
    pauseImg.setSize(15, 15); 
    pauseImg.addListener(new InputListener(){ 

     @Override 
     public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ 
      pausePressed = true; 
      return true; 
     } 

     @Override 
     public void touchUp(InputEvent event, float x, float y, int pointer, int button){ 
      pausePressed = false; 
     } 
    }); 

    // "A" Button 
    Image aImg = new Image(new Texture("controller/aButton.png")); 
    aImg.setSize(35, 35); 
    aImg.addListener(new InputListener(){ 

     @Override 
     public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ 
      aPressed = true; 
      return true; 
     } 

     @Override 
     public void touchUp(InputEvent event, float x, float y, int pointer, int button){ 
      aPressed = false; 
     } 
    }); 

    // "B" Button 
    Image bImg = new Image(new Texture("controller/bButton.png")); 
    bImg.setSize(35, 35); 
    bImg.addListener(new InputListener(){ 

     @Override 
     public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ 
      bPressed = true; 
      setBReleased(false); 
      return true; 
     } 

     @Override 
     public void touchUp(InputEvent event, float x, float y, int pointer, int button){ 
      setBReleased(true); 
      bPreviouslyPressed = false; 
      bPressed = false; 
     } 
    }); 

    table.add(leftImg).size(leftImg.getWidth(), leftImg.getHeight()); 
    table.add().size(5,35); 
    table.add(rightImg).size(rightImg.getWidth(), rightImg.getHeight()); 
    table.add().size(100, 35); 
    table.add(pauseImg).size(pauseImg.getWidth(), pauseImg.getHeight()).bottom(); 
    table.add().size(100, 35); 
    table.add(bImg).size(bImg.getWidth(), bImg.getHeight()); 
    table.add().size(5,35); 
    table.add(aImg).size(aImg.getWidth(), aImg.getHeight()); 

    stage.addActor(table); 
} 

// Returns the stage object so that it can be added to a multiplexer 
public Stage getStage() { 
    return stage; 
} 

public void draw(){ 
    stage.draw(); 
} 

public boolean isLeftPressed(){ 
    return leftPressed; 
} 

public boolean isRightPressed(){ 
    return rightPressed; 
} 

public boolean isPausePressed(){ 
    return pausePressed; 
} 

public boolean isAPressed(){ 
    return aPressed; 
} 

public boolean isBPressed(){ 
    return bPressed; 
} 

public boolean isBPreviouslyPressed(){ 
    return bPreviouslyPressed; 
} 

public boolean isBReleased(){ 
    return bReleased; 
} 

public void setBReleased(boolean released){ 
    bReleased = released; 
} 

public void resize(int width, int height){ 
    viewport.update(width, height); 
} 

public void animateChamp(Champion champ, PauseState pause){ 
    // Move Champion Right 
    if (isRightPressed() && champ.b2body.getLinearVelocity().x <= 2) 
     config.runRight(champ); 
    // Move Champion left 
    if (isLeftPressed() && champ.b2body.getLinearVelocity().x >= -2) 
     config.runLeft(champ); 
    // If A button is pressed and we are not jumping or falling, then Jump. 
    if (isAPressed() && (champ.getState() != champState.JUMPING && champ.getState() != champState.FALLING)){ 
     config.jump(champ); 
     aPressed = false; 
    } 
    // Toggle Pause Menu 
    if (isPausePressed()) 
     pause.togglePause(); 

    // Precondition for next else-if statement 
    if (isBPressed() && champ.b2body.getLinearVelocity().x == 0 && champ.b2body.getLinearVelocity().y == 0){ 
     bPressed = false; 
     bPreviouslyPressed = true; 
    } 
    // If b was pressed down but not released, and champion is not moving, use lightning attack 
    else if (bPreviouslyPressed && !isBReleased() && champ.b2body.getLinearVelocity().x == 0 && champ.b2body.getLinearVelocity().y == 0){ 
     champ.setMobileTrigger(true);  // Sets champion state to attacking region 
     config.setMLightningActive(true); 
     config.lightningAttack(champ); 
    } 
    // Exit lightning attack if moved 
    else if (!isBReleased() && (champ.b2body.getLinearVelocity().x != 0 || champ.b2body.getLinearVelocity().y != 0)){ 
     champ.setMobileTrigger(false); 
     config.setMLightningActive(false); 
     bReleased = true; 
    } 
    // Exit lightning attack if button released 
    else if (isBReleased() && config.getMLightningActive()){ 
     champ.setMobileTrigger(false);   // Does not alter champion state 
     config.setMLightningActive(false); 
     bReleased = true; 
    } 
    // Attack when moving 
    else if (isBPressed()){ 
     config.attack(champ); 
     bPressed = false; 
    } 

} 

@Override 
public void dispose(){ 
    stage.dispose(); 
} 
} 

Config.java

public final class Config { 
private static final Config instance = new Config(); 

private int moveLeft; 
private int moveRight;  
private int jump; 
private int attack; 
private String lStr; 
private String rStr; 
private String jStr; 
private String aStr; 
private boolean lightningActive = false; 
private boolean MlightningActive = false; // Mobile Game 

// Default constructor sets the keys to a default schema 
private Config() { 
    moveLeft = Input.Keys.A;  
    moveRight = Input.Keys.D;  
    jump = Input.Keys.L; 
    attack = Input.Keys.J; 
    lStr = "A"; 
    rStr = "D"; 
    jStr = "L"; 
    aStr = "J"; 
} 

// Return the instance of the class 
public static Config getInstance() { 
    return instance; 
} 

public void animateChamp(Champion champ){ 

    // Jump! 
    if(Gdx.input.isKeyJustPressed(jump) && (champ.getState() != champState.JUMPING && champ.getState() != champState.FALLING)) 
     jump(champ); 

    // Run Right (and make sure character is not moving faster than 2) 
    if(Gdx.input.isKeyPressed(moveRight) && champ.b2body.getLinearVelocity().x <= 2) 
     runRight(champ); 

    // Run Left (and make sure character is not moving faster than 2) 
    if(Gdx.input.isKeyPressed(moveLeft) && champ.b2body.getLinearVelocity().x >= -2) 
     runLeft(champ);  

    // Lightning Attack 
    if(Gdx.input.isKeyJustPressed(attack) && champ.b2body.getLinearVelocity().x == 0 && champ.b2body.getLinearVelocity().y == 0){ 
     setLightningActive(true); 
     lightningAttack(champ);   
    } 
    else if (getlightningActive() && (champ.b2body.getLinearVelocity().x != 0 || champ.b2body.getLinearVelocity().y != 0 || !Gdx.input.isKeyPressed(attack))) 
     setLightningActive(false); 

    else if (Gdx.input.isKeyJustPressed(attack)) 
     attack(champ); 
} 

public void runRight(Champion champ){ 
    champ.b2body.applyLinearImpulse(new Vector2(0.1f,0), champ.b2body.getWorldCenter(), true); 
} 

public void runLeft(Champion champ){ 
    champ.b2body.applyLinearImpulse(new Vector2(-0.1f,0), champ.b2body.getWorldCenter(), true); 
} 

public void jump(Champion champ){ 
    champ.b2body.applyLinearImpulse(new Vector2(0, 4.5f), champ.b2body.getWorldCenter(), true); 
} 

public void attack(Champion champ){ 
    champ.attack(); 
} 

public void lightningAttack(Champion champ){ 
    champ.lightningAttack(); 
} 

public boolean getlightningActive(){ 
    return lightningActive; 
} 

public void setLightningActive(boolean value){ 
    lightningActive = value; 
} 

// For Mobile Version 
public boolean getMLightningActive(){ 
    return MlightningActive; 
} 

// For Mobile Version 
public void setMLightningActive(boolean value){ 
    MlightningActive = value; 
} 

// sets the key to move left 
public void setMoveLeft(String n){ 
    moveLeft = Input.Keys.valueOf(n.toUpperCase()); 
    lStr = n; 
} 

// sets the key to move right 
public void setMoveRight(String n) { 
    moveRight = Input.Keys.valueOf(n.toUpperCase()); 
    rStr = n; 
} 

// sets the key to jump 
public void setJump(String n) { 
    jump = Input.Keys.valueOf(n.toUpperCase()); 
    jStr = n; 
} 

// sets the key to attack 
public void setAttack(String n) { 
    attack = Input.Keys.valueOf(n.toUpperCase()); 
    aStr = n; 
} 

// Returns the string representation of the move left key 
public String getMoveLeft(){ 
    return lStr; 
} 

// Returns the string representation of the move right key 
public String getMoveRight() { 
    return rStr; 
} 

// Returns the string representation of the jump key 
public String getJump() { 
    return jStr; 
} 

// Returns the string representation of the attack key 
public String getAttack() { 
    return aStr; 
} 

// Returns the int representation of the attack key 
public int getAttackInt(){ 
    return attack; 
} 
} 

答えて

0

私は、あなたが本当にしたいことはattack-あなたの稲妻にクールダウンを適用することがあるように聞こえる、別のアプローチを提案する可能性がある場合プレイヤーがボタンを押し下げたままにしておくと、クールダウンは繰り返し発砲することがなく、プレイヤーがキーボードを叩いたときにプレイヤーがどれくらい速く発射できるかを制御できます。将来は)。どこかgameloopで次に

this.lighteningAttack = new Cooldown(0.25f, new LightningAttack()); 

public class Cooldown { 
    private float cooldownTime = 0; 
    private float length = 0; 
    private Action action; 

    public Cooldown(float length, Action action) { 
     this.length = length; 
     this.action = action; 
    } 

    public boolean update(float delta) { 
     // Subtract the delta until we hit 0 
     this.cooldownTime = this.cooldownTime - delta <= 0 ? 0 : this.cooldownTime - delta; 
     // The boolean tells you that the cooldown has expired- useful for special effects 
     return this.cooldownTime == 0; 
    } 

    public void execute() { 
     if(this.cooldownTime > 0) return; 
     this.cooldownTime = this.length; 
     this.action.execute(); 
    } 

    public interface Action { 
     void execute(); 
    } 
} 

は、単にどこかにプレーヤーのコンストラクタの下に次のようなものを追加し、このクラスを使用するには:

は、私は以下の例一緒に投げました
this.lighteningAttack.update(deltaTime); 

最後に、イベントリスナーに:

this.lighteningAttack.execute(); 

ボタンを何回何回押しても、1秒に4回だけ実行する必要があります。

+0

これは興味深いと思いますが、私は時間を見つけたら試してみます。私はこのアプローチを私の基本的な攻撃に使うこともできます。返事をありがとう、私はあなたに戻ってきます。 – FoxDonut