flame tutorials- Adding input and graphics

flame tutorials- Adding input and graphics,第1张

flame tutorials- Adding input and graphics
  •  可移动的白色方块
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);
  }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5684932.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存