【发布时间】: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