AndEngine - Sprites are not getting scaled on different devices

不想你离开。 提交于 2019-12-10 11:19:37

问题


Code :

This is how m initializing camera.
I', trying to get resolution of device and based on that i set camera width and height.

     public class GameActivity extends SimpleBaseGameActivity {

        private SmoothCamera mCamera;
        private DisplayMetrics dM; 

        private int CAMERA_WIDTH, CAMERA_HEIGHT;

        private double  ScreenWidth,
                        ScreenHeight,
                        resolutionRatio;


        public EngineOptions onCreateEngineOptions() {

             //set Camera
            getDeviceResolution();
            setCamera();


        EngineOptions options =  new EngineOptions(true,
            ScreenOrientation.LANDSCAPE_FIXED, 
            new RatioResolutionPolicy((int)this.getCameraWidth(),(int)this.getCameraHeight()),
            //new FillResolutionPolicy(),
            mCamera);
          return options;

        }

      private void getDeviceResolution() {
            dM = new DisplayMetrics();
            this.getWindowManager().getDefaultDisplay().getMetrics(dM);
            this.ScreenWidth    = dM.widthPixels;
            this.ScreenHeight   = dM.heightPixels;
            this.resolutionRatio = this.ScreenWidth/this.ScreenHeight;

                resolutionRatio = (double)Math.round(resolutionRatio * 100) / 100;

            Log.d("Resolution","ScrennWidth: "+this.ScreenWidth );
            Log.d("Resolution","ScrennHeight: "+this.ScreenHeight );
                    Log.d("Resolution","Resolution Ratio: " +   this.resolutionRatio );
        }



    private void setCamera() {

        if(resolutionRatio == 1.66){
            this.setCameraHeight(340);
            this.setCameraWidth(400);
        }else if(resolutionRatio == 2.13){
            this.setCameraHeight(480);
            this.setCameraWidth(1024);
        }else if(resolutionRatio == 1.77){
            this.setCameraHeight(720);
            this.setCameraWidth(1280);
        }else if(resolutionRatio == 1.5){
            this.setCameraHeight(320);
            this.setCameraWidth(480);
        }else if(resolutionRatio == 1.67){
            this.setCameraHeight(480);
            this.setCameraWidth(800);
        }else {
            this.setCameraHeight((int) this.ScreenHeight);
            this.setCameraWidth((int) this.ScreenWidth);
        }

        // Create a Camera
        this.mCamera = new SmoothCamera(0,0,(int)getCameraWidth(),(int)getCameraHeight(),100,100,1.0f);
            mCamera.setZoomFactor(1.0f);

            mCamera.setBoundsEnabled(true);
            mCamera.setBounds(0, 0, mCamera.getWidth(), mCamera.getHeight());   
        }



        }

The issue is.

I've etried game on device with resolution 480x320;

and when i tried same code on device with resolution 800X480

I Thinks sprites are not getting scaled on higher resolution devices.
As per my knowledge andengine scales camera and sprites natively.

So why sprites are not getting scaled on this situation?

What should i do to scale up sprites based on resolution?

I tried FillResolutionPolicy too. Same thing.

I'm using TexturePackerExtension of andengine adn SpriteSheets.


回答1:


You want a Camera with a fixed width and a height that is calculated by the devices screen aspectratio. Pair that with a FillResolutionPolicy and you get what you want.

Note:

AndEngine doesn't scale Sprites for you, but you set the Camera so that the whole Scene appears scaled.




回答2:


AndEngine does scale your Sprites even in your example. The reason why the result is not what you expected is that you set the camera's widthand height manually (based on the resolution).

If you really want every sprite scaled in the same way on every display, then you should choose one fixed resolution in which you create your game. For example you could set up every sprite and every position to a resolution of 960x640.

 // skip the whole if(resolutionRatio == 1.66){..  part
this.mCamera = new SmoothCamera(0, 0, 960f, 640f, 100, 100, 1.0f);

AndEngine will then scale the camera, so that it fills as much of the display as possible.

 public EngineOptions onCreateEngineOptions() {
      EngineOptions options = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(1.5f), camera;
      return options;
 }

the 1.5f is the fitting aspect ratio for the 960x640 resolution. Now you won't have to take care of the different display sizes anymore.



来源:https://stackoverflow.com/questions/15820219/andengine-sprites-are-not-getting-scaled-on-different-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!