Object "Camera" will not centre in game when resized to fullscreen

I am trying to centre my camera object so that is can be, in a way, the main focal point of the game.

_camera = new Camera(_map, maxWidth, maxHeight);
_camera.x = _baseWidth / 2 - _camera.renderWidth / 2;
_camera.y = _baseHeight / 2 - _camera.renderHeight / 2;
addChild(_camera);

Lib.current.stage.addEventListener(Event.RESIZE, function(e)
        {
            if (!_inited)
            {
                _inited    = true;
            }
            else 
            {
                _camera.scaleX = Lib.current.stage.stageWidth / _baseWidth;
                _camera.scaleY = Lib.current.stage.stageHeight / _baseHeight;
                
            }
        });

Upon using the above code, when resizing, the camera position resets to (0,0) but when returning to default window size, the camera returns back to its original position…

Is there a bug I’m missing, or is my implementation wrong?

Make sure you set the camera x and y when you change the scale, maybe?

Using the following code - with or without setting the position - regardless results in the below images:

_cX = _camera.x = _baseWidth / 2 - _camera.renderWidth / 2;
_cY = _camera.y = _baseHeight / 2 - _camera.renderHeight / 2;

Lib.current.stage.addEventListener(Event.RESIZE, function(e)
        {
            if (!_inited)
            {
                _inited    = true;
            }
            else 
            {
                _camera.scaleX = Lib.current.stage.stageWidth / _baseWidth;
                _camera.scaleY = Lib.current.stage.stageHeight / _baseHeight;
                
                _camera.x = _cX;
                _camera.y = _cY;
                
            }
        });

Before maximise:

After maximise:

Graphics are licensed CC0 (just a remark to protect you in case you think I’m committing copyright infringement).

It’s almost as if the stage is trying to override the scaling methods done when the window RESIZE event is dispatched, causing any objects to be relocated. But I could be making that up for all I know.

Camera is an object that extends Sprite, and asks for three parameters, a “scene” to render, the render width and render height. These are specified as a scrollRect and thus, moving the camera modifies the scrollRect properties accordingly.

The Camera is instantiated inside of the Main class, and added there as well. You can test it out for yourself on my Github page. I’m testing in neko, haven’t tested on windows yet.

When you use scaleX/scaleY, it keeps the upper-left corner at the same position, then scales down and to the right. At a glance, I think your code is centering the x and y, which may look strange when you scale. You will need to offset the X and Y only by the size difference between the scaled camera and the stage size

Oh… So scaling scales the image from the centre of the image, not the top-left? Is there any way to change the “pivot” point if one exists so that it does not scale that way?

If you add it to another Sprite, you can change the point, for example:

var container = new Sprite ();
content.x = -content.width / 2;
content.y = -content.height / 2;
container.addChild (content);
container.x = stage.stageWidth / 2;
container.y = stage.stageHeight / 2;

Just out of curiosity, what is your priority on StageScaleMode in native targets? :wink:

Also, I’m certainly getting somewhere. The result is now the opposite. The position of the camera seems to be the top-left corner, and resizing causes the camera to position to the centre, and then back to top-left when demaximised (if that’s even a word, otherwise I’ve invented one to make up for a lack of one).

Trying other methods as well.

I’m starting to dislike this scaling, it’s so confusing on how it operates. I may try implementing my own pivot point into OpenFL, although I’m no mathematician nor physicist, since this is starting to annoy me.

Regardless of what I scale, whether it’s the Main, container or camera, I’m not getting any of the results I expect.

Have you looked at PiratePig? It has some code that resizes the game content to fill the screen