Viewport的Android问题(Android problems with Viewport)
当我在桌面上运行游戏工作正常,但是当我在我的Android设备中运行它时,图像看起来是半个,当我使用PLAY按钮游戏关闭时,任何人都可以帮助我吗? 谢谢。
public class GameScreen extends AbstractScreen { private Viewport viewport; private Camera camera; private SpriteBatch batch; private Texture texture; private float escala; private Paddle Lpaddle, Rpaddle; private Ball ball; private BitmapFont font; private int puntuacion, puntuacionMaxima; private Preferences preferencias; private Music music; private Sound sonidoex; public GameScreen(Main main) { super(main); preferencias = Gdx.app.getPreferences("PuntuacionAppPoints"); puntuacionMaxima = preferencias.getInteger("puntuacionMaxima"); music =Gdx.audio.newMusic(Gdx.files.internal("bgmusic.mp3")); music.play(); music.setVolume((float) 0.3); music.setLooping(true); sonidoex = Gdx.audio.newSound(Gdx.files.internal("explosion5.wav")); } public void create(){ camera = new PerspectiveCamera(); viewport = new FitViewport(800, 480, camera); } public void show(){ batch = main.getBatch(); texture = new Texture(Gdx.files.internal("spacebg.png")); Texture texturaBola = new Texture(Gdx.files.internal("bola.png")); ball = new Ball(Gdx.graphics.getWidth() / 2 - texturaBola.getWidth() / 2, Gdx.graphics.getHeight() / 2 - texturaBola.getHeight() / 2); Texture texturaPala= new Texture(Gdx.files.internal("pala.png")); Lpaddle = new LeftPaddle(80, Gdx.graphics.getHeight()/2 -texturaPala.getHeight() /2); Rpaddle = new RightPaddle(Gdx.graphics.getWidth() -100, Gdx.graphics.getHeight()/2 - texturaPala.getHeight() /2, ball); font = new BitmapFont(); font.setColor(Color.WHITE); font.setScale(1f); puntuacion = 0; } public void render(float delta){ Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); updatePuntuacion(); Lpaddle.update(); Rpaddle.update(); ball.update(Lpaddle, Rpaddle); batch.begin(); batch.draw(texture, 0, 0,texture.getWidth(), texture.getHeight()); ball.draw(batch); Lpaddle.draw(batch); Rpaddle.draw(batch); font.draw(batch, "Points: " + Integer.toString(puntuacion), Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5); font.draw(batch, "High score: " + Integer.toString(puntuacionMaxima),Gdx.graphics.getWidth() - Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5); batch.end(); } private void updatePuntuacion(){ if(ball.getBordes().overlaps(Lpaddle.getBordes())) { puntuacion = puntuacion + 1; if(puntuacion > puntuacionMaxima) puntuacionMaxima = puntuacion; } if(ball.getBordes().x <= 0) sonidoex.play(); if(ball.getBordes().x <= 0) puntuacion =0; if(ball.getBordes().x <=0) Gdx.input.vibrate(1000); if(ball.getBordes().x <=0) Screens.juego.setScreen(Screens.MAINSCREEN); ball.comprobarPosicionBola(); } public void hide(){ font.dispose(); texture.dispose(); } @Override public void dispose(){ preferencias.putInteger("puntuacionMaxima", puntuacionMaxima); preferencias.flush(); } public void resize(int width, int height){ float widthImage = texture.getWidth(); float heightImage = texture.getHeight(); float r = heightImage / widthImage; if(heightImage > height) { heightImage = height; widthImage = heightImage / r; } if(widthImage > width) { widthImage = width; heightImage = widthImage * r; } escala = width / widthImage; if(Gdx.app.getType()== ApplicationType.Android) viewport.update(width, height); } }
When i run the game in Desktop works fine, but when i run it in my android device, the image looks cuted in a half and when i use the PLAY button the game closes, anyone can help me? thank you.
public class GameScreen extends AbstractScreen { private Viewport viewport; private Camera camera; private SpriteBatch batch; private Texture texture; private float escala; private Paddle Lpaddle, Rpaddle; private Ball ball; private BitmapFont font; private int puntuacion, puntuacionMaxima; private Preferences preferencias; private Music music; private Sound sonidoex; public GameScreen(Main main) { super(main); preferencias = Gdx.app.getPreferences("PuntuacionAppPoints"); puntuacionMaxima = preferencias.getInteger("puntuacionMaxima"); music =Gdx.audio.newMusic(Gdx.files.internal("bgmusic.mp3")); music.play(); music.setVolume((float) 0.3); music.setLooping(true); sonidoex = Gdx.audio.newSound(Gdx.files.internal("explosion5.wav")); } public void create(){ camera = new PerspectiveCamera(); viewport = new FitViewport(800, 480, camera); } public void show(){ batch = main.getBatch(); texture = new Texture(Gdx.files.internal("spacebg.png")); Texture texturaBola = new Texture(Gdx.files.internal("bola.png")); ball = new Ball(Gdx.graphics.getWidth() / 2 - texturaBola.getWidth() / 2, Gdx.graphics.getHeight() / 2 - texturaBola.getHeight() / 2); Texture texturaPala= new Texture(Gdx.files.internal("pala.png")); Lpaddle = new LeftPaddle(80, Gdx.graphics.getHeight()/2 -texturaPala.getHeight() /2); Rpaddle = new RightPaddle(Gdx.graphics.getWidth() -100, Gdx.graphics.getHeight()/2 - texturaPala.getHeight() /2, ball); font = new BitmapFont(); font.setColor(Color.WHITE); font.setScale(1f); puntuacion = 0; } public void render(float delta){ Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); updatePuntuacion(); Lpaddle.update(); Rpaddle.update(); ball.update(Lpaddle, Rpaddle); batch.begin(); batch.draw(texture, 0, 0,texture.getWidth(), texture.getHeight()); ball.draw(batch); Lpaddle.draw(batch); Rpaddle.draw(batch); font.draw(batch, "Points: " + Integer.toString(puntuacion), Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5); font.draw(batch, "High score: " + Integer.toString(puntuacionMaxima),Gdx.graphics.getWidth() - Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5); batch.end(); } private void updatePuntuacion(){ if(ball.getBordes().overlaps(Lpaddle.getBordes())) { puntuacion = puntuacion + 1; if(puntuacion > puntuacionMaxima) puntuacionMaxima = puntuacion; } if(ball.getBordes().x <= 0) sonidoex.play(); if(ball.getBordes().x <= 0) puntuacion =0; if(ball.getBordes().x <=0) Gdx.input.vibrate(1000); if(ball.getBordes().x <=0) Screens.juego.setScreen(Screens.MAINSCREEN); ball.comprobarPosicionBola(); } public void hide(){ font.dispose(); texture.dispose(); } @Override public void dispose(){ preferencias.putInteger("puntuacionMaxima", puntuacionMaxima); preferencias.flush(); } public void resize(int width, int height){ float widthImage = texture.getWidth(); float heightImage = texture.getHeight(); float r = heightImage / widthImage; if(heightImage > height) { heightImage = height; widthImage = heightImage / r; } if(widthImage > width) { widthImage = width; heightImage = widthImage * r; } escala = width / widthImage; if(Gdx.app.getType()== ApplicationType.Android) viewport.update(width, height); } }
原文:https://stackoverflow.com/questions/26202622
最满意答案
你可以在
&
后面运行一个进程(一个&符号,而不是两个),但这需要你手动管理它,这将是相当乏味的。 有关详细信息,请参阅shell脚本行末尾的&符号是什么意思? 。对于那些
concurrently
构建的用例,可以很简单地并行运行进程并跟踪其输出。npm install --save-dev concurrently
你的
start
脚本变成:"start": "concurrently 'npm run webpack' 'npm run server'"
如果你想让输出更漂亮一点,你可以用
-c
给这些进程的名字加上-n
和颜色,例如:"start": "concurrently -n 'webpack,server' -c 'bgBlue.bold,bgGreen.bold' 'npm run webpack' 'npm run server'"
You could run one process in the background with
&
(one ampersand, not two) but that would require you to manage it manually, which would be rather tedious. For details see What does ampersand mean at the end of a shell script line?.For that use-case someone built
concurrently
, which makes it simple to run processes in parallel and keep track of their output.npm install --save-dev concurrently
And your
start
script becomes:"start": "concurrently 'npm run webpack' 'npm run server'"
If you want to make the output a little prettier you can give the processes names with
-n
and colours with-c
, for example:"start": "concurrently -n 'webpack,server' -c 'bgBlue.bold,bgGreen.bold' 'npm run webpack' 'npm run server'"
相关问答
更多-
你可以在&后面运行一个进程(一个&符号,而不是两个),但这需要你手动管理它,这将是相当乏味的。 有关详细信息,请参阅shell脚本行末尾的&符号是什么意思? 。 对于那些concurrently构建的用例,可以很简单地并行运行进程并跟踪其输出。 npm install --save-dev concurrently 你的start脚本变成: "start": "concurrently 'npm run webpack' 'npm run server'" 如果你想让输出更漂亮一点,你可以用-c给这些进 ...
-
使用Bazel的npm(Use npm from Bazel)[2023-09-15]
我认为您链接到的项目是一个npm模块,用于生成一个bazel工作区(可能还有构建文件)。 我想你想要另一个方向依赖于npm模块。 AFAIK还没有rules_npm,但在rules_typescript中有一些基本的支持 。 我还没有使用它(我们在JS代码之前迁移JVM代码)但是我建议你试着在那里查看它是否可用,即使没有打字稿部分。 上面的链接显示了针对npm的一些提交,问题和非常基本的代码指针。 希望这可以帮助。 更新: rules_nodejs已经发布,它内置了对npm的支持。 I think the ... -
我相信你可能只是错过了分号 create database test; I believe that you may just be missing the semicolon create database test;
-
npm run做什么?(What npm run do?)[2024-01-22]
如果这是你所要求的, Npm run与节点子进程无关。 Npm run是由npm CLI提供的命令,它允许实例化一个shell并执行项目的package.json文件中提供的命令。 考虑到这是你的package.json: { "name": "my-awesome-package", "version": "1.0.0", "script" : { "test" : "mocha ./test/unit/mytest.js" } } 现在,如果您执行npm run test ,npm将会简单 ... -
您需要确保将npm s全局bin文件夹添加到PATH 。 你如何做到这一点可能取决于你的shell。 但是,您可以执行npm bin -g来获取npm bin -g的全局bin文件夹。 you need to ensure that npms global bin folder is added to your PATH. How you do this may depend on your shell. However you can execute npm bin -g to get the globa ...
-
无法运行任何npm全局命令(cannot run any npm global command)[2022-12-02]
您是否查看了该文件夹并查看全局npm命令是否存在? 如果是这样,您可以将/Users/Alex/.npm-packages/bin包含在$ PATH中,它应该可以解决问题。 否则,您可能无法正确安装全局程序包。 希望能帮助到你! Have you looked into the folder and see if the global npm commands are there? If it does, you can just include /Users/Alex/.npm-packages/bin ... -
在kudu中运行npm脚本(run npm scripts in kudu)[2022-04-02]
来自kudu的wiki : 安装azure-cli npm install azure-cli -g 将azure控制台切换到asm模式 azure config mode asm 在项目的根目录中,运行自定义部署脚本生成器命令: azure site deploymentscript [options] 现在,您可以编辑deploy.cmd文件并添加自定义步骤(如npm命令)。 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ... -
您可以通过环境变量来抑制或静默NPM: npm_config_loglevel=silent npm version 这应该在您的.bashrc (或者在您调用nvm任何地方): npm_config_loglevel=silent nvm use 0.10 或者,您可以全局设置环境变量 export npm_config_loglevel=silent 这里可以找到不同的loglevels - 它们是: “沉默”,“错误”,“警告”,“http”,“信息”,“详细”,“愚蠢” You can sup ...
-
是的,你可以添加一个脚本来执行所有这些操作,例如 { "scripts": { "setup": "npm install && bower install && grunt build" } } 然后你就可以运行了 npm run setup Yes, you could add a single script to perform all those operations, e.g. { "scripts": { "setup": "npm install && bowe ...
-
我认为缓存卡在某种程度上,尝试删除~/.heroku并再次尝试。 编辑:事实证明这是在Heroku私人npm服务器上缓存的问题。 I think the cache got stuck somehow, try deleting ~/.heroku and trying again. EDIT: turns out this was an issue with caching on the Heroku private npm server.