Zoom Scrolling Camera

48 views
Skip to first unread message

Martin Wells

unread,
Mar 3, 2015, 12:15:27 PM3/3/15
to
Hi guys,

I recently struggled with building out a camera that could both scroll and zoom (whilst staying centered).

Thought I would share a class I wrote to handle this (feedback welcome).

package;


import flixel.util.FlxPoint;
import flixel.FlxCamera;
import flixel.FlxG;


class ZoomScrollCamera extends FlxCamera
{
 
public var targetZoom(default,set):Float;
 
public var zoomSpeed:Float;


 
private var _currentZoomSpeed:Float;
 
private var _zoomStartWidth:Int;
 
private var _zoomStartHeight:Int;
 
private var _startScrollX:Float;
 
private var _startScrollY:Float;
 
public var _lastMousePos:FlxPoint;


 
public function new(X:Int, Y:Int, Width:Int, Height:Int, Zoom:Float = 0)
 
{
   
super(X, Y, Width, Height, FlxCamera.defaultZoom);

    _currentZoomSpeed
= 0;
    x
= 0;
    y
= 0;
    targetZoom
= 1;
    zoom
= 1;
    zoomSpeed
= 3;
    _lastMousePos = new FlxPoint(0,0);
 }


 
public function set_targetZoom(z:Float):Float
 
{
    targetZoom
= z;
   
if (targetZoom < zoom) _currentZoomSpeed = -zoomSpeed;
   
if (targetZoom > zoom) _currentZoomSpeed = zoomSpeed;

    _zoomStartWidth
= width;
    _zoomStartHeight
= height;

    _startScrollX
= scroll.x;
    _startScrollY
= scroll.y;

   
return targetZoom;
 
}


 
public override function update():Void
 
{
   
super.update();


   
if (FlxG.mouse.justPressed) {
        _lastMousePos
.x = FlxG.mouse.screenX;
       _lastMousePos
.y = FlxG.mouse.screenY;
   
}


   
if (FlxG.mouse.pressed) {
       scroll
.x -= (FlxG.mouse.screenX - _lastMousePos.x);
       scroll
.y -= (FlxG.mouse.screenY - _lastMousePos.y);
       _lastMousePos
.x = FlxG.mouse.screenX;
       _lastMousePos
.y = FlxG.mouse.screenY;
   
}


   
if (_currentZoomSpeed != 0)
   
{
       zoom
+= FlxG.elapsed * _currentZoomSpeed;
       width
= Std.int(FlxG.width / zoom);
       height
= Std.int(FlxG.height / zoom);
       scroll
.x = _startScrollX + ((_zoomStartWidth - width)/2);
       scroll
.y = _startScrollY + ((_zoomStartHeight - height)/2);

       
if (_currentZoomSpeed > 0 && zoom >= targetZoom || _currentZoomSpeed < 0 && zoom <= targetZoom)
          doneZoom
();
     
}
 
}


 
private function doneZoom()
 
{
    _currentZoomSpeed
= 0;
 
}


}

Usage:

 _camera = new ZoomScrollCamera(0, 0, FlxG.width, FlxG.height, 1);
 
FlxCamera.defaultCameras = [_camera];

// zoom in
_camera.targetZoom = _camera.zoom * 1.5;

// zoom out
_camera.targetZoom = _camera.zoom / 1.5;




yannicular

unread,
Mar 5, 2015, 7:39:09 AM3/5/15
to haxef...@googlegroups.com
Nice, thanx for sharing
Reply all
Reply to author
Forward
0 new messages