试图让我的(非常简单的)libgdx游戏更加面向对象。添加了GameObject类 - 游戏不再启动了

时间:2014-11-09 12:39:42

标签: java android object libgdx 2d

标题是什么。

首先,这是我的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);
    }

}

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

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

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

1 个答案:

答案 0 :(得分:0)

如果您的图像位于android项目的资产文件夹中,请测试img = new Texture (Gdx.files.internal ("dildo.png"));

例如: AdroidProyect-&GT;资产 - &GT; 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;

添加新:

Musta去看你的代码而没有冒犯,你的代码有点疯狂:)

变量的可见性有点令人困惑,因为你想要但却不能很好地测试并查看访问修饰符,无论它们是否匹配你想做的事情。 我的英语也有点疯狂。

在你的班级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);

}

..//

类GameObject Change;

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;