flame-engine / flame

A Flutter based game engine.
https://flame-engine.org
MIT License
9.14k stars 895 forks source link

Zoom with ViewFinder doesn't work on TiledComponents #2878

Closed furqan-ahm closed 9 months ago

furqan-ahm commented 9 months ago

I was creating a pretty simple game with tile size of 82x82. I created the tilemap on Tiled using Uncompressed settings of fixed tile count. When trying to zoom in based on the canvasSize and the mapSize so that the whole map fits in view. It doesnt seem to work at all, I tried with sprite object and it seemed to be bigger but the Tile Map Component seemed to be same size regardless of zoom level.

Flutter version: 3.13.6 Flame version: 1.10.1 Flame Tiled version: 1.17.1

TiledComponent map =
    await TiledComponent.load('level1.tmx', Vector2.all(82), priority: 20);
mapSize = map.scaledSize;
add(map);

final flameSprite = await loadSprite('tiles/tile0.png');

world.add(
  SpriteComponent(
    sprite: flameSprite,
    size: Vector2(149, 211),
  )..anchor = Anchor.center,
);
// final spawnLayer = map.tileMap.getLayer<ObjectGroup>('spawn');
// for (final spawn in spawnLayer!.objects) {
//   if (spawn.class_ == '1') {
//     print('yes');
//   }
// }
camera.follow(map);
camera.viewfinder.zoom=canvasSize.x/mapSize.x;
print(camera.viewfinder.zoom);
return super.onLoad();
spydon commented 9 months ago

You are adding the map to the game instance instead of to the world, swap it to world.add(map) and it should work.

spydon commented 9 months ago

It also looks like you don't want to manually zoom here, but using the FixedResolutionViewport:

camera = CameraComponent.withFixedResolution(width: mapSize.x, height: mapSize.y);
furqan-ahm commented 9 months ago

You are adding the map to the game instance instead of to the world, swap it to world.add(map) and it should work.

Thank you, it seems I was using a bad practice of sorts that worked before the update. Adding it to the world seems to work fine.