2016-02-27 129 views
2

但是,当我运行我的游戏时,我几乎完成写作游戏,Android Studio抱怨出现以下错误:Error:(103, 8) Gradle: error: illegal start of expressionError:(103, 15) Gradle: error: illegal start of expressionError:(103, 26) Gradle: error: ';' expectedError:(103, 43) Gradle: error: ';' expectedAndroid Studio LibGDX游戏问题

下面是我的GameScreen类的代码:

package com.circlecrashavoider; 



     import com.badlogic.gdx.Gdx; 
     import com.badlogic.gdx.graphics.GL20; 
     import com.badlogic.gdx.graphics.Texture; 
     import com.badlogic.gdx.math.MathUtils; 
     import com.badlogic.gdx.math.Vector2; 
     import com.badlogic.gdx.physics.box2d.Contact; 
     import com.badlogic.gdx.physics.box2d.ContactImpulse; 
     import com.badlogic.gdx.physics.box2d.ContactListener; 
     import com.badlogic.gdx.physics.box2d.Manifold; 
     import com.badlogic.gdx.physics.box2d.World; 
     import com.badlogic.gdx.scenes.scene2d.Stage; 
     import com.badlogic.gdx.utils.viewport.FitViewport; 
     import com.circlecrashavoider.entities.FloorEntity; 
     import com.circlecrashavoider.entities.ObstacleEntity; 
     import com.circlecrashavoider.entities.ObstacleEntity2; 
     import com.circlecrashavoider.entities.PlayerEntity; 
     import com.circlecrashavoider.scene2d.EntityFactory; 

     import java.util.ArrayList; 
     import java.util.List; 
     import java.util.Objects; 
     import java.util.Random; 

/** 
* Created by Felipe on 2/22/2016. 
*/ 

public class GameScreen extends BaseScreen { 

    private Stage stage; 

    private World world; 

    private PlayerEntity player; 

    private List<FloorEntity> floorList = new ArrayList<FloorEntity>(); 

    private List<ObstacleEntity> obstacleList = new ArrayList<ObstacleEntity>(); 

    private List<ObstacleEntity2> obstacle2List = new ArrayList<ObstacleEntity2>(); 

    public GameScreen(MainGame game) { 
     super(game); 
     stage = new Stage(new FitViewport(1024, 620)); 
     world = new World(new Vector2(0, -10), true); 


     world.setContactListener(new ContactListener() { 

      private boolean areCollided(Contact contact, Object userA, Object userB) { 
       return (contact.getFixtureA().getUserData().equals(userA) && contact.getFixtureB().getUserData().equals(userB)) || 
         (contact.getFixtureA().getUserData().equals(userB) && contact.getFixtureB().getUserData().equals(userA)); 
      } 

      @Override 
      public void beginContact(Contact contact) { 
       if (areCollided(contact, "player", "floor")) { 
        player.setJumping(false); 
        if (Gdx.input.isTouched()) { 
         player.setMustJump(true); 
        } 
       } 

       if (areCollided(contact, "player", "obstacle")) { 
        player.setAlive(false); 
        System.out.println("GAME OVER"); 

       } 

       if (areCollided(contact, "player", "obstacle2")) { 
        player.setAlive(false); 
        System.out.println("GAME OVER"); 
       } 
      } 

      @Override 
      public void endContact(Contact contact) { 

      } 

      @Override 
      public void preSolve(Contact contact, Manifold oldManifold) { 

      } 

      @Override 
      public void postSolve(Contact contact, ContactImpulse impulse) { 

      } 
     }); 
    } 

    private float spawnTime = 4f; 
    private float timer = 0; 

    @Override 
    public void show() { 


     public void update(float deltaTime) { 
      timer += deltaTime; 


      if (timer >= spawnTime) { 
       this.spawnEntity(); 

       spawnTime = MathUtils.random(2f, 4f); 
       timer = 0; 

      } 
     } 
    } 
     private void spawnEntity(){ 

      Texture floorTexture = game.getManager().get("floor.png"); 
      Texture overfloorTexture = game.getManager().get("overfloor.png"); 
      Texture overfloor2Texture = game.getManager().get("overfloor2.png"); 
      Texture obstacleTexture = game.getManager().get("obstacle.png"); 
      Texture obstacle2Texture = game.getManager().get("obstacle2.png"); 
      //Spawn your object 
      floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture, 0, 1000, 1)); 
      floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,8, 10 ,5)); 
      floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,10, 10 ,8)); 
      floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,34 , 3 ,5)); 
      floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,19 , 8 ,4)); 
      floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,24 , 8 ,1.5f)); 
      floorList.add(new FloorEntity(world, floorTexture, overfloorTexture,overfloor2Texture ,27 , 5 , 2)); 
      obstacleList.add(new ObstacleEntity(world, obstacleTexture, 6, 1)); 

      stage.addActor(player); 
      for (FloorEntity floor: floorList) { 
       stage.addActor(floor); 
      } 
      for (ObstacleEntity obstacle : obstacleList) { 
       stage.addActor(obstacle); 
      } 
     } 

















    @Override 
    public void render(float delta) { 
     Gdx.gl20.glClearColor(0.5f, 0.6f, 1, 3f); 
     Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); 



     stage.act(); 
     world.step(delta, 6, 2); 
     stage.draw(); 
    } 



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

我MainGame类的代码:

package com.circlecrashavoider; 

import com.badlogic.gdx.Game; 
import com.badlogic.gdx.assets.AssetManager; 
import com.badlogic.gdx.graphics.Texture; 
import com.circlecrashavoider.scene2d.Box2DScreen; 
import com.circlecrashavoider.scene2d.Scene2DScreen; 

public class MainGame extends Game { 

    private AssetManager manager; 

    public AssetManager getManager() { 
     return manager; 
    } 

    @Override 
    public void create() { 
     manager = new AssetManager(); 
     manager.load("floor.png", Texture.class); 
     manager.load("overfloor.png", Texture.class); 
     manager.load("overfloor2.png", Texture.class); 
     manager.load("obstacle.png", Texture.class); 
     manager.load("obstacle2.png", Texture.class); 
     manager.load("crash.png", Texture.class); 
     manager.load("player.png", Texture.class); 
     manager.finishLoading(); 

     setScreen(new GameScreen(this)); 
    } 
} 

可能有人给我帮助上修复这些错误?我将不胜感激!

+0

您声明函数中的函数。这不是有效的java,我不知道你期望它做什么。 – njzk2

+0

也阅读你的错误,它会告诉你哪一行是问题所在。 – njzk2

回答

1

您不允许在构造函数中使用其他方法或方法。将上面或下面的代码部分移到构造函数外部。

@Override 
    public void beginContact(Contact contact) { 
     if (areCollided(contact, "player", "floor")) { 
      player.setJumping(false); 
      if (Gdx.input.isTouched()) { 
       player.setMustJump(true); 
      } 
     } 

     if (areCollided(contact, "player", "obstacle")) { 
      player.setAlive(false); 
      System.out.println("GAME OVER"); 

     } 

     if (areCollided(contact, "player", "obstacle2")) { 
      player.setAlive(false); 
      System.out.println("GAME OVER"); 
     } 
    } 

    @Override 
    public void endContact(Contact contact) { 

    } 

    @Override 
    public void preSolve(Contact contact, Manifold oldManifold) { 

    } 

    @Override 
    public void postSolve(Contact contact, ContactImpulse impulse) { 

    } 
});