1

使用 Flutter Flame 引擎和 AndroidStudio 制作游戏进行开发。渲染函数使用 canvas.save() 和 canvas.render() 函数确保帧之间的屏幕清晰(以防止重复的精灵绘制)。在 iPhone 模拟器上,应用程序按预期运行,但在多个 Android 模拟器上会呈现重复的精灵。无法弄清楚为什么会这样......任何帮助表示赞赏!

在此处输入图像描述

import 'dart:ui';
import 'package:flame/components/component.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';


class ThisGame extends Game {
  final SharedPreferences storage;
  Size screenSize;
  double tileSize;

  Player player;
  double spriteSize = 128;
  double timeElapsed = 0;

  ThisGame(this.storage) {
    initialize();
  }

  initialize() async {
    resize(await Flame.util.initialDimensions());
    player = Player();
    player.x = 0;
    player.y = 0;
  }

  @override
  void render(Canvas canvas) {
    canvas.save();
    player.render(canvas);
    canvas.restore();
  }

  @override
  void update(double t) {
    timeElapsed += t;
    if (timeElapsed > 1) {
      timeElapsed = 0;
      player.y += t * 300;
      player.x += t * 300;
    }
  }

  @override
  void resize(Size size) {
    screenSize = size;
    tileSize = screenSize.width / 9;
  }
}

class Player extends SpriteComponent {
  Player() : super.square(128.0, 'player.png') {
    this.angle = 0.0;
   }

}
4

1 回答 1

1

您不必自己处理渲染方法,只需将 SpriteComponent 添加到游戏中,它就会为您处理渲染。

class ThisGame extends Game {
  final SharedPreferences storage;
  Size screenSize;
  double tileSize;

  Player player;
  double spriteSize = 128;
  double timeElapsed = 0;

  ThisGame(this.storage) {
    initialize();
  }

  initialize() async {
    resize(await Flame.util.initialDimensions());
    player = Player();
    player.x = 0;
    player.y = 0;
    add(player); // <-- Like this
  }

  @override
  void update(double t) {
    super.update(t) // Also don't forget to call super
    timeElapsed += t;
    if (timeElapsed > 1) {
      timeElapsed = 0;
      player.y += t * 300;
      player.x += t * 300;
    }
  }

  @override
  void resize(Size size) {
    screenSize = size;
    tileSize = screenSize.width / 9;
  }
}

class Player extends SpriteComponent {
  Player() : super.square(128.0, 'player.png') {
    this.angle = 0.0;
  }
}

另外,不要忘记调用super.update(t)您的更新覆盖。

于 2020-06-08T17:31:29.007 回答