使用spawnTime Android Studio的LibGDX问题

时间:2016-02-26 18:43:50

标签: java android android-studio libgdx google-play

我试图在这个游戏中重新创造障碍我几乎要完成创作,但是,Android Studio抱怨了一系列新的错误:

Error:(133, 4) Gradle: error: illegal start of expression
Error:(133, 12) Gradle: error: illegal start of expression
Error:(133, 28) Gradle: error: ';' expected
Error:(136, 19) Gradle: error: ';' expected
Error:(170, 22) Gradle: error: ';' expected
Error:(170, 34) Gradle: error: ';' expected
Error:(184, 23) Gradle: error: ';' expected
Error:(188, 1) Gradle: error: reached end of file while parsing

我应该创建一个SpawnEntity类吗?这是我的MainGame类的代码:

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

            }
        });
    }

    @Override
    public void show() {
        Texture playerTexture = game.getManager().get("player.png");
        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");


        player = new PlayerEntity(world, playerTexture, new Vector2(1, 2));


        for (FloorEntity floor : floorList) {

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

            stage.addActor(obstacle);


            stage.addActor(player);
        }
        for (ObstacleEntity2 obstacle2 : obstacle2List) {
            stage.addActor(obstacle2);
        }

    }


    public void update(float deltaTime) {
        Texture playerTexture = game.getManager().get("player.png");
        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");

        float timer = 0;
        float spawnTime = 4f;
    private void spawnEntity();
    {
        //Increment timer by the duration since the previous frame
        float timer += Gdx.graphics.getRawDeltaTime();
        //Compare to spawntime
        if (timer >= (float) spawnTime)
        {
            //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, floorTexture, overfloorTexture, overfloor2Texture ,25, 10 ,20));
            //But you will probably want to spawn something on the right, just outside of your screen view.

            //This is the right side of your vp in the world. Depending how you draw you can add some more to it.
            float spawnX = camera.position.x + camera.viewportWidth / 2;
            //Then use this to spawn your object, since you hardcoded stuff I have no idea where to put it.

            //Now reset timer
            timer-= spawnTime;

            //And perhaps randomize the spawnTime? (between 2 and 4 seconds)
            Random random;
            spawnTime = random.nextFloat() * 2 + 2;
        }
    }






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

我没有SpawnEntity类,我应该创建它吗?我该怎么做才能解决这个问题?请帮助我!!

1 个答案:

答案 0 :(得分:0)

您的代码存在许多问题。

首先,由于你没有关闭更新功能,你会收到错误,你不能把一个函数放在另一个函数中,但你可以从另一个函数中调用一个函数,如下所示: / p>

public void update(float deltaTime) {
    spawnEntity();
}

将发生的另一个错误是您从未创建过Random对象,因此从中调用方法将导致 NullPointerException 。为避免这种情况,您需要在某处实例化Random对象:

Random random = new Random();
// ...
spawnTime = random.nextFloat() * 2f + 2f;

请注意,出于性能原因,最好不要在更新循环中实例化它,因为它每秒调用多次。

您也可以简单地使用LibGDX的方法来实现相同的结果:

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

另一个错误是您的FloorList永远不会添加任何楼层实体,因为您总是将计时器重置为更新方法中的 ,因此它们很可能永远不会达到4.正确的方法是将spawnTime设置为属性(使用您的舞台,世界和玩家)。

固定代码可能如下所示:

import com.badlogic.gdx.math.MathUtils;

// ... define your class ...

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

// ...


public void update(float deltaTime) {
    // Increment timer by the duration since the previous frame
    timer += deltaTime;

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

        // the next spawn time will be triggered between 2 and 4 seconds :
        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");

    //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, floorTexture, overfloorTexture, overfloor2Texture, 25, 10 ,20));
}

至于您的类创建问题,您可以根据需要创建一个SpawnEntity类,但如您所见,它不是必需的:这取决于您以及您希望如何管理代码。但这当然是一个明智的选择,因为在代码增长时管理所有内容会更容易。