Implementing Timer and its examples

This commit is contained in:
erickzanardo
2019-06-25 12:50:44 -03:00
committed by Erick
parent 27cb0cfff5
commit 4b2288623e
6 changed files with 167 additions and 5 deletions

70
doc/examples/timer/.gitignore vendored Normal file
View 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

View 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

View File

@ -0,0 +1,3 @@
# timer
Flame game showcasing Timer class

View 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));
}
}

View 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

View File

@ -1,19 +1,36 @@
/// 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()) {
_running = false;
_callback();
if (_repeat) {
_current -= _limit;
} else {
_running = false;
}
if (_callback != null) {
_callback();
}
}
}
}
@ -26,7 +43,7 @@ class Timer {
_current = 0;
_running = true;
}
void stop() {
_current = 0;
_running = false;