Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Añadir color a un elemento BodyComponent usando Flame en Flutter

Tiempo de lectura: 2 minutos

En este tutorial os voy a explicar cómo podéis añadir colores a los elementos tipo BodyComponent de Flame Engine (https://examples.flame-engine.org)

En este ejemplo voy a añadir un BodyComponent que representa una barra azul.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class BarraAzul extends BodyComponent {
//parametros pasados
final double posicionX;
final double posicionY;
final double velocidadAngular;
//Constructor
BarraAzul(this.posicionX, this.posicionY, this.velocidadAngular) {
}
@override
Body createBody() {
final bodyDef = BodyDef(
position: Vector2(posicionX, posicionY),
type: BodyType.kinematic,
);
//shape whit color
final shape = PolygonShape()..setAsBoxXY(.15, 1.25);
final fixtureDef = FixtureDef(shape);
return world.createBody(bodyDef)
..createFixture(fixtureDef)
..angularVelocity = radians(velocidadAngular);
}
}
class BarraAzul extends BodyComponent { //parametros pasados final double posicionX; final double posicionY; final double velocidadAngular; //Constructor BarraAzul(this.posicionX, this.posicionY, this.velocidadAngular) { } @override Body createBody() { final bodyDef = BodyDef( position: Vector2(posicionX, posicionY), type: BodyType.kinematic, ); //shape whit color final shape = PolygonShape()..setAsBoxXY(.15, 1.25); final fixtureDef = FixtureDef(shape); return world.createBody(bodyDef) ..createFixture(fixtureDef) ..angularVelocity = radians(velocidadAngular); } }
class BarraAzul extends BodyComponent {
  //parametros pasados
  final double posicionX;
  final double posicionY;
  final double velocidadAngular;
 
  //Constructor
  BarraAzul(this.posicionX, this.posicionY, this.velocidadAngular) {
    
  }

  @override
  Body createBody() {
    final bodyDef = BodyDef(
      position: Vector2(posicionX, posicionY),
      type: BodyType.kinematic,
    );

    //shape whit color
    final shape = PolygonShape()..setAsBoxXY(.15, 1.25);

   final fixtureDef = FixtureDef(shape);

    return world.createBody(bodyDef)
      ..createFixture(fixtureDef)
      ..angularVelocity = radians(velocidadAngular);
  }
}

Esto nos crea la siguiente figura en la pantalla:

Para aplicarle un color, utilizaremos la librería de colores de flame engine package:flame/palette.dart

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import 'package:flame/palette.dart';
import 'package:flame/palette.dart';
import 'package:flame/palette.dart';

Una vez añadida, solo tenemos que crear el color que queramos:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
final Paint azul = BasicPalette.blue.paint();
final Paint azul = BasicPalette.blue.paint();
  final Paint azul = BasicPalette.blue.paint();

En este caso color azul.

Si queremos asignarlo al elemento, solo tendremos que cambiar la variable color en su constructor (la variable color ya viene por defecto en los componentes BodyComponent/ Component.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
BarraAzul(this.posicionX, this.posicionY, this.velocidadAngular) {
paint = azul;
}
BarraAzul(this.posicionX, this.posicionY, this.velocidadAngular) { paint = azul; }
  BarraAzul(this.posicionX, this.posicionY, this.velocidadAngular) {
    paint = azul;
  }

Este es el código resultado:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class BarraAzul extends BodyComponent {
//parametros pasados
final double posicionX;
final double posicionY;
final double velocidadAngular;
final Paint azul = BasicPalette.blue.paint();
//Constructor
BarraAzul(this.posicionX, this.posicionY, this.velocidadAngular) {
paint = azul;
}
@override
Body createBody() {
final bodyDef = BodyDef(
position: Vector2(posicionX, posicionY),
type: BodyType.kinematic,
);
//shape whit color
final shape = PolygonShape()..setAsBoxXY(.15, 1.25);
Paint randomPaint() => PaintExtension.random(withAlpha: 0.9, base: 100);
//final fixtureDef = FixtureDef(shape);
final fixtureDef = FixtureDef(
shape,
restitution: 0.8,
density: 1.0,
friction: 0.4,
);
return world.createBody(bodyDef)
..createFixture(fixtureDef)
..angularVelocity = radians(velocidadAngular);
}
}
class BarraAzul extends BodyComponent { //parametros pasados final double posicionX; final double posicionY; final double velocidadAngular; final Paint azul = BasicPalette.blue.paint(); //Constructor BarraAzul(this.posicionX, this.posicionY, this.velocidadAngular) { paint = azul; } @override Body createBody() { final bodyDef = BodyDef( position: Vector2(posicionX, posicionY), type: BodyType.kinematic, ); //shape whit color final shape = PolygonShape()..setAsBoxXY(.15, 1.25); Paint randomPaint() => PaintExtension.random(withAlpha: 0.9, base: 100); //final fixtureDef = FixtureDef(shape); final fixtureDef = FixtureDef( shape, restitution: 0.8, density: 1.0, friction: 0.4, ); return world.createBody(bodyDef) ..createFixture(fixtureDef) ..angularVelocity = radians(velocidadAngular); } }
class BarraAzul extends BodyComponent {
  //parametros pasados
  final double posicionX;
  final double posicionY;
  final double velocidadAngular;
  final Paint azul = BasicPalette.blue.paint();
  //Constructor
   BarraAzul(this.posicionX, this.posicionY, this.velocidadAngular) {
    paint = azul;
  }

  @override
  Body createBody() {
    final bodyDef = BodyDef(
      position: Vector2(posicionX, posicionY),
      type: BodyType.kinematic,
    );

    //shape whit color
    final shape = PolygonShape()..setAsBoxXY(.15, 1.25);

    Paint randomPaint() => PaintExtension.random(withAlpha: 0.9, base: 100);

    //final fixtureDef = FixtureDef(shape);

    final fixtureDef = FixtureDef(
      shape,
      restitution: 0.8,
      density: 1.0,
      friction: 0.4,
    );

    return world.createBody(bodyDef)
      ..createFixture(fixtureDef)
      ..angularVelocity = radians(velocidadAngular);
  }
}

Y así aparece en la pantalla:

3

Deja un comentario