Helero Dias
Helero Dias

Reputation: 21

Android Studio LibGDX Game problems

I'm almost finishing writing the game, however, when I run my game, Android Studio complains with the following errors: 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: ';' expected.

Here's the code for my GameScreen class:

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();
    }
}

The code for my MainGame class:

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));
    }
}

Could somebody give me assistance on fixing these errors? I would greatly appreciate it!

Upvotes: 2

Views: 200

Answers (1)

Gregory Saldanha
Gregory Saldanha

Reputation: 106

You aren't allowed to have methods in other methods or methods in a constructor. Move the part of your code that looks like this outside the constructor either above or below.

    @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) {

    }
});

Upvotes: 1

Related Questions