【问题标题】:trying to make my (very simple) libgdx game more object-oriented. added GameObject class - game doesn't launch anymore试图让我的(非常简单的)libgdx 游戏更加面向对象。添加了 GameObject 类 - 游戏不再启动
【发布时间】:2014-11-09 22:04:01
【问题描述】:

标题所说的。

首先,这是我的 3 个课程:

MyGdxGame

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Sprite sp; 
    Texture img;
    int width;
    int height; 
    Player p;

    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("dildo.png");
        sp = new Sprite(img); 
        width = Gdx.graphics.getWidth();
        height = Gdx.graphics.getHeight();
        p = new Player(img);
        Gdx.input.setInputProcessor(p);

    }

    @Override
    public void render () {
        //sp.flip(true, false);
        p.update();
        Gdx.gl.glClearColor(255,255,255,255);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        //batch.draw(img, 0, height/6, -img.getWidth(), img.getHeight());
        p.draw(batch);
        //sp.draw(batch); 
        batch.end();
    }
    }

播放器

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.TimeUtils;

public class Player extends GameObject implements InputProcessor {

    private Sprite sp;
    private Texture te;
    private float xPos;
    private float yPos;
    private float xSpeed;
    private float ySpeed;
    private float accelerationX;
    private float g;
    Vector2 position;
    private float accelerationJump = 9f;
    private boolean allowJump = true;
    private boolean isDragged = false;

        public Player(Texture sprite) {
        xPos = 0;
        yPos = (super.getHeight()/6);
        xSpeed = 5.5f;
        ySpeed = 0;
        te = sprite;
        sp.setX(xPos);
        sp.setY(yPos);
        g = 0.2f;
        accelerationX=0.02f;

    }

    public void update() {
        ySpeed -= g;

        if(isDragged) {
            xSpeed += accelerationX;
            moveBy(xSpeed,0);
            isDragged = false;
        }

        if (getxPos() > super.getWidth()) {
            setxPos(0);
        } else {
            moveBy(xSpeed, ySpeed);
        }

        if (getyPos() < super.getHeight() / 6) {
            moveTo(sp.getX(), getHeight() / 6);
        }
        if (onGround()) {
            allowJump = true;
            ySpeed = 0;
        }
    }

    public void jump() {
        ySpeed += accelerationJump;
    }

    public boolean onGround() {
        return (sp.getY() == super.getHeight()/6);
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {

        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        if (allowJump && isDragged == false) {
            allowJump = false;
            jump(); 
        }
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        if(isDragged==false) {
        isDragged = true;
        }
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}

游戏对象

package com.mygdx.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public abstract class GameObject {

    private Sprite sp;

    private int width;

    private int height;

    private float xPos;

    private float yPos;

    private float xSpeed;

    private float ySpeed;

    public GameObject(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public GameObject() {

    }

    public void moveTo(float xPos2, float yPos2) {
        setxPos(xPos2);
        setyPos(yPos2);
    }

    public void moveBy(float dx, float dy) {
        sp.setY(sp.getY() + dy);
        sp.setX(sp.getX() + dx);
    }

    public float getxPos() {
        return xPos;
    }

    public float getyPos() {
        return yPos;
    }

    public void setxPos(float xPos2) {
        sp.setX(xPos2);
    }

    public void setyPos(float yPos) {
        sp.setY(yPos);
    }

    public float getxSpeed() {
        return xSpeed;
    }

    public float getySpeed() {
        return ySpeed;
    }

    public void setxSpeed(float xSpeed2) {
        xSpeed = xSpeed2;
    }

    public void setySpeed(float ySpeed2) {
        ySpeed = ySpeed2;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height
    }

    public void draw(SpriteBatch batch) {
        sp.draw(batch);
    }

}

基本上,现在的游戏只是一个从屏幕左到右移动的精灵 - 当您点击时,它会跳跃,当您拖动屏幕时,它会向前冲刺。我现在想开始添加诸如加电和平台之类的东西,所以我认为创建一个 GameObject 类是个好主意。但是现在当我运行它时屏幕变黑并且应用程序崩溃了,我感觉它与我初始化纹理的方式有关?有什么想法吗?

任何帮助都非常感谢,非常感谢提前:)

编辑:在GameObject构造函数中width和height是屏幕的宽度和高度

【问题讨论】:

    标签: java android object libgdx 2d


    【解决方案1】:

    如果你的图片在android项目的asset文件夹中,测试img = new Texture (Gdx.files.internal ("dildo.png"));

    示例: AdroidProyect->Asset->YourFile.png

    我认为在播放器中 1-

    public Player(Texture sprite) {
            xPos = 0;
            yPos = (super.getHeight()/6);
            xSpeed = 5.5f;
            ySpeed = 0;
            te = sprite;
    

    //添加并重构你的代码,如果它有效

      sp = new Sprite(sprite);
    
    
    
    
            sp.setX(xPos);
            sp.setY(yPos);
            g = 0.2f;
            accelerationX=0.02f;
    
        }
    

    或 2- 我不想做,但在你的 MyGdxGame 中,你的 你说,

    ..//
        img = new Texture ("dildo.png");
                 sp = new Sprite (img);
                 p = new Player (img);
    ..//
    

    但是在你给玩家一个纹理之后,因为没有精灵或者正如我在初始化玩家之前所说的,你也可以改变玩家的构造函数。

    public Player(Sprite sprite){t
    ..//
    this.sp = sprite;
    

    添加新的:

    一定看过你的代码,没有冒犯,你的代码有点疯狂:)

    变量的可见性有点令人困惑,因为你想要什么但没有很好地测试并查看访问修饰符,如果它们与你想要做的匹配。 我的英语也有点疯狂。

    在你的班级 MyGdxGame; ..//

    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture(Gdx.files.internal("dildo.png"));
        sp = new Sprite(img); 
        width = Gdx.graphics.getWidth();
        height = Gdx.graphics.getHeight();
        p = new Player(sp);
        Gdx.input.setInputProcessor(p);
    
    }
    

    ..//

    类游戏对象更改;

    public abstract class GameObject {
    
    protected Sprite sp;
    
    protected int width;
    
    protected int height;
    
    protected float xPos;
    
    protected float yPos;
    
    protected float xSpeed;
    
    protected float ySpeed;
    

    玩家类。

    public class Player extends GameObject implements InputProcessor {
    
    //private Sprite sp;   remove
    private Texture te;   
    //private float xPos; remove
    //private float yPos; remove
    //private float xSpeed; remove
    //private float ySpeed; remove
    private float accelerationX;
    private float g;
    Vector2 position;
    private float accelerationJump = 9f;
    private boolean allowJump = true;
    private boolean isDragged = false;
    
        public Player(Sprite sprite) {
        ..//
        sp = sprite;
    

    【讨论】:

      猜你喜欢
      • 2019-05-11
      • 2023-04-09
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多