2016-10-03 5 views
1

1/解像度および方法は/Ubuntuでlibgdxでbox2dを使用しており、私はチュートリアルをオンラインで続けてきました。重力は

 public class Flappy2 extends ApplicationAdapter { 

      public static final int WIDTH = 480; 
      public static final int HEIGHT = 800; 

      public static final String TITLE = "Flappy 2"; 
      private GameStateManager gsm; 


      private SpriteBatch batch; 


      @Override 
      public void create() { 
       batch = new SpriteBatch(); 
       gsm = new GameStateManager(); 
       Gdx.gl.glClearColor(1, 0, 0, 1); 
       gsm.push(new MenuState(gsm)); 
      } 

      @Override 
      public void render() { 

       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
       gsm.update(Gdx.graphics.getDeltaTime()); //update method 
       gsm.render(batch); 
      } 

      @Override 
      public void dispose() { 
       batch.dispose(); 

      } 
     } 

// Bird.class

 public class Bird { 
      private static final int GRAVITY = -15; 
      private Vector3 position; 
      private Vector3 velocity; 
      private Texture bird; 

      public Bird(int x, int y){ 
       position = new Vector3(x,y,0); 
       velocity = new Vector3(0,0,0); 
       bird = new Texture("bird.png"); 
      } 

      public void update(float dt){ 
       velocity.add(0, GRAVITY, 0); 
       velocity.scl(dt); 
       velocity.add(0, velocity.y, 0); 
       velocity.scl(1/dt); 

      } 

      public Texture getTexture() { 
       return bird; 
      } 

      public Vector3 getPosition() { 
       return position; 
      } 

      public void jump(){ 
       velocity.y = 250; //the on-click jump 
      } 
     } 

// GameStateManager.class

を初期化しているメインクラスを動作するようには思えません
 public class GameStateManager { 

      private Stack<State> states; 
      public GameStateManager(){ 
       states = new Stack<State>(); 
      } 

      public void push(State state){ 
       states.push(state); 
      } 

      public void pop(){ 
       states.pop(); 
      } 

      public void set(State state){ 
       states.pop(); 
       states.push(state); 
      } 

      public void update(float dt){ 
       states.peek().update(dt); 
      } 

      public void render(SpriteBatch sb){ 
       states.peek().render(sb); 
      } 


     } 

2./*メニュー状態が正常に動作しています*/

(メニュー状態の後)
 public class MenuState extends State { 
      private Texture background; 
      private Texture playBtn; 

      public MenuState(GameStateManager gsm) { 
       super(gsm); 
       background = new Texture("bg.jpg"); 
       playBtn = new Texture("playbtn.png"); /*menu state button*/ 
      } 

      @Override 
      public void handleInput() { 
       if (Gdx.input.justTouched()) { /*input from user*/ 
        gsm.set(new PlayState(gsm)); 
        dispose(); 
        } 
      } 

      @Override 
      public void update(float dt) { 
       handleInput(); 

      } 

      @Override 
      public void dispose() { 
       background.dispose(); 
       playBtn.dispose(); 
      } 

      @Override 
      public void render(SpriteBatch sb) { 
       sb.begin(); 
       sb.draw(background,0,0, Flappy2.WIDTH, Flappy2.HEIGHT); 
       sb.draw(playBtn, (Flappy2.WIDTH/2) - (playBtn.getWidth()/2), Flappy2.HEIGHT/2); 
       sb.end(); 
      } 
     } 

3/プレイ状態クラス/

 public class PlayState extends State { 
        private Bird bird; 


      public PlayState(GameStateManager gsm) { 
       super(gsm); 
       bird = new Bird(50,300); 
       cam.setToOrtho(false, Flappy2.WIDTH/2, Flappy2.HEIGHT/2); 
      } 

      @Override 
      protected void handleInput() { 
       if(Gdx.input.justTouched()){ /*taking input from user*/ 
        bird.jump(); 
       } 
      } 

      @Override 
      public void update(float dt) { 
       handleInput(); 
       bird.update(dt); 
      } 

      @Override 
      public void render(SpriteBatch sb) { 
       sb.setProjectionMatrix(cam.combined); 
       sb.begin(); 
       sb.draw(bird.getTexture(), bird.getPosition().x, bird.getPosition().y); 
       sb.end(); 

      } 

      @Override 
      public void dispose() { 

      } 
     } 

/4。抽象Stateクラス/

 public abstract class State { 
      protected OrthographicCamera cam; 
      protected Vector3 mouse; 
      protected GameStateManager gsm; 

      protected State(GameStateManager gsm){ 
       this.gsm = gsm; 
       cam   = new OrthographicCamera(); /*Camera init*/ 
       mouse  = new Vector3();/*vector3 object initialised*/ 
      } 

      protected abstract void handleInput();/*method handles i/p*/ 
      public abstract void update(float dt);/*takes a deltatime argument*/ 
      public abstract void render(SpriteBatch sb); 
      public abstract void dispose(); 
     } 

    /* Build.Gradle */ /*build gradle file */ 

    buildscript { 
     repositories { 
      mavenLocal() 
      mavenCentral() 
      maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } 
      jcenter() 
     } 
     dependencies { 
      classpath 'com.android.tools.build:gradle:1.5.0' 
     } 
    } 

    allprojects { 
     apply plugin: "eclipse" 
     apply plugin: "idea" 

     version = '1.0' 
     ext { 
      appName = "Flappy2" 
      gdxVersion = '1.9.4' 
      roboVMVersion = '2.2.0' 
      box2DLightsVersion = '1.4' 
      ashleyVersion = '1.7.0' 
      aiVersion = '1.8.0' 
     } 

     repositories { 
      mavenLocal() 
      mavenCentral() 
      maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } 
      maven { url "https://oss.sonatype.org/content/repositories/releases/" } 
     } 
    } 

    project(":desktop") { 
     apply plugin: "java" 


     dependencies { 
      compile project(":core") 
      compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion" 
      compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" 
      compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop" 
     } 
    } 

    project(":android") { 
     apply plugin: "android" 

     configurations { natives } 

     dependencies { 
      compile project(":core") 
      compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" 
      natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi" 
      natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a" 
      natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a" 
      natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86" 
      natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64" 
      compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" 
      natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi" 
      natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a" 
      natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a" 
      natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86" 
      natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64" 
     } 
    } 

    project(":core") { 
     apply plugin: "java" 


     dependencies { 
      compile "com.badlogicgames.gdx:gdx:$gdxVersion" 
      compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" 
     } 
    } 

    tasks.eclipse.doLast { 
     delete ".project" 
    } 

答えて

0

私が見ることができるものから、鳥の位置に速度が適用されていません。あなたはbird.getPosition()に鳥を描画していますが、positionは常に(50,300)です。

計算された速度をBird's updateメソッドの位置に追加します。

position.add(velocity); 
+0

はいこれが解決策です。ありがとう。 –

+0

しかし、velocity.y = 5に設定しても、鳥が画面から飛び散ってしまいます。 –

+0

速度よりもむしろ重力の値を持っています。 –

関連する問題