Camera zoom/stage scale problem on Android when losing focus

35 views
Skip to first unread message

Bogwog

unread,
May 21, 2015, 9:53:21 PM5/21/15
to haxef...@googlegroups.com
I've implemented a simple pinch-to-zoom gesture to my game, which works by altering the FlxCamera's zoom depending on the distance between the two fingers on screen.

The actual gesture works, but there is an issue with what I suspect might be the stage size, although I'm not entirely certain how that part works. Here's the issue:

1. Player zooms in, FlxG.camera.zoom increases (up to a max of 2.5)
2. Player switches out of app; press home button, gets called, etc
3. Back to game, still zoomed in like it was before (good!)
4. Player tries to zoom out, and the entire stage shrinks to a little corner on the screen

Here is a gif showing the problem:

That FlxText there shows the current value of the FlxG.camera.zoom, which you can see acts predictably. However, when the user switches back to the application after closing it, when they try to zoom out, the entire screen shrinks into a little corner! I've tried using different ScaleModes, but that doesn't make a difference. 

And yes, the device in the gif is a Kindle Fire HDX, which isn't "regular" android, but its version of Fire OS is based off of KitKat, and regardless I get the exact same results when testing on an LG G3 with Android 5.0

Here is the relevant code just in case:

var dragging:Bool = false;
var dragOX:Float = 0;
var dragOY:Float = 0;
var threshold:Float = 1;
var maxZoom:Float = -1;
var camTargetZoom:Float = 1;
public function ZoomCamera(Delta:Float):Void {
if(maxZoom == -1)
maxZoom = Math.sqrt(Math.pow(FlxG.width, 2) + Math.pow(FlxG.height, 2));
var deltaAdjusted:Float = Delta / (maxZoom*0.25);
var MAXZOOM:Float = 2.5;
camTargetZoom += deltaAdjusted;
if (camTargetZoom < 1)
camTargetZoom = 1;
if (camTargetZoom > MAXZOOM)
camTargetZoom = MAXZOOM;
}
private function getDistance(x1:Float,y1:Float, x2:Float,y2:Float):Float {
return Math.sqrt(
Math.pow(x1 - x2, 2) +
Math.pow(y1 - y2, 2));
}
var tAnchor:Int = -1;
var tDrag:Int = -1;
var tAnchorDist:Float = 0;
var resetAnchorDist:Bool = false;
override public function update():Void {
super.update(); //player is an invisible FlxSprite
FlxG.camera.follow(player, FlxCamera.STYLE_TOPDOWN_TIGHT, 5);
for (touch in FlxG.touches.list)
if (tAnchor == -1){
tAnchor = touch.touchPointID;
resetAnchorDist = true;
}else if (tDrag == -1){
tDrag = touch.touchPointID;
resetAnchorDist = true;
}else {
continue;
}
var camspd:Float = 0.2;
if (Math.abs(FlxG.camera.zoom - camTargetZoom) <= 0.0001)
FlxG.camera.zoom = camTargetZoom;
else {
var mdelt:Float = ((camTargetZoom - FlxG.camera.zoom) * camspd);
FlxG.camera.zoom += mdelt;// + (0.05 * (mdelt < 0? -1:1));
}
if (FlxG.camera.zoom < 1)
FlxG.camera.zoom = 1;
var t1:FlxTouch = FlxG.touches.getByID(tAnchor);
var t2:FlxTouch = FlxG.touches.getByID(tDrag);
if (t1 == null || t2 == null) {
if(tAnchor != -1 || tDrag != -1){
tAnchor = tDrag = -1;
tAnchorDist = 0;
}
}else {
if (resetAnchorDist){
tAnchorDist = getDistance(t1.x, t1.y, t2.x, t2.y);
resetAnchorDist = false;
}
var dist = getDistance(t1.x, t1.y, t2.x, t2.y);
ZoomCamera(dist - tAnchorDist);
tAnchorDist = getDistance(t1.x, t1.y, t2.x, t2.y);
}
touchDebug.text = "camera zoom = " + FlxG.camera.zoom;
if (FlxG.mouse.pressed) {
if (!dragging) {
dragOX = FlxG.mouse.x;
dragOY = FlxG.mouse.y;
}
dragging = true;
}else
dragging = false;
if (dragging) {  
var xdelta:Float = (dragOX - FlxG.mouse.x);
var ydelta:Float = (dragOY - FlxG.mouse.y);
dragOX = FlxG.mouse.x;
dragOY = FlxG.mouse.y;
if(Math.abs(xdelta) > threshold)
player.x += xdelta*1.5;
if(Math.abs(ydelta) > threshold)
player.y += ydelta*1.5;
if (player.x < 0)
player.x = 0;
if (player.y < 0)
player.y = 0;
if (player.x + player.width > mapSizeX)
player.x = mapSizeX - player.width;
if (player.y + player.height > mapSizeY)
player.y = mapSizeY - player.height;
}
}


This is with HaxeFlixel 3.3.5 (I know it's outdated, but I'd like to avoid having to update if possible since doing so causes my rather large codebase to not build anymore, and that's a mess I'd rather not deal with.... :\ )


Any help appreciated!
Reply all
Reply to author
Forward
0 new messages