So I'm trying to fade out a Tiled Layer in Flutter Flame and I'm finding that regardless of what value I set on the layer's opacity property the opacity is not updating.
This is what I have so far:
class HiddenSwitch extends PositionComponent
with HasGameRef<TheGame>, CollisionCallbacks {
HiddenSwitch({
super.position,
super.size,
});
bool visible = false;
bool offLayerVisible = false;
bool onLayerVisible = false;
double lifeTime = 0.8;
double fadeTime = 2;
double elapsedTime = 0.0;
bool initiateFade = false;
void collidedWithPlayer() {
initiateFade = visible = !visible;
}
double map(
double value, double start1, double stop1, double start2, double stop2) {
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
}
@override
void update(double dt) {
// Update elapsed time
elapsedTime += dt;
if (initiateFade) {
final backgroundOff = game.tileMap.getLayer("BackgroundOff");
if (backgroundOff != null) {
// Calculate the opacity based on the elapsed time and fade time
double opacity = 1.0 - (elapsedTime / fadeTime).clamp(0.0, 1.0);
// Set the opacity of the BackgroundOff layer
backgroundOff.opacity = opacity;
// Check if the opacity has reached 0, indicating the end of the fade
if (opacity <= 0.0) {
initiateFade = false;
}
}
}
super.update(dt);
}
@override
FutureOr<void> onLoad() async {
add(RectangleHitbox());
return super.onLoad();
}
}