mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-01 01:18:38 +08:00 
			
		
		
		
	 bd130b711b
			
		
	
	bd130b711b
	
	
	
		
			
			The `visibleGameSize` should be based on the virtual size of the viewport, otherwise it won't work for `FixedResolutionViewport` for example. We noticed this when using the `ScreenHitbox` in a world that was looked upon by a camera with a `FixedResolutionViewport`.
		
			
				
	
	
		
			80 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			80 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:flame/collisions.dart';
 | |
| import 'package:flame/components.dart';
 | |
| import 'package:flame/events.dart';
 | |
| import 'package:flame/game.dart';
 | |
| import 'package:flutter/material.dart' hide Image, Draggable;
 | |
| 
 | |
| class CirclesExample extends FlameGame {
 | |
|   static const description = '''
 | |
|     This example will create a circle every time you tap on the screen. It will
 | |
|     have the initial velocity towards the center of the screen and if it touches
 | |
|     another circle both of them will change color.
 | |
|   ''';
 | |
| 
 | |
|   CirclesExample()
 | |
|       : super(
 | |
|           camera: CameraComponent.withFixedResolution(width: 600, height: 400),
 | |
|           world: MyWorld(),
 | |
|         );
 | |
| }
 | |
| 
 | |
| class MyWorld extends World with TapCallbacks, HasCollisionDetection {
 | |
|   MyWorld() : super(children: [ScreenHitbox()..debugMode = true]);
 | |
| 
 | |
|   @override
 | |
|   void onTapDown(TapDownEvent info) {
 | |
|     add(MyCollidable(position: info.localPosition));
 | |
|   }
 | |
| }
 | |
| 
 | |
| class MyCollidable extends PositionComponent
 | |
|     with HasGameReference<CirclesExample>, CollisionCallbacks {
 | |
|   MyCollidable({super.position})
 | |
|       : super(size: Vector2.all(30), anchor: Anchor.center);
 | |
| 
 | |
|   late Vector2 velocity;
 | |
|   final _collisionColor = Colors.amber;
 | |
|   final _defaultColor = Colors.cyan;
 | |
|   late ShapeHitbox hitbox;
 | |
| 
 | |
|   @override
 | |
|   Future<void> onLoad() async {
 | |
|     final defaultPaint = Paint()
 | |
|       ..color = _defaultColor
 | |
|       ..style = PaintingStyle.stroke;
 | |
|     hitbox = CircleHitbox()
 | |
|       ..paint = defaultPaint
 | |
|       ..renderShape = true;
 | |
|     add(hitbox);
 | |
|     velocity = -position
 | |
|       ..scaleTo(50);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void update(double dt) {
 | |
|     super.update(dt);
 | |
|     position.add(velocity * dt);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void onCollisionStart(
 | |
|     Set<Vector2> intersectionPoints,
 | |
|     PositionComponent other,
 | |
|   ) {
 | |
|     super.onCollisionStart(intersectionPoints, other);
 | |
|     hitbox.paint.color = _collisionColor;
 | |
|     if (other is ScreenHitbox) {
 | |
|       removeFromParent();
 | |
|       return;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void onCollisionEnd(PositionComponent other) {
 | |
|     super.onCollisionEnd(other);
 | |
|     if (!isColliding) {
 | |
|       hitbox.paint.color = _defaultColor;
 | |
|     }
 | |
|   }
 | |
| }
 |