mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 04:18:25 +08:00
Fixes in examples
This commit is contained in:
@ -8,26 +8,38 @@ import 'package:flutter/material.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Flame.images.loadAll(['creture.png', 'chopper.png']);
|
||||
|
||||
final Size size = await Flame.util.initialDimensions();
|
||||
final game = MyGame(size);
|
||||
runApp(game.widget);
|
||||
}
|
||||
|
||||
class MyGame extends BaseGame with TapDetector {
|
||||
final animation = flame_animation.Animation.sequenced('chopper.png', 4,
|
||||
textureWidth: 48, textureHeight: 48, stepTime: 0.15, loop: true);
|
||||
final animation = flame_animation.Animation.sequenced(
|
||||
'chopper.png',
|
||||
4,
|
||||
textureWidth: 48,
|
||||
textureHeight: 48,
|
||||
stepTime: 0.15,
|
||||
loop: true,
|
||||
);
|
||||
|
||||
void addAnimation(double x, double y) {
|
||||
const textureWidth = 291.0;
|
||||
const textureHeight = 178.0;
|
||||
final animationComponent = AnimationComponent.sequenced(
|
||||
291, 178, 'creature.png', 18,
|
||||
291,
|
||||
178,
|
||||
'creature.png',
|
||||
18,
|
||||
amountPerRow: 10,
|
||||
textureWidth: textureWidth,
|
||||
textureHeight: textureHeight,
|
||||
stepTime: 0.15,
|
||||
loop: true,
|
||||
destroyOnFinish: true);
|
||||
destroyOnFinish: true,
|
||||
);
|
||||
animationComponent.x = x - textureWidth / 2;
|
||||
animationComponent.y = y - textureHeight / 2;
|
||||
|
||||
@ -42,14 +54,15 @@ class MyGame extends BaseGame with TapDetector {
|
||||
MyGame(Size screenSize) {
|
||||
size = screenSize;
|
||||
|
||||
final animationComponent = AnimationComponent(100, 100, animation);
|
||||
animationComponent.x = size.width / 2 - 100;
|
||||
animationComponent.y = 100;
|
||||
const s = 100.0;
|
||||
final animationComponent = AnimationComponent(s, s, animation);
|
||||
animationComponent.x = size.width / 2 - s;
|
||||
animationComponent.y = s;
|
||||
|
||||
final reversedAnimationComponent =
|
||||
AnimationComponent(100, 100, animation.reversed());
|
||||
AnimationComponent(s, s, animation.reversed());
|
||||
reversedAnimationComponent.x = size.width / 2;
|
||||
reversedAnimationComponent.y = 100;
|
||||
reversedAnimationComponent.y = s;
|
||||
|
||||
add(animationComponent);
|
||||
add(reversedAnimationComponent);
|
||||
|
||||
@ -18,7 +18,9 @@ class MyGame extends BaseGame {
|
||||
|
||||
void _start() async {
|
||||
final animation = await flame_animation.Animation.fromAsepriteData(
|
||||
'chopper.png', 'chopper.json');
|
||||
'chopper.png',
|
||||
'chopper.json',
|
||||
);
|
||||
final animationComponent = AnimationComponent(200, 200, animation);
|
||||
|
||||
animationComponent.x = (size.width / 2) - 100;
|
||||
|
||||
@ -21,16 +21,17 @@ TextConfig regular = TextConfig(color: BasicPalette.white.color);
|
||||
AudioPool pool = AudioPool('laser.mp3');
|
||||
|
||||
class MyGame extends BaseGame with TapDetector {
|
||||
static final black = BasicPalette.black.paint;
|
||||
|
||||
MyGame(Size screenSize) {
|
||||
size = screenSize;
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
canvas.drawRect(Rect.fromLTWH(0.0, 0.0, size.width, size.height),
|
||||
BasicPalette.black.paint);
|
||||
regular.render(canvas, 'hit me!', Position.fromSize(size).div(2),
|
||||
anchor: Anchor.center);
|
||||
canvas.drawRect(Rect.fromLTWH(0.0, 0.0, size.width, size.height), black);
|
||||
final p = Position.fromSize(size).div(2);
|
||||
regular.render(canvas, 'hit me!', p, anchor: Anchor.center);
|
||||
super.render(canvas);
|
||||
}
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ class MyGame extends BaseGame with TapDetector {
|
||||
final TextConfig fpsTextConfig = TextConfig(color: const Color(0xFFFFFFFF));
|
||||
|
||||
final paint = Paint()..color = const Color(0xFFE5E5E5E5);
|
||||
final List<String> _animations = ["Stand", "Wave", "Jump", "Dance"];
|
||||
final List<String> _animations = ['Stand', 'Wave', 'Jump', 'Dance'];
|
||||
int _currentAnimation = 0;
|
||||
|
||||
FlareAnimation flareAnimation;
|
||||
@ -48,26 +48,26 @@ class MyGame extends BaseGame with TapDetector {
|
||||
}
|
||||
|
||||
void _start() async {
|
||||
flareAnimation = await FlareAnimation.load("assets/Bob_Minion.flr");
|
||||
flareAnimation.updateAnimation("Stand");
|
||||
flareAnimation = await FlareAnimation.load('assets/Bob_Minion.flr');
|
||||
flareAnimation.updateAnimation('Stand');
|
||||
|
||||
flareAnimation.width = 306;
|
||||
flareAnimation.height = 228;
|
||||
|
||||
final flareAnimation2 =
|
||||
FlareComponent("assets/Bob_Minion.flr", "Wave", 306, 228);
|
||||
FlareComponent('assets/Bob_Minion.flr', 'Wave', 306, 228);
|
||||
flareAnimation2.x = 50;
|
||||
flareAnimation2.y = 240;
|
||||
add(flareAnimation2);
|
||||
|
||||
final flareAnimation3 =
|
||||
FlareComponent("assets/Bob_Minion.flr", "Jump", 306, 228);
|
||||
FlareComponent('assets/Bob_Minion.flr', 'Jump', 306, 228);
|
||||
flareAnimation3.x = 50;
|
||||
flareAnimation3.y = 400;
|
||||
add(flareAnimation3);
|
||||
|
||||
final flareAnimation4 =
|
||||
FlareComponent("assets/Bob_Minion.flr", "Dance", 306, 228);
|
||||
FlareComponent('assets/Bob_Minion.flr', 'Dance', 306, 228);
|
||||
flareAnimation4.x = 50;
|
||||
flareAnimation4.y = 550;
|
||||
add(flareAnimation4);
|
||||
|
||||
@ -3,7 +3,7 @@ import 'package:flame/game.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class MyGame extends BaseGame {
|
||||
class MyGame extends Game {
|
||||
static final Paint paint = Paint()..color = const Color(0xFFFFFFFF);
|
||||
|
||||
var movingLeft = false;
|
||||
@ -24,19 +24,19 @@ class MyGame extends BaseGame {
|
||||
|
||||
final keyLabel = e.data.logicalKey.keyLabel;
|
||||
|
||||
if (keyLabel == "a") {
|
||||
if (keyLabel == 'a') {
|
||||
movingLeft = isKeyDown;
|
||||
}
|
||||
|
||||
if (keyLabel == "d") {
|
||||
if (keyLabel == 'd') {
|
||||
movingRight = isKeyDown;
|
||||
}
|
||||
|
||||
if (keyLabel == "w") {
|
||||
if (keyLabel == 'w') {
|
||||
movingUp = isKeyDown;
|
||||
}
|
||||
|
||||
if (keyLabel == "s") {
|
||||
if (keyLabel == 's') {
|
||||
movingDown = isKeyDown;
|
||||
}
|
||||
});
|
||||
|
||||
@ -29,9 +29,9 @@ class MyGame extends Game with KeyboardEvents {
|
||||
@override
|
||||
void onKeyEvent(e) {
|
||||
final bool isKeyDown = e is RawKeyDownEvent;
|
||||
if (e.data.keyLabel == "a") {
|
||||
if (e.data.keyLabel == 'a') {
|
||||
_dir = isKeyDown ? -1 : 0;
|
||||
} else if (e.data.keyLabel == "d") {
|
||||
} else if (e.data.keyLabel == 'd') {
|
||||
_dir = isKeyDown ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,15 +12,18 @@ void main() async {
|
||||
class MyGame extends BaseGame {
|
||||
MyGame() {
|
||||
final images = [
|
||||
ParallaxImage("bg.png"),
|
||||
ParallaxImage("mountain-far.png"),
|
||||
ParallaxImage("mountains.png"),
|
||||
ParallaxImage("trees.png"),
|
||||
ParallaxImage("foreground-trees.png"),
|
||||
ParallaxImage('bg.png'),
|
||||
ParallaxImage('mountain-far.png'),
|
||||
ParallaxImage('mountains.png'),
|
||||
ParallaxImage('trees.png'),
|
||||
ParallaxImage('foreground-trees.png'),
|
||||
];
|
||||
|
||||
final parallaxComponent = ParallaxComponent(images,
|
||||
baseSpeed: const Offset(20, 0), layerDelta: const Offset(30, 0));
|
||||
final parallaxComponent = ParallaxComponent(
|
||||
images,
|
||||
baseSpeed: const Offset(20, 0),
|
||||
layerDelta: const Offset(30, 0),
|
||||
);
|
||||
|
||||
add(parallaxComponent);
|
||||
}
|
||||
|
||||
@ -12,8 +12,13 @@ void main() async {
|
||||
}
|
||||
|
||||
class MyGame extends BaseGame {
|
||||
final animation = flame_animation.Animation.sequenced('chopper.png', 4,
|
||||
textureWidth: 48, textureHeight: 48, stepTime: 0.15);
|
||||
final animation = flame_animation.Animation.sequenced(
|
||||
'chopper.png',
|
||||
4,
|
||||
textureWidth: 48,
|
||||
textureHeight: 48,
|
||||
stepTime: 0.15,
|
||||
);
|
||||
|
||||
AnimationComponent buildAnimation() {
|
||||
final ac = AnimationComponent(100, 100, animation);
|
||||
|
||||
@ -35,7 +35,6 @@ class Ball extends PositionComponent {
|
||||
}
|
||||
|
||||
forward = !forward;
|
||||
print('boin');
|
||||
Flame.audio.play('boin.mp3', volume: 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ class TiledGame extends BaseGame {
|
||||
|
||||
void _addCoinsInMap(TiledComponent tiledMap) async {
|
||||
final ObjectGroup objGroup =
|
||||
await tiledMap.getObjectGroupFromLayer("AnimatedCoins");
|
||||
await tiledMap.getObjectGroupFromLayer('AnimatedCoins');
|
||||
if (objGroup == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@ dependencies:
|
||||
sdk: flutter
|
||||
flame:
|
||||
path: ../../../
|
||||
tiled: 0.6.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
@ -1,15 +1,19 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'component.dart';
|
||||
import 'package:flame/animation.dart';
|
||||
import '../animation.dart';
|
||||
|
||||
class AnimationComponent extends PositionComponent {
|
||||
Animation animation;
|
||||
Paint overridePaint;
|
||||
bool destroyOnFinish = false;
|
||||
|
||||
AnimationComponent(double width, double height, this.animation,
|
||||
{this.destroyOnFinish = false}) {
|
||||
AnimationComponent(
|
||||
double width,
|
||||
double height,
|
||||
this.animation, {
|
||||
this.destroyOnFinish = false,
|
||||
}) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
@ -54,8 +58,12 @@ class AnimationComponent extends PositionComponent {
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
prepareCanvas(canvas);
|
||||
animation.getSprite().render(canvas,
|
||||
width: width, height: height, overridePaint: overridePaint);
|
||||
animation.getSprite().render(
|
||||
canvas,
|
||||
width: width,
|
||||
height: height,
|
||||
overridePaint: overridePaint,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user