首页 \ 问答 \ Viewport的Android问题(Android problems with Viewport)

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
更新时间:2023-09-21 13:09

最满意答案

你可以在&后面运行一个进程(一个&符号,而不是两个),但这需要你手动管理它,这将是相当乏味的。 有关详细信息,请参阅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给这些进 ...
  • 我认为您链接到的项目是一个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与节点子进程无关。 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命令是否存在? 如果是这样,您可以将/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的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.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)