- 可移动的白色方块
import 'dart:ui'; import 'package:flame/components.dart'; import 'package:flame/game.dart'; import 'package:flame/input.dart'; import 'package:flutter/material.dart'; class SpaceShooterGame1 extends FlameGame with PanDetector{ late Player1 player; @override Future? onLoad() async{ // TODO: implement onLoad await super.onLoad(); player=Player1(); add( player..position=size/2 ..width=50 ..height=100 ..anchor=Anchor.center ); } @override void onPanUpdate(DragUpdateInfo info) { player.move(info.delta.game); } } class Player1 extends PositionComponent{ static final _paint=Paint()..color=Colors.white; @override void render(Canvas canvas) { canvas.drawRect(size.toRect(), _paint); } void move(Vector2 delta){ position.add(delta); } }
- 可移动的精灵一
import 'dart:ui'; import 'package:flame/components.dart'; import 'package:flame/game.dart'; import 'package:flame/input.dart'; import 'package:flutter/material.dart'; class SpaceShooterGame2 extends FlameGame with PanDetector{ late Player2 player; @override Future? onLoad() async{ await super.onLoad(); player=Player2(); final playerSprite=await loadSprite("bloc_logo_small.png"); add( player..position=size/2 ..sprite=playerSprite ..width=50 ..height=100 ..anchor=Anchor.center ); } @override void onPanUpdate(DragUpdateInfo info) { player.move(info.delta.game); } } class Player2 extends SpriteComponent{ void move(Vector2 delta){ position.add(delta); } }
- 可移动的精灵二
import 'dart:ui'; import 'package:flame/components.dart'; import 'package:flame/game.dart'; import 'package:flame/input.dart'; import 'package:flutter/material.dart'; class SpaceShooterGame3 extends FlameGame with PanDetector{ late Player3 player; @override Future? onLoad() async{ await super.onLoad(); player=Player3(); add(player); } @override void onPanUpdate(DragUpdateInfo info) { player.move(info.delta.game); } } class Player3 extends SpriteComponent with HasGameRef { @override Future ? onLoad() async{ await super.onLoad(); sprite=await gameRef.loadSprite("bloc_logo_small.png"); position=gameRef.size/2; width=100; height=150; anchor=Anchor.center; } void move(Vector2 delta){ position.add(delta); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)