browserloops-examples/lib/src/01-WhileLoop.dart

42 lines
1,015 B
Dart
Raw Normal View History

2018-07-20 09:58:11 +00:00
import 'package:browserloop/game/Game.dart';
import 'package:browserloop/game/LoopExample.dart';
/// The most basic update loop possible.
///
/// Best not to use this loop since it has a tendency to
/// crash your browser and not load correctly at all.
/// The way to get this to run would be the setInterval method
/// simple is either through setInterval in Javascript:
/// https://www.w3schools.com/howto/howto_js_animate.asp
/// or through the Timer API in Dart, especially
/// with Timer.periodic(16, callbackFunction);
/// To see how I got it running for the example,
/// see file 02-AnimationFrameWhile.dart
2018-07-20 14:28:29 +00:00
class SimpleLoop implements LoopExample {
2018-07-20 09:58:11 +00:00
Game game;
2018-07-20 14:28:29 +00:00
bool running = false;
2018-07-20 09:58:11 +00:00
2018-07-20 17:02:54 +00:00
SimpleLoop(Game this.game);
2018-07-20 09:58:11 +00:00
void eventloop() {
2018-07-20 17:02:54 +00:00
while (running) {
2018-07-20 14:28:29 +00:00
update();
2018-07-20 09:58:11 +00:00
}
}
void update() {
2018-07-20 14:28:29 +00:00
game.update();
game.draw();
2018-07-20 09:58:11 +00:00
}
2018-07-20 14:28:29 +00:00
// Starting and stopping the loop for the example page
void stop() {
running = false;
}
void start() {
2018-07-20 17:02:54 +00:00
eventloop();
2018-07-20 14:28:29 +00:00
running = true;
}
}