mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 10:38:17 +08:00
Unify examples structure (#1118)
* Animations, CameraAndViewport, CollisionDetection and Components unified * Added descriptions to effects * Rename input games * Unify input stories * Add info to parallax section * Added descriptions to the rendering examples * Add descriptions to the sprites directory * Fix utils and rendering section * Add descriptions to the widgets section * Delete directory that rebase brought back * Unify game names * Added some styleguide docs for examples * Fix analyze issues * All files should have _example as suffix * Made the FollowComponentExample a bit easier to understand * Change priority of ember
This commit is contained in:
49
examples/lib/stories/input/scroll_example.dart
Normal file
49
examples/lib/stories/input/scroll_example.dart
Normal file
@ -0,0 +1,49 @@
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ScrollExample extends FlameGame with ScrollDetector {
|
||||
static const String description = '''
|
||||
In this example we show how to use the `ScrollDetector`.\n\n
|
||||
Scroll within the canvas (both horizontally and vertically) and the white
|
||||
square will move around.
|
||||
''';
|
||||
|
||||
static const speed = 2000.0;
|
||||
final _size = Vector2.all(50);
|
||||
final _paint = BasicPalette.white.paint();
|
||||
|
||||
Vector2 position = Vector2.all(100);
|
||||
Vector2? target;
|
||||
|
||||
@override
|
||||
void onScroll(PointerScrollInfo info) {
|
||||
target = position + info.scrollDelta.game * 5;
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
super.render(canvas);
|
||||
canvas.drawRect(position.toPositionedRect(_size), _paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void update(double dt) {
|
||||
super.update(dt);
|
||||
final target = this.target;
|
||||
final ds = speed * dt;
|
||||
if (target != null) {
|
||||
if (position != target) {
|
||||
final diff = target - position;
|
||||
if (diff.length < ds) {
|
||||
position.setFrom(target);
|
||||
} else {
|
||||
diff.scaleTo(ds);
|
||||
position.setFrom(position + diff);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user