2016-10-03 2 views
1

1/해상도 및 방법/우분투에서 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)입니다.

계산 된 속도를 새의 업데이트 방법에서 위치에 추가합니다.

position.add(velocity); 
+0

예. 해결책입니다. 감사. –

+0

하지만 이제 velocity.y = 5로 설정하더라도 새가 화면에서 날아갑니다. –

+0

속도가 아닌 중력 값으로 뒤죽박죽. –

관련 문제