英文:
Multi touch detection In Flutter flame
问题
我想为拳击游戏实现游戏控制。
黑色部分分别表示上勾拳和左直拳。为了格挡,玩家应该使用多点触控,其中一个手指应该在屏幕的左半部,另一个手指在右半部。这显然会与其他控件重叠。
在Flame引擎(flutter)中处理多点触控,同时减少与其他控件的冲突的最佳方法是什么?
英文:
I want to implement game control for a boxing game.
Four sections in black represent uppercuts and jabs.
In order to block, the player should use multi-touch, where one figure should be on the left half of the screen and another figure on the right half. This is clearly overlapping other controls.
What is the best way to handle multi-touch while little conflicts with other controls in Flame engine (flutter).
答案1
得分: 1
因为这在GitHub问题中已经回答过了,如果其他人也遇到了这个问题,我也会在这里写下答案。
你可以在覆盖这些区域的组件上使用Tappable
或TapCallbacks
mixin,就像这样:
class MultiTapGame extends FlameGame with HasTappables {
@override
Future<void> onLoad() async {
debugMode = true;
await add(Controls());
}
}
class Controls extends Component with HasGameRef {
bool isLeftDown = false;
bool isRightDown = false;
@override
Future<void> onLoad() async {
await addAll(
[
TappableRegion(
position: Vector2.all(0),
size: Vector2(gameRef.size.x * 0.5, gameRef.size.y * 0.75),
onTapDownCallback: () {
isLeftDown = true;
if (isRightDown) {
print("Block");
} else {
print("Left Uppercut");
}
},
onTapUpCallback: () {
isLeftDown = false;
},
),
// 其他 TappableRegion 的配置...
],
);
}
}
class TappableRegion extends PositionComponent with Tappable {
TappableRegion({
super.position,
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
this.onTapDownCallback,
this.onTapUpCallback,
});
late RectangleComponent _rectangleComponent;
@override
FutureOr<void> onLoad() {
_rectangleComponent = RectangleComponent(
size: size,
paint: Paint()..color = Colors.green,
);
return super.onLoad();
}
// 其他方法...
}
英文:
Since this was replied to in the GitHub issue, I'll write the answer here too if anyone else stumbles upon this.
You can use Tappable
or TapCallbacks
mixin on a component that covers those areas, like this:
class MultiTapGame extends FlameGame with HasTappables {
@override
Future<void> onLoad() async {
debugMode = true;
await add(Controls());
}
}
class Controls extends Component with HasGameRef {
bool isLeftDown = false;
bool isRightDown = false;
@override
Future<void> onLoad() async {
await addAll(
[
TappableRegion(
position: Vector2.all(0),
size: Vector2(gameRef.size.x * 0.5, gameRef.size.y * 0.75),
onTapDownCallback: () {
isLeftDown = true;
if (isRightDown) {
print("Block");
} else {
print("Left Uppercut");
}
},
onTapUpCallback: () {
isLeftDown = false;
},
),
TappableRegion(
position: Vector2(gameRef.size.x * 0.5, 0),
size: Vector2(gameRef.size.x * 0.5, gameRef.size.y * 0.75),
onTapDownCallback: () {
isRightDown = true;
if (isLeftDown) {
print("Block");
} else {
print("Right Uppercut");
}
},
onTapUpCallback: () {
isRightDown = false;
},
),
TappableRegion(
position: Vector2(0, gameRef.size.y * 0.75),
size: Vector2(gameRef.size.x * 0.5, gameRef.size.y * 0.25),
onTapDownCallback: () {
isLeftDown = true;
if (isRightDown) {
print("Block");
} else {
print("Left Jab");
}
},
onTapUpCallback: () {
isLeftDown = false;
},
),
TappableRegion(
position: Vector2(gameRef.size.x * 0.5, gameRef.size.y * 0.75),
size: Vector2(gameRef.size.x * 0.5, gameRef.size.y * 0.25),
onTapDownCallback: () {
isRightDown = true;
if (isLeftDown) {
print("Block");
} else {
print("Right Jab");
}
},
onTapUpCallback: () {
isRightDown = false;
},
),
],
);
}
}
class TappableRegion extends PositionComponent with Tappable {
TappableRegion({
super.position,
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
this.onTapDownCallback,
this.onTapUpCallback,
});
late RectangleComponent _rectangleComponent;
@override
FutureOr<void> onLoad() {
_rectangleComponent = RectangleComponent(
size: size,
paint: Paint()..color = Colors.green,
);
return super.onLoad();
}
void Function()? onTapDownCallback;
void Function()? onTapUpCallback;
@override
bool onTapDown(TapDownInfo info) {
onTapDownCallback?.call();
add(_rectangleComponent);
return super.onTapDown(info);
}
@override
bool onTapUp(TapUpInfo info) {
onTapUpCallback?.call();
_rectangleComponent.removeFromParent();
return super.onTapUp(info);
}
@override
bool onTapCancel() {
onTapUpCallback?.call();
_rectangleComponent.removeFromParent();
return super.onTapCancel();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论