所以,我的问题是我的精灵似乎是从屏幕左侧生成到屏幕右侧(我正在使用三星 s3 来测试我的应用程序)。它们应该从屏幕顶部下来。我正在使用 Libgdx,我知道 0 从左下角开始。
这是我的代码来设置他们应该从哪里开始..
ublic class EntityManager {
private final Array<Entity> entities = new Array<Entity>();
public EntityManager(int amount){
for(int i = 0; i< amount; i++) {
float x = MathUtils.random (0, MainGame.WIDTH - TextureManager.ENEMY.getWidth());
float y = MathUtils.random(MainGame.HEIGHT, MainGame.HEIGHT * 2);
float speed = MathUtils.random(2, 5);
addEntity(new Enemy(new Vector2 (x, y), new Vector2(0, - speed)));
这是我的 MainGame 类...(以及它的上半部分)
public class MainGame implements ApplicationListener {
public static int WIDTH = 480, HEIGHT = 800;
private SpriteBatch batch;
这是我的正射相机设置(上半部分)
public OrthoCamera() {
this(new VirtualViewport(MainGame.WIDTH, MainGame.HEIGHT));
这是我完整的 OrthoCamera 课程
public class OrthoCamera extends OrthographicCamera {
Vector3 tmp = new Vector3();
Vector2 origin = new Vector2();
VirtualViewport virtualViewport;
Vector2 pos = new Vector2();
public OrthoCamera() {
this(new VirtualViewport(MainGame.WIDTH, MainGame.HEIGHT));
}
public OrthoCamera(VirtualViewport virtualViewport) {
this(virtualViewport, 0f, 0f);
}
public OrthoCamera(VirtualViewport virtualViewport, float cx, float cy) {
this.virtualViewport = virtualViewport;
this.origin.set(cx, cy);
}
public void setVirtualViewport(VirtualViewport virtualViewport) {
this.virtualViewport = virtualViewport;
}
public void setPosition(float x, float y) {
position.set(x - viewportWidth * origin.x, y - viewportHeight * origin.y, 0f);
pos.set(x, y);
}
public Vector2 getPos() {
return pos;
}
@Override
public void update() {
float left = zoom * -viewportWidth / 2 + virtualViewport.getVirtualWidth() * origin.x;
float right = zoom * viewportWidth / 2 + virtualViewport.getVirtualWidth() * origin.x;
float top = zoom * viewportHeight / 2 + virtualViewport.getVirtualHeight() * origin.y;
float bottom = zoom * -viewportHeight / 2 + virtualViewport.getVirtualHeight() * origin.y;
projection.setToOrtho(left, right, bottom, top, Math.abs(near), Math.abs(far));
view.setToLookAt(position, tmp.set(position).add(direction), up);
combined.set(projection);
Matrix4.mul(combined.val, view.val);
invProjectionView.set(combined);
Matrix4.inv(invProjectionView.val);
frustum.update(invProjectionView);
}
/**
* This must be called in ApplicationListener.resize() in order to correctly update the camera viewport.
*/
public void updateViewport() {
setToOrtho(false, virtualViewport.getWidth(), virtualViewport.getHeight());
}
public Vector2 unprojectCoordinates(float x, float y) {
Vector3 rawtouch = new Vector3(x, y,0);
unproject(rawtouch);
return new Vector2(rawtouch.x, rawtouch.y);
}
public void resize() {
VirtualViewport virtualViewport = new VirtualViewport(MainGame.WIDTH, MainGame.HEIGHT);
setVirtualViewport(virtualViewport);
updateViewport();
}
}
还有我完整的 VirtualViewport 课程
import com.badlogic.gdx.Gdx;
public class VirtualViewport {
float virtualWidth;
float virtualHeight;
public float getVirtualWidth() {
return virtualWidth;
}
public float getVirtualHeight() {
return virtualHeight;
}
public VirtualViewport(float virtualWidth, float virtualHeight) {
this(virtualWidth, virtualHeight, false);
}
public VirtualViewport(float virtualWidth, float virtualHeight, boolean shrink) {
this.virtualWidth = virtualWidth;
this.virtualHeight = virtualHeight;
}
public float getWidth() {
return getWidth(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
public float getHeight() {
return getHeight(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
/**
* Returns the view port width to let all the virtual view port to be shown on the screen.
*
* @param screenWidth
* The screen width.
* @param screenHeight
* The screen Height.
*/
public float getWidth(float screenWidth, float screenHeight) {
float virtualAspect = virtualWidth / virtualHeight;
float aspect = screenWidth / screenHeight;
if (aspect > virtualAspect || (Math.abs(aspect - virtualAspect) < 0.01f)) {
return virtualHeight * aspect;
} else {
return virtualWidth;
}
}
/**
* Returns the view port height to let all the virtual view port to be shown on the screen.
*
* @param screenWidth
* The screen width.
* @param screenHeight
* The screen Height.
*/
public float getHeight(float screenWidth, float screenHeight) {
float virtualAspect = virtualWidth / virtualHeight;
float aspect = screenWidth / screenHeight;
if (aspect > virtualAspect || (Math.abs(aspect - virtualAspect) < 0.01f)) {
return virtualHeight;
} else {
return virtualWidth / aspect;
}
}
}
这是我的 Enemy Class (应该下降的对象)
public class Enemy extends Entity {
public Enemy(Vector2 pos, Vector2 direction) {
super(TextureManager.ENEMY, pos, direction);
}
@Override
public void update() {
pos.add(direction);
if (pos.y <= - TextureManager.ENEMY.getHeight()){
float x = MathUtils.random(0, MainGame.WIDTH - TextureManager. ENEMY.getWidth());
pos.set(x, MainGame.HEIGHT);
}
}
}