MBR*_*que 11 java scroll libgdx

我已经忙了10个小时(字面意思)而且我已经完成了,我需要问一下.我正在学习如何使用LibGdx编写Java游戏.我正在做一个水平太空船游戏.所以,我最糟糕的问题是我不知道如何滚动(我认为绘图会更好地解释).我想绘制一个hudge背景(Space)并使我的OrthographicCamera像我的SpaceShip一样向右移动,因此它将创建一个带有Space Background的Scroll效果.没有敌人,只有屏幕上的船只.
我在嘲笑这个
public void moveCamera(float x,float y){
cam.position.set(x, y, 0);
}
Run Code Online (Sandbox Code Playgroud)
然后我在我的WorldRender render()方法中使用该方法:
public void render(float delta){
ship.Move(delta);
moveCamera(ship.getPosition().x,ship.getPosition().y);
cam.update();
System.out.println(""+cam.position);
spriteBatch.begin();
drawBackground();
drawShip();
spriteBatch.end();
}
Run Code Online (Sandbox Code Playgroud)
我实际上移动了相机的位置(我可以看到这要归功于println),但它并没有在游戏中移动,所以SpaceShip只是在窗口的边缘消失了.
我也在spriteBatch.end()之前尝试过这个
spriteBatch.setProjectionMatrix(camera.combined);
Run Code Online (Sandbox Code Playgroud)
但当我这样做时,窗户显示黑屏,没有船,没有什么.正如我所说,我很绝望,我看到很多例子(用鼠标滚动,paralexscrolling等),但所有都是高级或与我的代码无关.任何帮助将不胜感激
这是我绘制东西的方式.背景和船是WorldRender里面的纹理.我画的背景图片非常宽,所以我的意图是按照我说的做一些滚动.这就是代码
private void loadTextures(){
shipTexture=new Texture(Gdx.files.internal("nave.png"));
background=new Texture(Gdx.files.internal("fondo.jpg"));
}
public void drawShip(){
spriteBatch.draw(shipTexture,ship.getPosition().x*ppuX,ship.getPosition().y*ppuY, ship.WIDTH*ppuX,ship.HEIGHT*ppuY);
}
public void drawBackground(){
spriteBatch.draw(background, -10*ppuX,0*ppuY, Gdx.graphics.getWidth()*10,Gdx.graphics.getHeight());
}
Run Code Online (Sandbox Code Playgroud)
如果有人想在硬核模式下提供帮助,您可以在这里下载代码
我终于解决了它!WIIIIIIIIII!这是我在类名WorldRenderer中使用的代码,它具有在GameScreen中用于渲染,调整大小等的方法
public WorldRenderer(World world) {
// TODO Auto-generated constructor stub
this.world=world;
this.ship=world.getShip();
this.cam = new OrthographicCamera(CAMERA_WIDTH,CAMERA_HEIGHT);
this.cam.setToOrtho(false,CAMERA_WIDTH,CAMERA_HEIGHT);
this.cam.position.set(ship.getPosition().x,CAMERA_HEIGHT/2,0);
this.cam.update();//actualizamos la camara
spriteBatch=new SpriteBatch();
loadTextures();
}
private void loadTextures(){
shipTexture=new Texture(Gdx.files.internal("nave.png"));
background=new Texture(Gdx.files.internal("fondo.jpg"));
}
public void drawShip(){
spriteBatch.draw(shipTexture,ship.getPosition().x,ship.getPosition().y,10,10);
}
public void drawBackground(){
spriteBatch.draw(background, 0,0, 500,50);
}
public void render(float delta){
ship.Move(delta);
moveCamera(ship.getPosition().x);
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
drawBackground();
drawShip();
spriteBatch.end();
}
public void moveCemara(float x){
cam.position.set(x+20,cam.position.y, 0);
cam.update();
}
Run Code Online (Sandbox Code Playgroud)
在船内,我有这种方法,我在WorldRenderer中的渲染中调用它来移动它
public void Move(float delta){
if(Gdx.input.isKeyPressed(Keys.LEFT)) this.position.x -=velocity *delta;
if(Gdx.input.isKeyPressed(Keys.RIGHT)) this.position.x +=velocity *delta;
if(Gdx.input.isKeyPressed(Keys.UP)) this.position.y +=velocity *delta;
if(Gdx.input.isKeyPressed(Keys.DOWN)) this.position.y -=velocity *delta;
}
Run Code Online (Sandbox Code Playgroud)
我也非常感谢帮助我的人们.我将第一个答案标记为好的答案,但是,混合两者是给我真正解决方案的原因.
我离开这里,我跟着一些非常适合新手的tutos
这是一个很好的一切 - 从刮擦tuto LiGdxForNoobs
一个简单的plataform游戏平台游戏
一个非常简单的游戏桶游戏
我不知道这是你唯一的错误,但这是一个错误.如果这就是你说你做的:
spriteBatch.begin();
drawBackground();
drawShip();
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.end();
Run Code Online (Sandbox Code Playgroud)
你什么都看不到.在begin()/ end()块中调用setProjectionMatrix时.当前批次被刷新到gpu.所以,你实际上并没有用相机矩阵绘制任何东西.你应该这样做:
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
drawBackground();
drawShip();
spriteBatch.end();
Run Code Online (Sandbox Code Playgroud)
编辑:
如果你不调用那一行,spriteBatch会使用自己的默认摄像头(不会注意到你的camera.update()修改,所以这不是你想要的).
您现在应该更加关注您正在使用的坐标.我不太确定你真的需要ppu转换的东西.首先,在假想的世界坐标中定义所有东西,注意你会在你的世界中看到一些伸展.
public void drawShip(){
spriteBatch.draw(shipTexture,ship.getPosition().x,ship.getPosition().y, 10, 10);
}//your ship is 10 units wide and tall!
public void drawBackground(){
spriteBatch.draw(background, -10,0, 500, 100);
} //your background is 500 units wide, and 100 units tall
//camera setup
camera = new OrthographicCamera(50, 50);
//your camera will print to screen 50 units of your world
Run Code Online (Sandbox Code Playgroud)
如果你看到一个伸展的世界,试着去理解它是如何工作的(如果你看不到任何东西,那么某处就会出现问题).
编辑2
我看了你的代码.首先删除ppu,因为它模糊了你的代码.在ship.position*ppu绘图时,您将凸轮位置设置为ship.postion.你的背景也太大了(这就是你看它像素化的原因).你应该看到一些合理的东西.(总有一天你必须以另一种方式初始化你的相机来处理拉伸,但是忘记它直到你理解它们是如何工作的).
this.cam = new OrthographicCamera(CAMERA_WIDTH,CAMERA_HEIGHT);
public void drawShip(){
spriteBatch.draw(shipTexture, ship.getPosition().x ,ship.getPosition().y, 10, 10);
}
public void drawBackground(){
spriteBatch.draw(background, -CAMERA_WIDTH/2, -CAMERA_HEIGHT/2, 100, 100); //let bg begin where camera starts. (0,0)
}
public void render(float delta){
ship.Move(delta);
moverCamara(ship.getPosition().x, ship.getPosition().y);
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
drawBackground();
drawShip();
spriteBatch.end();
}
Run Code Online (Sandbox Code Playgroud)
不清楚你的图是怎么画的?我不确定你的这种方法是否正确。你能提供你的背景和船舶的详细信息吗?您能否提供有关背景图像的详细信息,它是您滚动的巨大图像还是您想要在滚动时重复的重复图像?
- 编辑 -
好吧,我想我知道会发生什么。我通常会将相机应用到当前环境。
将以下内容放入调整大小中
public void resize(int width, int height) {
cam = new OrthographicCamera(width, height);
cam.translate(width / 2, height / 2, 0);
}
Run Code Online (Sandbox Code Playgroud)
将以下内容放在 render() 的开头
cam.position.set(posX,posY,0);
cam.update();
cam.apply(Gdx.gl10);
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // #14
Run Code Online (Sandbox Code Playgroud)
这将使您拥有一个清晰的屏幕,原点设置在窗口的左下角。然后你应该先画你的背景
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
spriteBatch.draw(background,0,0,sizeX,sizeY);
spriteBatch.end()
Run Code Online (Sandbox Code Playgroud)
看看当您移动相机位置 posX 和 posY 时效果如何。然后将你的船添加到组合中
-- 更多编辑 ---
然后您可以将 posX 和 posY 计算为
posX = 默认偏移X+shipX
等等..
无论如何,希望这会有所帮助,我仍然只是在学习自己,所以这可能不是最好的方法..但它似乎有效。