LibGDX独立移动矩形

时间:2016-08-02 18:00:27

标签: libgdx 2d-games

    public class gameMain implements Screen {
    final main game;



    SpriteBatch batch;
    Texture img;
    private Texture alexTexture;
    private Rectangle alex;

    private Texture cTex;
    private Texture dropper;
    private Texture ender;
    private Texture partsImg;
    private Texture toy;

    private OrthographicCamera camera;
    private Array<Rectangle> part;       // ******
    private long lastDropTime;
    private int beltSpeed = 100;         // ******

    //Score Keeper
    private int score;
    private String scoreName;



    //basically a create method
    public gameMain(final main gam){
        this.game = gam;

        // load images into memory
        dropper = new Texture("android/assets/dropper.png");
        ender = new Texture("android/assets/endOfBelt.png");
        partsImg = new Texture("android/assets/unmadeToyParts.png");
        toy = new Texture("android/assets/toymade.png");
        cTex = new Texture("android/assets/conveyerBeltBackground.png");
        alexTexture = new Texture(Gdx.files.internal("android/assets/alexAlpha2.png"));

        // set the game window resolution
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 1080,720);

        // Alex's position / hitbox
        alex = new Rectangle();
        alex.x = 250;
        alex.y = 150;
        alex.width = 126;
        alex.height = 75;

        // part stuff
        part = new Array<Rectangle>();
        spawnPart();

        // score handler
        score = 0;
        scoreName = "Toys Made: ";
    }

    private void spawnPart(){             // *******
        Rectangle parts = new Rectangle();
        parts.x = 0;
        parts.y = 210;
        parts.width = 52;
        parts.height = 60;
        part.add(parts);
        lastDropTime = TimeUtils.nanoTime();
    }                                          //********

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();

        // load alex sprite
        game.batch.setProjectionMatrix(camera.combined);
        // begin drawing to screen
        game.batch.begin();
        batch.draw(cTex, -5, 200); // draw conveyer background
        batch.draw(dropper, 0, 210); // draw the dropper
        batch.draw(ender, 600, 200); // draw the ender
        game.batch.draw(alexTexture, alex.x, alex.y); // draw alex
        for(Rectangle parts: part){
            game.batch.draw(partsImg, parts.x, parts.y); // draw part
        }
        game.font.draw(game.batch, scoreName, 25, 100); // draw scoreboard
        game.batch.end(); // end drawing to screen

       //******************************************************************************
//HERE IS WHERE I AM TRYING TO PUT IT

part.x += beltspeed;
if (part.rectangle overlaps ender.rectangle){
despawn part;
}

//******************************************************************************



    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void dispose() {

    }
}

您好我是LibGDX的新手,在做了一些研究后,我决定制作一个游戏,你是一个名叫亚历克斯的工厂工人,你制造玩具。我很乐意接受您的任何反馈,以使我的代码更加专业化或更好地工作。我用一些星号突出了我认为与我的问题相关的所有内容。

但我的问题是:你如何制作一个长方形&#39;比如我的部分&#39;独自行动。我添加了一个皮带速度和一个产卵区,一旦它的命中箱越过了'ender&#39; hitbox我想要消灭它。

此外,我还没有达到这个目标,但是当这部分&#39;部分&#39; hitbox传递了Alex&#39; hitbox我想要一个小小的名字出现在你必须向上,向下,向左,向右(随机)按下以构建玩具的地方。如果您对我应该如何处理有任何建议,我将非常感激。

"C:\Program Files\Java\jdk1.7.0_79\jre\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;C:\Users\RAFiredog\Desktop\Intellij\AlexTheTemp\out\production\desktop;C:\Users\RAFiredog\Desktop\Intellij\AlexTheTemp\out\production\core;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx\1.9.2\e641cb91bec06bc64d9ddf7a7d7062caceec73cd\gdx-1.9.2.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-box2d\1.9.2\b78eaa90aaaf7830e6dffff587ea6e859c2787b2\gdx-box2d-1.9.2.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-freetype\1.9.2\3609253d14edb1b3ca5aacff4e06989edde75be4\gdx-freetype-1.9.2.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-backend-lwjgl\1.9.2\d710a8704ed584ec2ba9ca52e3a6a5885dd759cc\gdx-backend-lwjgl-1.9.2.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-platform\1.9.2\dde4cf7e9ce61c24042f512203aed87657496639\gdx-platform-1.9.2-natives-desktop.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-box2d-platform\1.9.2\497cbfbf81e7e8b1e8433b19291c5af4bbc2ec66\gdx-box2d-platform-1.9.2-natives-desktop.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-tools\1.9.2\37d8e3009ae0febdf93717b21980c67c8cadac07\gdx-tools-1.9.2.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-freetype-platform\1.9.2\1e1afb0b6c9e8aeb7d495cb8f55c353d3da58cf0\gdx-freetype-platform-1.9.2-natives-desktop.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\org.lwjgl.lwjgl\lwjgl\2.9.2\a9d80fe5935c7a9149f6584d9777cfd471f65489\lwjgl-2.9.2.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\org.lwjgl.lwjgl\lwjgl_util\2.9.2\4b9e37300a87799856e0bd15ed81663cdb6b0947\lwjgl_util-2.9.2.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.jlayer\jlayer\1.0.1-gdx\7cca83cec5c1b2f011362f4d85aabd71a73b049d\jlayer-1.0.1-gdx.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\org.jcraft\jorbis\0.0.17\8872d22b293e8f5d7d56ff92be966e6dc28ebdc6\jorbis-0.0.17.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\com.badlogicgames.gdx\gdx-backend-headless\1.9.2\813e6020de85cd831f02cb5e9060fd7ea8cb208e\gdx-backend-headless-1.9.2.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\org.lwjgl.lwjgl\lwjgl-platform\2.9.2\510c7d317f5e9e700b9cfaac5fd38bdebf0702e0\lwjgl-platform-2.9.2-natives-windows.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\org.lwjgl.lwjgl\lwjgl-platform\2.9.2\d276cdf61fe2b516c7b7f4aa1b8dea91dbdc8d56\lwjgl-platform-2.9.2-natives-linux.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\org.lwjgl.lwjgl\lwjgl-platform\2.9.2\d55b46b40b40249d627a83a7f7f22649709d70c3\lwjgl-platform-2.9.2-natives-osx.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\net.java.jinput\jinput\2.0.5\39c7796b469a600f72380316f6b1f11db6c2c7c4\jinput-2.0.5.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\net.java.jutils\jutils\1.0.0\e12fe1fda814bd348c1579329c86943d2cd3c6a6\jutils-1.0.0.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\net.java.jinput\jinput-platform\2.0.5\7ff832a6eb9ab6a767f1ade2b548092d0fa64795\jinput-platform-2.0.5-natives-linux.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\net.java.jinput\jinput-platform\2.0.5\385ee093e01f587f30ee1c8a2ee7d408fd732e16\jinput-platform-2.0.5-natives-windows.jar;C:\Users\RAFiredog\.gradle\caches\modules-2\files-2.1\net.java.jinput\jinput-platform\2.0.5\53f9c919f34d2ca9de8c51fc4e1e8282029a9232\jinput-platform-2.0.5-natives-osx.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.rafiredog.game.desktop.DesktopLauncher
Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.rafiredog.game.gameMain.render(gameMain.java:97)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.rafiredog.game.main.render(main.java:45)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

Process finished with exit code 0

最后,我现在收到此错误。我在'batch.draw&#39;附近移动了一些代码。但是,这可能不是错误的原因?我现在有点失落。但是,一旦错误得到解决,我很乐意截现我现在对游戏的看法。

感谢您对修复此游戏的任何见解。

2 个答案:

答案 0 :(得分:2)

您需要进行游戏更新,然后在游戏循环的每个周期进行渲染。代码的游戏更新部分可以迭代您的对象来为它们做些事情。

例如,您的渲染方法看起来像这样。您需要为for循环显式使用Iterator,因为否则在迭代时无法从列表中删除项目。

@Override
public void render(float delta) {

    //Update game
    Iterator<Rectangle> iterator = parts.iterator();
    while (iterator.hasNext()){
        Rectangle part = iterator.next();
        part.x += beltSpeed * delta; //distance = speed * time
        if (part.rectangle.overlaps(ender.rectangle)){
            iterator.remove(); //removes rectangle from the list
        }
    }
    //This is also where you would update movement for anything else,
    //such as a character jumping, or counting down a timer and spawning
    //something when it reaches zero, for example.

    //Draw everything
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    game.batch.setProjectionMatrix(camera.combined);
    game.batch.begin();
    //...
    game.batch.end();
}

如果你产生并“消灭”了很多对象,你可能需要使用池来避免因内存释放造成的口吃。您可以将new Rectangle()替换为Pools.obtain(Rectangle.class)并在Pools.free(part)之后添加行iterator.remove()来执行此操作。

我还注意到你通过交换复数和单数来反对直观地使用“部分”和“部分”这两个词。确保不会绊倒你。

至于你的NullPointerException,read here

答案 1 :(得分:0)

小猪支持Tenfour04的回答并试图在评论中回答你的问题。我建议查看以下链接:https://github.com/libgdx/libgdx/wiki/The-life-cycle