在Flutter/Flame游戏中从不同的文件向世界添加组件



我的游戏有几个组件涉及分层。我还计划有多个级别,这些级别会改变这些组件的位置。我把第一个关卡编码到主游戏文件中(不是main.dart,而是相当于myGame.dart的文件(,但现在我想把它移到它自己的类中,以便更容易地更改关卡。我有一个5乘5的网格,底部有一个字符,中间有一个苹果。当我在myGame.dart文件中有一级块代码时,苹果不会显示,但块和字符会显示。现在我已经将块代码移动到levelOne.dart文件中,块不会显示,但有苹果和字符。这是我将其移动到新文件后的代码。

myGame.dart:

final character = Character()
..size = squareSize
..position = Vector2(
squareGap + (0+4) * (squareWidth + squareGap),
(squareHeight*4) + 5 * squareGap,);
//..position = Vector2(squareGap, squareGap);
final apple = Apple()
..size = squareSize
..position = Vector2( squareGap + (0+4) * (squareWidth + squareGap),
(squareHeight*2) + 3 * squareGap,);
//originally all of the lists of blocks in the levelOne.dart file were here, but it got moved to the file and replaced with the following line
final blocks = LevelOne();
final world = World()
..add(character)
..add(apple)
//originally all of the lists of blocks were in here separately, replaced with the following line
..add(blocks);

add(world);

levelOne.dart:

class LevelOne extends PositionComponent {
static const double squareWidth = 1000.0;
static const double squareHeight = 1000.0;
static const double squareGap = 175.0;
static const double squareRadius = 100.0;
static final Vector2 squareSize = Vector2(squareWidth, squareHeight);
final blocks5 = List.generate(
5,
(i) => GrassBlocks()
..size = squareSize
..position =
Vector2((i + 2) * (squareWidth + squareGap) + squareGap, squareGap),
);
final blocks4 = List.generate(
5,
(i) => GrassBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+2) * (squareWidth + squareGap),
squareHeight + 2 * squareGap,),
);
final blocks3P1 = List.generate(
2,
(i) => GrassBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+2) * (squareWidth + squareGap),
(squareHeight*2) + 3 * squareGap,),
);
final blocks3P2 = List.generate(
1,
(i) => PavementBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+4) * (squareWidth + squareGap),
(squareHeight*2) + 3 * squareGap,),
);
final blocks3P3 = List.generate(
2,
(i) => GrassBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+5) * (squareWidth + squareGap),
(squareHeight*2) + 3 * squareGap,),
);
final blocks2P1 = List.generate(
2,
(i) => GrassBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+2) * (squareWidth + squareGap),
(squareHeight*3) + 4 * squareGap,),
);
final blocks2P2 = List.generate(
1,
(i) => PavementBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+4) * (squareWidth + squareGap),
(squareHeight*3) + 4 * squareGap,),
);
final blocks2P3 = List.generate(
2,
(i) => GrassBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+5) * (squareWidth + squareGap),
(squareHeight*3) + 4 * squareGap,),
);
final blocks1P1 = List.generate(
2,
(i) => GrassBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+2) * (squareWidth + squareGap),
(squareHeight*4) + 5 * squareGap,),
);
final blocks1P2 = List.generate(
1,
(i) => PavementBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+4) * (squareWidth + squareGap),
(squareHeight*4) + 5 * squareGap,),
);
final blocks1P3 = List.generate(
2,
(i) => GrassBlocks()
..size = squareSize
..position = Vector2(
squareGap + (i+5) * (squareWidth + squareGap),
(squareHeight*4) + 5 * squareGap,),
);
}

我曾尝试将PositionComponent更改为SpriteComponent,但由于此代码不直接包含Sprite(称为SpriteComponents类(,因此无法成功。我如何让移动的代码显示levelOne.dart中的所有块,并将苹果放在上面?

当您将组件添加到世界中时,如果未设置priority,它们将按照添加的顺序进行渲染。你可以把priority看作Flame中的Z索引。

所以在你这样做的情况下:

final world = World()
..add(character)
..add(apple)
//originally all of the lists of blocks were in here separately, replaced with the following line
..add(blocks);

blocks将渲染在角色和苹果的顶部,使它们不可见。要解决此问题,您可以先添加blocks,也可以将characterapplepriority设置为高于0的值(默认值(。

优先级可以设置在调用超级构造函数的任意目录中:

class Test extends PositionCompontent {
Test() : super(priority: 1);
}

或者可以在运行时使用进行更改

final test = Test();
add(test);
test.priority = 1;

最新更新