英文:
Multiple issues after migrating the Flame package in Flutter
问题
以下是您提供的代码部分的翻译:
我有以下这个类,但在更新包时出现了多个错误。我无法记住之前的版本是什么,但之前是正常工作的,但现在我正在使用 flame: ^1.6.0。
class Monkey extends PositionComponent with HasHitboxes, Collidable, HasGameRef<MovingBackgroundGame> {
Monkey({Vector2? position, bool isVertical = false})
: super(
position: position,
size: Vector2(40, 80),
anchor: Anchor.topLeft,
);
bool collided = false;
HitboxShape hitbox = HitboxRectangle(relation: Vector2(1, 1));
@override
Future<void> onLoad() async {
addHitbox(hitbox);
super.onLoad();
}
以下是您提到的错误:
HasHitboxes
和Collidable
- 类只能混合(mix in)混入和类。HitboxShape
- 未定义类 'HitboxShape'。HitboxRectangle
- 方法 'HitboxRectangle' 未定义于类型 'Monkey'。addHitbox
- 方法 'addHitbox' 未定义于类型 'Monkey'。
英文:
I got the below class but having multiple errors when I update my package. I didn't manage to capture what was my previous version but it was working before but I'm using flame: ^1.6.0 now.
class Monkey extends PositionComponent with HasHitboxes, Collidable, HasGameRef<MovingBackgroundGame> {
Monkey({Vector2? position, bool isVertical = false})
: super(
position: position,
size: Vector2(40, 80),
anchor: Anchor.topLeft,
);
bool collided = false;
HitboxShape hitbox = HitboxRectangle(relation: Vector2(1, 1));
@override
Future<void> onLoad() async {
addHitbox(hitbox);
super.onLoad();
}
These are the following errors that I got. I tried to follow the documentation but haven't seen about these errors.
HasHitboxes
and Collidable
- Classes can only mix in mixins and classes.
HitboxShape
- Undefined class 'HitboxShape'.
HitboxRectangle
- The method 'HitboxRectangle' isn't defined for the type 'Monkey'.
addHitbox
- The method 'addHitbox' isn't defined for the type 'Monkey'.
答案1
得分: 1
HasHitboxes
和Collidable
mixins不再需要,所以你可以将它们移除。如果你想在此组件内对碰撞做出反应,可以添加CollisionCallbacks
。
然后,HitboxRectangle
已重命名为RectangleHitbox
,由于你设置了关系为(1, 1),所以在构造函数中实际上不需要添加任何内容,因为(1, 1)是默认值。
addHitbox
已被移除,因为现在碰撞框是普通组件。
在v1.6.0
中,该类将如下所示:
class Monkey extends PositionComponent with CollisionCallbacks, HasGameRef<MovingBackgroundGame> {
Monkey({Vector2? position, bool isVertical = false})
: super(
position: position,
size: Vector2(40, 80),
anchor: Anchor.topLeft,
);
RectangleHitbox hitbox = RectangleHitbox();
@override
Future<void> onLoad() async {
super.onLoad();
addHitbox(hitbox);
}
}
我猜测你之前可能在v1.0.0
版本上。
英文:
The HasHitboxes
and Collidable
mixins are no longer needed, so you can just remove those ones. You can add CollisionCallbacks
if you want to react to collisions within this component though.
Then the HitboxRectangle
has been renamed to RectangleHitbox
and since you set the relation to (1, 1) you don't actually have to add anything at all in its constructor since (1, 1) is the default.
addHitbox
is removed since hitboxes now are normal components.
This is what the class would look like in v1.6.0
:
class Monkey extends PositionComponent with CollisionCallbacks, HasGameRef<MovingBackgroundGame> {
Monkey({Vector2? position, bool isVertical = false})
: super(
position: position,
size: Vector2(40, 80),
anchor: Anchor.topLeft,
);
RectangleHitbox hitbox = RectangleHitbox();
@override
Future<void> onLoad() async {
super.onLoad();
addHitbox(hitbox);
}
}
I would guess that you were on maybe v1.0.0
before you upgraded.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论