使用 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;
}
}