mirror of
https://github.com/flame-engine/flame.git
synced 2025-10-30 16:36:57 +08:00
Remove unnecessary super.render calls (#1084)
* Remove unnecessary super.render calls * Update tutorial and docs
This commit is contained in:
@ -83,7 +83,6 @@ class GameOverPanel extends PositionComponent with HasGameRef<MyGame> {
|
|||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
if (visible) {
|
if (visible) {
|
||||||
super.render(canvas);
|
|
||||||
} // If not visible none of the children will be rendered
|
} // If not visible none of the children will be rendered
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,10 +100,6 @@ render from the top left corner (0.0). Your render method should not handle wher
|
|||||||
component should be rendered. To handle where and how your component should be rendered use the
|
component should be rendered. To handle where and how your component should be rendered use the
|
||||||
`position`, `angle` and `anchor` properties and flame will automatically handle the rest for you.
|
`position`, `angle` and `anchor` properties and flame will automatically handle the rest for you.
|
||||||
|
|
||||||
If you really want to handle the canvas translations yourself you can just omit the
|
|
||||||
`super.render(canvas)` line and suppress the warning, but for most use cases this is not
|
|
||||||
recommended.
|
|
||||||
|
|
||||||
If you want to know where on the screen the bounding box of the component is you can use the
|
If you want to know where on the screen the bounding box of the component is you can use the
|
||||||
`toRect` method.
|
`toRect` method.
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ that extends from `Game`. Once applied you can access the current fps by using t
|
|||||||
like shown in the example below.
|
like shown in the example below.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
class MyGame extends Game with FPSCounter {
|
class MyGame extends FlameGame with FPSCounter {
|
||||||
static final fpsTextConfig = TextConfig(color: BasicPalette.white.color);
|
static final fpsTextConfig = TextConfig(color: BasicPalette.white.color);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@ -57,7 +57,6 @@ class Background extends PositionComponent with HasGameRef {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
super.render(c);
|
|
||||||
c.drawRect(hugeRect, white);
|
c.drawRect(hugeRect, white);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,7 +47,6 @@ class MovableSquare extends SquareComponent
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
super.render(c);
|
|
||||||
final text = '(${x.toInt()}, ${y.toInt()})';
|
final text = '(${x.toInt()}, ${y.toInt()})';
|
||||||
textRenderer.render(c, text, size / 2, anchor: Anchor.center);
|
textRenderer.render(c, text, size / 2, anchor: Anchor.center);
|
||||||
}
|
}
|
||||||
@ -96,7 +95,6 @@ class Map extends Component {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawRect(bounds, _paintBg);
|
canvas.drawRect(bounds, _paintBg);
|
||||||
canvas.drawRect(bounds, _paintBorder);
|
canvas.drawRect(bounds, _paintBorder);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,7 +51,6 @@ class MyCollidable extends PositionComponent
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
renderHitboxes(canvas);
|
renderHitboxes(canvas);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -76,7 +76,6 @@ abstract class MyCollidable extends PositionComponent
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
renderHitboxes(canvas, paint: _activePaint);
|
renderHitboxes(canvas, paint: _activePaint);
|
||||||
if (_isDragged) {
|
if (_isDragged) {
|
||||||
final localCenter = (scaledSize / 2).toOffset();
|
final localCenter = (scaledSize / 2).toOffset();
|
||||||
|
|||||||
@ -30,7 +30,6 @@ class Square extends PositionComponent with HasGameRef<Priority>, Tappable {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawRect(size.toRect(), paint);
|
canvas.drawRect(size.toRect(), paint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -107,7 +107,6 @@ class Compass extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawCircle(Offset(_radius, _radius), _radius, _bgPaint);
|
canvas.drawCircle(Offset(_radius, _radius), _radius, _bgPaint);
|
||||||
canvas.drawPath(_marksPath, _marksPaint);
|
canvas.drawPath(_marksPath, _marksPaint);
|
||||||
}
|
}
|
||||||
@ -144,7 +143,6 @@ class CompassArrow extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawPath(_northPath, _northPaint);
|
canvas.drawPath(_northPath, _northPaint);
|
||||||
canvas.drawPath(_southPath, _southPaint);
|
canvas.drawPath(_southPath, _southPaint);
|
||||||
}
|
}
|
||||||
@ -193,7 +191,6 @@ class CompassRim extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawCircle(Offset(_radius, _radius), _radius - _width / 2, _bgPaint);
|
canvas.drawCircle(Offset(_radius, _radius), _radius - _width / 2, _bgPaint);
|
||||||
canvas.drawCircle(Offset(_radius, _radius), _radius - _width, _marksPaint);
|
canvas.drawCircle(Offset(_radius, _radius), _radius - _width, _marksPaint);
|
||||||
canvas.drawPath(_marksPath, _marksPaint);
|
canvas.drawPath(_marksPath, _marksPaint);
|
||||||
|
|||||||
@ -15,7 +15,6 @@ class HoverableSquare extends PositionComponent with Hoverable {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawRect(size.toRect(), isHovered ? _grey : _white);
|
canvas.drawRect(size.toRect(), isHovered ? _grey : _white);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,6 @@ class TappableSquare extends PositionComponent with Tappable {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawRect(size.toRect(), currentPaint);
|
canvas.drawRect(size.toRect(), currentPaint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,6 @@ class TappableSquare extends PositionComponent with Tappable {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawRect(size.toRect(), _beenPressed ? _grey : _white);
|
canvas.drawRect(size.toRect(), _beenPressed ? _grey : _white);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -561,7 +561,6 @@ class TrafficLightComponent extends Component {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
super.render(c);
|
|
||||||
c.drawRect(rect, Paint()..color = currentColor);
|
c.drawRect(rect, Paint()..color = currentColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,6 @@ class RenderedTimeComponent extends TimerComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
textPaint.render(
|
textPaint.render(
|
||||||
canvas,
|
canvas,
|
||||||
'Elapsed time: ${timer.current.toStringAsFixed(3)}',
|
'Elapsed time: ${timer.current.toStringAsFixed(3)}',
|
||||||
|
|||||||
@ -20,6 +20,7 @@
|
|||||||
- `randomColor` method in the `Color` extension
|
- `randomColor` method in the `Color` extension
|
||||||
- Calling super-method in `.render()` is now optional
|
- Calling super-method in `.render()` is now optional
|
||||||
- Components that manipulate canvas state are now responsible for saving/restoring that state
|
- Components that manipulate canvas state are now responsible for saving/restoring that state
|
||||||
|
- Remove `super.render` calls that are no longer needed
|
||||||
- Fixed typo in error message
|
- Fixed typo in error message
|
||||||
|
|
||||||
## [1.0.0-releasecandidate.16]
|
## [1.0.0-releasecandidate.16]
|
||||||
|
|||||||
@ -26,8 +26,6 @@ class Square extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
super.render(c);
|
|
||||||
|
|
||||||
c.drawRect(size.toRect(), white);
|
c.drawRect(size.toRect(), white);
|
||||||
c.drawRect(const Rect.fromLTWH(0, 0, 3, 3), red);
|
c.drawRect(const Rect.fromLTWH(0, 0, 3, 3), red);
|
||||||
c.drawRect(Rect.fromLTWH(width / 2, height / 2, 3, 3), blue);
|
c.drawRect(Rect.fromLTWH(width / 2, height / 2, 3, 3), blue);
|
||||||
|
|||||||
@ -36,8 +36,6 @@ class CustomPainterComponent extends PositionComponent {
|
|||||||
@override
|
@override
|
||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
|
|
||||||
painter?.paint(canvas, size.toSize());
|
painter?.paint(canvas, size.toSize());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -66,8 +66,6 @@ class IsometricTileMapComponent extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
super.render(c);
|
|
||||||
|
|
||||||
final size = effectiveTileSize;
|
final size = effectiveTileSize;
|
||||||
for (var i = 0; i < matrix.length; i++) {
|
for (var i = 0; i < matrix.length; i++) {
|
||||||
for (var j = 0; j < matrix[i].length; j++) {
|
for (var j = 0; j < matrix[i].length; j++) {
|
||||||
|
|||||||
@ -36,7 +36,6 @@ class NineTileBoxComponent extends PositionComponent {
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
@override
|
@override
|
||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
super.render(c);
|
|
||||||
nineTileBox.drawRect(c, size.toRect());
|
nineTileBox.drawRect(c, size.toRect());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -116,7 +116,6 @@ class ParallaxComponent<T extends FlameGame> extends PositionComponent
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
parallax?.render(canvas);
|
parallax?.render(canvas);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,6 @@ class ParticleComponent extends Component {
|
|||||||
/// [Particle] within this [Component].
|
/// [Particle] within this [Component].
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
particle.render(canvas);
|
particle.render(canvas);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -34,7 +34,6 @@ class ShapeComponent extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
shape.render(canvas, paint);
|
shape.render(canvas, paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -91,7 +91,6 @@ class SpriteAnimationComponent extends PositionComponent with HasPaint {
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
animation?.getSprite().render(
|
animation?.getSprite().render(
|
||||||
canvas,
|
canvas,
|
||||||
size: size,
|
size: size,
|
||||||
|
|||||||
@ -102,7 +102,6 @@ class SpriteAnimationGroupComponent<T> extends PositionComponent with HasPaint {
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
animation?.getSprite().render(
|
animation?.getSprite().render(
|
||||||
canvas,
|
canvas,
|
||||||
size: size,
|
size: size,
|
||||||
|
|||||||
@ -21,7 +21,6 @@ class SpriteBatchComponent extends Component {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
spriteBatch?.render(
|
spriteBatch?.render(
|
||||||
canvas,
|
canvas,
|
||||||
blendMode: blendMode,
|
blendMode: blendMode,
|
||||||
|
|||||||
@ -71,7 +71,6 @@ class SpriteComponent extends PositionComponent with HasPaint {
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
sprite?.render(
|
sprite?.render(
|
||||||
canvas,
|
canvas,
|
||||||
size: size,
|
size: size,
|
||||||
|
|||||||
@ -48,7 +48,6 @@ class SpriteGroupComponent<T> extends PositionComponent with HasPaint {
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
sprite?.render(
|
sprite?.render(
|
||||||
canvas,
|
canvas,
|
||||||
size: size,
|
size: size,
|
||||||
|
|||||||
@ -187,7 +187,6 @@ class TextBoxComponent<T extends TextRenderer> extends PositionComponent {
|
|||||||
if (_cache == null) {
|
if (_cache == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
super.render(c);
|
|
||||||
c.save();
|
c.save();
|
||||||
c.scale(1 / pixelRatio);
|
c.scale(1 / pixelRatio);
|
||||||
c.drawImage(_cache!, Offset.zero, _imagePaint);
|
c.drawImage(_cache!, Offset.zero, _imagePaint);
|
||||||
|
|||||||
@ -57,7 +57,6 @@ class TextComponent<T extends TextRenderer> extends PositionComponent {
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
_textRenderer.render(canvas, text, Vector2.zero());
|
_textRenderer.render(canvas, text, Vector2.zero());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,7 +33,6 @@ class MyComponent extends PositionComponent with Tappable, HasGameRef {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
isRenderCalled = true;
|
isRenderCalled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,6 @@ class TestComponent extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
super.render(c);
|
|
||||||
c.drawRect(size.toRect(), _paint);
|
c.drawRect(size.toRect(), _paint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -100,7 +100,6 @@ class BGComponent extends Component with HasGameRef {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas c) {
|
void render(Canvas c) {
|
||||||
super.render(c);
|
|
||||||
c.drawRect(gameRef.size.toRect(), paint);
|
c.drawRect(gameRef.size.toRect(), paint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,13 +20,13 @@ class FlareActorComponent extends PositionComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@mustCallSuper
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
flareAnimation.render(canvas, size);
|
flareAnimation.render(canvas, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@mustCallSuper
|
|
||||||
@override
|
@override
|
||||||
|
@mustCallSuper
|
||||||
void update(double dt) {
|
void update(double dt) {
|
||||||
super.update(dt);
|
super.update(dt);
|
||||||
flareAnimation.advance(dt);
|
flareAnimation.advance(dt);
|
||||||
|
|||||||
@ -5,7 +5,6 @@ import 'dart:ui' as ui;
|
|||||||
import 'package:flame/components.dart';
|
import 'package:flame/components.dart';
|
||||||
import 'package:flutter/rendering.dart';
|
import 'package:flutter/rendering.dart';
|
||||||
import 'package:rive/rive.dart';
|
import 'package:rive/rive.dart';
|
||||||
|
|
||||||
// ignore_for_file: implementation_imports
|
// ignore_for_file: implementation_imports
|
||||||
import 'package:rive/src/rive_core/math/aabb.dart';
|
import 'package:rive/src/rive_core/math/aabb.dart';
|
||||||
import 'package:rive/src/rive_core/math/mat2d.dart';
|
import 'package:rive/src/rive_core/math/mat2d.dart';
|
||||||
@ -47,7 +46,6 @@ class RiveComponent extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(ui.Canvas canvas) {
|
void render(ui.Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
_renderer.render(canvas, size.toSize());
|
_renderer.render(canvas, size.toSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,6 @@ class SvgComponent extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
svg.render(canvas, size);
|
svg.render(canvas, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,6 @@ class TiledComponent extends Component {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
tileMap.render(canvas);
|
tileMap.render(canvas);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,6 @@ class Player extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawRect(size.toRect(), _paint);
|
canvas.drawRect(size.toRect(), _paint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -45,7 +45,6 @@ class Player extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawRect(size.toRect(), _paint);
|
canvas.drawRect(size.toRect(), _paint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,7 +41,6 @@ class Player extends PositionComponent {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void render(Canvas canvas) {
|
void render(Canvas canvas) {
|
||||||
super.render(canvas);
|
|
||||||
canvas.drawRect(size.toRect(), _paint);
|
canvas.drawRect(size.toRect(), _paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,7 +132,7 @@ removed from the Flutter widget tree.
|
|||||||
|
|
||||||
Before we close this step, there is one small improvement that we can do. Right now, we are loading
|
Before we close this step, there is one small improvement that we can do. Right now, we are loading
|
||||||
the sprite and passing it to our component. For now, this may seen fine, but imagine a game with
|
the sprite and passing it to our component. For now, this may seen fine, but imagine a game with
|
||||||
a lot of components; if the game is responsible for loading assets for all coponents, our code can
|
a lot of components; if the game is responsible for loading assets for all components, our code can
|
||||||
become a mess quite fast.
|
become a mess quite fast.
|
||||||
|
|
||||||
Just like `FlameGame`, components also have an `onLoad` method that can be overridden to do
|
Just like `FlameGame`, components also have an `onLoad` method that can be overridden to do
|
||||||
|
|||||||
Reference in New Issue
Block a user