Android Studio LibGDX游戏问题

时间:2016-02-27 23:14:01

标签: java android android-studio libgdx

我差不多完成了游戏的编写,然而,当我运行游戏时,Android Studio会抱怨以下错误: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(); } }

以下是我的GameScreen类的代码:

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

我的MainGame类的代码:

// StrByteMap
java.util.HashMap@15327b79d footprint:
     COUNT       AVG       SUM   DESCRIPTION
         1       184       184   [B
    100000        31   3120000   [C

有人可以帮我解决这些错误吗?我将不胜感激!

1 个答案:

答案 0 :(得分:1)

不允许在构造函数中使用其他方法或方法中的方法。在上面或下面的构造函数外部移动代码中看起来像这样的部分。

void find(int[]arr, int tofind){
   for (int i: arr){
        if (arr[i]==toFind){
           System.out.println(tofind+" first occurs at "+i+"th place in the array")
        return
   System.out.println("not found")