如何资产图像颤动?

时间:2021-04-15 09:22:29

标签: flutter flutter-assetimage

请求许可,这里我有一些代码,我想问一下,如何将“盒子”的形状更改为多个图像。所以当你第一次点击它会改变图像并且它会掉落,在随机响应改变到下一个图像之后。

(main.dart)

import 'package:flutter/material.dart';
import 'package:flame/flame.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_app/box-game.dart';
import 'package:flutter/gestures.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  //Util flameUtil = Util();
  await Flame.util.fullScreen();
  await Flame.util.setOrientation(DeviceOrientation.portraitUp);

  BoxGame game = BoxGame();
 runApp(MaterialApp(
    home: Scaffold(
      body: Stack(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTapDown: (TapDownDetails d) {
              // place your tap down handler code here
              //print('outer tap down');
              game.onTapDown(d);
            },

  child: game.widget,
          ),
          Text('This is the HUD UI'),
        ],
      ),
    ),
  ));
}`enter code here`

1 个答案:

答案 0 :(得分:0)

(box-game.dart)

import 'dart:ui';
import 'package:flame/game.dart';
import 'package:flame/flame.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/components/fly.dart';
import 'dart:math';
import 'package:flutter/gestures.dart';

class BoxGame extends Game {
  Size screenSize;
  double tileSize;
  // List<Fly> flies;
  List<Fly> flies = <Fly>[];

  Random rnd;

  BoxGame() {
    initialize();
}
void initialize() async {
    // flies = List<Fly>();
    flies = <Fly>[];
    rnd = Random();
    resize(await Flame.util.initialDimensions());

    spawnFly();
  }

  void spawnFly() {
    //flies.add(Fly(this, 50, 50));
    double x = rnd.nextDouble() * (screenSize.width - tileSize);
    double y = rnd.nextDouble() * (screenSize.height - tileSize);
    flies.add(Fly(this, x, y));
  }

  void render(Canvas canvas) {
    // ignore: todo
    // TODO: implement render
    Rect bgRect = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
    Paint bgPaint = Paint();
    bgPaint.color = Color(0xfffffffff);
    canvas.drawRect(bgRect, bgPaint);

    flies.forEach((Fly fly) => fly.render(canvas));
}

void onTapDown(TapDownDetails d) {
    print('Box tap down');
    flies.forEach((Fly fly) {
      if (fly.flyRect.contains(d.globalPosition)) {
        fly.onTapDown();
        print('Box Fly outer tap down');
      }
    });
  }

  void update(double t) {
    // ignore: todo
    // TODO: implement update
    flies.forEach((Fly fly) => fly.update(t));
    flies.removeWhere((Fly fly) => fly.isOffScreen);
  }

  void resize(Size size) {
    screenSize = size;
    tileSize = screenSize.width / 9;
  }
}