mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +08:00
Implementing Timer and its examples
This commit is contained in:
70
doc/examples/timer/.gitignore
vendored
Normal file
70
doc/examples/timer/.gitignore
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
# Miscellaneous
|
||||
*.class
|
||||
*.log
|
||||
*.pyc
|
||||
*.swp
|
||||
.DS_Store
|
||||
.atom/
|
||||
.buildlog/
|
||||
.history
|
||||
.svn/
|
||||
|
||||
# IntelliJ related
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea/
|
||||
|
||||
# Visual Studio Code related
|
||||
.vscode/
|
||||
|
||||
# Flutter/Dart/Pub related
|
||||
**/doc/api/
|
||||
.dart_tool/
|
||||
.flutter-plugins
|
||||
.packages
|
||||
.pub-cache/
|
||||
.pub/
|
||||
/build/
|
||||
|
||||
# Android related
|
||||
**/android/**/gradle-wrapper.jar
|
||||
**/android/.gradle
|
||||
**/android/captures/
|
||||
**/android/gradlew
|
||||
**/android/gradlew.bat
|
||||
**/android/local.properties
|
||||
**/android/**/GeneratedPluginRegistrant.java
|
||||
|
||||
# iOS/XCode related
|
||||
**/ios/**/*.mode1v3
|
||||
**/ios/**/*.mode2v3
|
||||
**/ios/**/*.moved-aside
|
||||
**/ios/**/*.pbxuser
|
||||
**/ios/**/*.perspectivev3
|
||||
**/ios/**/*sync/
|
||||
**/ios/**/.sconsign.dblite
|
||||
**/ios/**/.tags*
|
||||
**/ios/**/.vagrant/
|
||||
**/ios/**/DerivedData/
|
||||
**/ios/**/Icon?
|
||||
**/ios/**/Pods/
|
||||
**/ios/**/.symlinks/
|
||||
**/ios/**/profile
|
||||
**/ios/**/xcuserdata
|
||||
**/ios/.generated/
|
||||
**/ios/Flutter/App.framework
|
||||
**/ios/Flutter/Flutter.framework
|
||||
**/ios/Flutter/Generated.xcconfig
|
||||
**/ios/Flutter/app.flx
|
||||
**/ios/Flutter/app.zip
|
||||
**/ios/Flutter/flutter_assets/
|
||||
**/ios/ServiceDefinitions.json
|
||||
**/ios/Runner/GeneratedPluginRegistrant.*
|
||||
|
||||
# Exceptions to above rules.
|
||||
!**/ios/**/default.mode1v3
|
||||
!**/ios/**/default.mode2v3
|
||||
!**/ios/**/default.pbxuser
|
||||
!**/ios/**/default.perspectivev3
|
||||
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
|
||||
10
doc/examples/timer/.metadata
Normal file
10
doc/examples/timer/.metadata
Normal file
@ -0,0 +1,10 @@
|
||||
# This file tracks properties of this Flutter project.
|
||||
# Used by Flutter tool to assess capabilities and perform upgrades etc.
|
||||
#
|
||||
# This file should be version controlled and should not be manually edited.
|
||||
|
||||
version:
|
||||
revision: 0ba67226ee62d6c9d663a6f8410fb4b2f1076046
|
||||
channel: dev
|
||||
|
||||
project_type: app
|
||||
3
doc/examples/timer/README.md
Normal file
3
doc/examples/timer/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# timer
|
||||
|
||||
Flame game showcasing Timer class
|
||||
45
doc/examples/timer/lib/main.dart
Normal file
45
doc/examples/timer/lib/main.dart
Normal file
@ -0,0 +1,45 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/time.dart';
|
||||
import 'package:flame/text_config.dart';
|
||||
import 'package:flame/position.dart';
|
||||
|
||||
void main() {
|
||||
final game = MyGame();
|
||||
runApp(game.widget);
|
||||
|
||||
Flame.util.addGestureRecognizer(TapGestureRecognizer()
|
||||
..onTapDown = (TapDownDetails evt) {
|
||||
game.countdown.start();
|
||||
});
|
||||
}
|
||||
|
||||
class MyGame extends Game {
|
||||
final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF));
|
||||
Timer countdown;
|
||||
Timer interval;
|
||||
|
||||
int elapsedSecs = 0;
|
||||
|
||||
MyGame() {
|
||||
countdown = Timer(2);
|
||||
interval = Timer(1, repeat: true, callback: () {
|
||||
elapsedSecs += 1;
|
||||
});
|
||||
interval.start();
|
||||
}
|
||||
|
||||
@override
|
||||
void update(double dt) {
|
||||
countdown.update(dt);
|
||||
interval.update(dt);
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
textConfig.render(canvas, "Countdown: ${countdown.current.toString()}", Position(10, 100));
|
||||
textConfig.render(canvas, "Elapsed time: $elapsedSecs", Position(10, 150));
|
||||
}
|
||||
}
|
||||
17
doc/examples/timer/pubspec.yaml
Normal file
17
doc/examples/timer/pubspec.yaml
Normal file
@ -0,0 +1,17 @@
|
||||
name: timer
|
||||
description: Example app using Timer class
|
||||
|
||||
version: 1.0.0+1
|
||||
|
||||
environment:
|
||||
sdk: ">=2.0.0-dev.68.0 <3.0.0"
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
flame:
|
||||
path: ../../../
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
@ -1,22 +1,39 @@
|
||||
|
||||
/// Simple utility class that helps handling time counting and implementing interval like events.
|
||||
///
|
||||
class Timer {
|
||||
final double _limit;
|
||||
void Function() _callback;
|
||||
double _current;
|
||||
bool _repeat;
|
||||
double _current = 0;
|
||||
bool _running = false;
|
||||
|
||||
Timer(this._limit, this._callback);
|
||||
Timer(this._limit, { repeat = false, callback }) {
|
||||
_repeat = repeat;
|
||||
_callback = callback;
|
||||
}
|
||||
|
||||
double get current {
|
||||
return _current;
|
||||
}
|
||||
|
||||
void update(double dt) {
|
||||
if (_running) {
|
||||
_current += dt;
|
||||
|
||||
if (isFinished()) {
|
||||
if (_repeat) {
|
||||
_current -= _limit;
|
||||
} else {
|
||||
_running = false;
|
||||
}
|
||||
|
||||
if (_callback != null) {
|
||||
_callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool isFinished() {
|
||||
return _current >= _limit;
|
||||
|
||||
Reference in New Issue
Block a user