Util utility class


Position

Dart and Flutter both have several different classes to deal with the two-dimensional two-point concept among the various modules needed to build the game. Particularly common in the API are math.point and UI.offset.

The Position class is a utility class that facilitates easy conversions with these classes.

It is also different from the default implementation of these providing classes (math.point and UI.offset) because it is mutable and provides some useful manipulation methods.

Util class


This class can be accessed through Flame. This ray contains sparse functions that are independent and easy to use.

It is recommended that functions in this class be called through the Flame. Util getter to take advantage of a single instance prepared by the Flame engine.

Flame.util.fullScrren()

When called, this will disable all SystemUiOverlay and make the application full screen. Make your app full screen (no top and bottom bars) when blocking calls from main

Flame.util.setLanscape()

This method sets the orientation of the entire application (effectively, including the game) to landscape, depending on the operating system and device Settings, and should allow left and right landscape orientation. In order to set up the direction for the specific direction of landscape, using Flame. Util. SetLandscapeLeftOnly or Flame. Util. SetLandscapeRightOnly.

Flame.util.setPortrait()

This method sets the orientation of the entire application (effectively, including the game) to portrait, depending on the operating system and device Settings, and the application allows up and down portrait orientation. In order to set the direction of the application for a specific vertical screen, using Flame. Util. SetPortraitUpOnly or Flame. Util. SetPortraitDownOnly.

Flame. Util. SetOrientation () and Flame. Util. SetOrientations ()

If you need better control over the allowed direction (without having to deal directly with SystemChrome), You can use setOrientation(accept a single DeviceOrientation as a parameter),setOrientations(accept a List as a possible orientation).

** Note: ** calls this method first in the asynchronous primary system, waiting for its value to avoid size errors that affect specific devices when building for production.

Flame.util.addGestureRecognizer()andFlame.util.removeGestureRecognizer

These two functions help register (unregister) gesture recognition so that the game allows input. These two functions are right here.

Other functions

  • Text: here
  • DrawWhere: A very simple function that manually applies the offset value toCanvasOn. Render the content provided by the function, and then reset the canvas without using the canvas’s built-in save/restore function. This could be very useful becauseBaseGameUsing the state of the canvas, you should not confuse it.

The Timer Timer


Flame provides a simple user class to help you handle events with countdowns and similar events.

Countdown examples:

import 'dart:ui';
import 'package:flame/game.dart';
import 'package:flame/position.dart';
import 'package:flame/text_confgi.dart';
import 'package:flame/time.dart';

class MyGame extends Game {
    final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF));
    final countdown = Timer(2);
    
    MyGame() {
        countdown.start();
    }
    
    @override
    void update(double dt) {
        countdown.update(dt);
        if(countdown.isFinished()) {
            // do something ...
        }
    }
    
    @override
    void render(Canvas canvas) {
        textConfig.render(canvas, "Countdown: ${countdown.current.toString()}",Position(10, 100); }}Copy the code

Interval examples:

import 'dart:ui';
import 'package:flame/game.dart';
import 'package:flame/position.dart';
import 'package:flame/text_config.dart';
import 'package:flame/time.dart';

class MyGame extends Game {
    final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF));
    Timer interval;
    
    int elapsedSecs = 0;
    
    
    MyGame() {
        interval = Timer(1, repeat: true, callback: () {
            elapsedSecs += 1;
        });
        interval.start();
    }
    
    @override
    void update(double dt) {
        interval.update(dt);
    }
    
    @override
    void render(Canvas canvas) {
        textConfig.render(canvas, "Elapsed time: $elapsedSecs", Position(10, 150)); }}Copy the code

Timer instances can also be used within BaseGame games via the TimerComponent.

Timer Component

import 'package:flame/time.dart';
import 'package:flame/components/timer_component.dart';
import 'package:flame/game.dart';

class MyBaseGame extends BaseGame {
    MyBaseGaem() {
        add(
            TimerComponent(
                Timer(10, repeat: true, callback: () {
                    print("10 seconds elapsed"); }).. start() ); }}Copy the code